Results 1 to 2 of 2
  1. #1
    rahilgada is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Sorting Student test results

    kindly help me with this program.thank you in advance


    import javax.swing.JOptionPane;
    import java.util.Arrays;

    public class Test {

    private static Student[] allStudents;

    public static void main(String[] args) {
    Test t = new Test();
    t.start();
    }

    public void start() {

    int numTests = 0;

    int numStudents = 0;

    numTests = Integer.parseInt(askUser(NUM_TESTS_QUERY, NUM_TESTS_TITLE));
    numStudents = Integer.parseInt(
    askUser(NUM_STUDENTS_QUERY, NUM_STUDENTS_TITLE ));

    allStudents = new Student[numStudents];

    for(int studentNo = 0; studentNO < studentNumber; studentNo++) {

    String studentNameQuery = String.format(
    STUDENT_NAME_QUERY_FORMAT, studentNo, numStudents);
    String studentName = askUser(studentNameQuery, STUDENT_NAME_TITLE);

    allStudents[i] = new Student(studentName, numTests);
    }

    printResults();


    String menuText = getMenuText();

    main_loop: while (true) {

    String input = askUser(menuText, MENU_TITLE);

    if (input.length() != 1) continue;

    switch (input.charAt(0)) {

    case 'P': case 'p':
    printResults();
    break;

    case 'A': case 'a':

    String whichTestQuery =
    String.format(WHICH_TEST_QUERY_FORMAT, numTests);
    int whichTest = Integer.parseInt(
    askUser(whichTestQuery, WHICH_TEST_TITLE));
    highestToLowest(whichTest);
    break;

    case 'B': case 'b':
    nameSort();
    break;

    case 'M': case 'm':
    int scores = new int;


    int whichTest = Integer.parseInt(

    askUser(whichTestQuery, WHICH_TEST_TITLE));

    int max = fmax(scores, whichTest);

    search(scores, max, whichTest);



    case 'E': case 'e':

    break main_loop;

    }





    }

    }





    public static void printResults() {




    StringBuilder output = new StrinBuilder();



    for(int student = 0; student < allStudents.length; student++) {


    output.append(rowOfHyphens(22));

    output.append(NEW_LINE);

    output.append(STUDENT_INFO);

    output.append(allStudents[i].lastName);


    output.append(NEW_LINE);



    for(int testNo = 0; testNo <

    allStudents[student].testScores.length; testNo++) {



    output.append(String.format( TEST_SCORE_FORMAT, testNo

    + 1, allStudents[student].testScore[testNo]);

    }



    output.append(NEW_LINE);

    output.append(rowOfHyphens(28));

    output.append(NEW_LINE);

    output.append(NEW_LINE);

    }



    System.out.print(output);

    }



    public static void highestToLowest(int testNumber) {

    if(testNumber != 0) testNumber--;



    StringBuilder output = new StringBuilder();

    for(int i = allStudents.length; i >= 2; i--) {

    for(int j = 0; j < i - 1; j++) {

    if (allStudents[j].testScores[testNumber] >

    allStudents[j + 1].testScores[testNumber]) {



    Student tempStudent = allStudents[j];

    allStudents[j] = allStudents[j + 1];

    allStudents[j + 1] = tempStudent;

    }

    }

    }



    output.append(rowOfHyphens(24));

    output.append(NEW_LINE);

    output.append(String.format(

    SORTED_LIST_DESCRIPTION_FORMAT, testNumber + 1));



    for (int student = 0; student < allStudents.length; student++) {



    output.append(String.format(STUDENT_SCORE_FORMAT,

    allStudents[student].testScores[testNumber],

    allStudents[student].lastName));





    }



    output.append(rowOfHyphens(28));

    output.append(NEW_LINE);



    JOptionPane.showMessageDialog(null, output);

    }



    public static void nameSort() {

    StringBuilder output = new StringBuilder();



    for (int i = allStudents.length; i >= 2; i--) {

    for (int j = 0; j < i - 1; j++) {

    if (allStudents[j].lastName.compareToIgnoreCase(

    allStudents[j + 1].lastName) > 0) {



    Student tempStudent = allStudents[j];

    allStudents[j] = allStudents[j+1];

    allStudents[j+1] = tempStudent;

    }

    }

    }



    output.append(rowOfHyphens(26));

    output.append(NEW_LINE);

    output.append(STUDENT_NAMES_MESSAGE);

    output.append(NEW_LINE);



    for (int student = 0; student < allStudents.length; student++) {

    output.append(allStudents[i].lastName);

    output.append(NEW_LINE);

    }

    output.append(rowOfHyphens(26));

    output.append(NEW_LINE);

    JOptionPane.showMessageDialog(null, output);

    }



    public static int fmax(int a, int qn) {


    int max = 0;

    for(int i = 0; i < a.length; i++) {

    if(max > a[i][qn]) max = a[i][qn];

    }

    return max;

    }



    public static void search(int students, int score, int testNo) {

    for(int student = 0; student < students.length; student++) {

    if(students[student][testNo] == score) {

    System.out.println(MAX_SCORE_MESSAGE_FORMAT, student, testNo);

    }

    }

    }

    private static String askUser(String question, String title) {

    return JOptionPane.showInputDialog(null, question, title,

    JOptionPane.QUESTION_MESSAGE);

    }



    return String.format(MENU_FORMAT, MENU_DIRECTIVE,

    MENU_OPTION_PRINT, MENU_OPTION_SORT_SCORES,

    MENU_OPTION_SORT_STUDENTS, MENU_OPTION_MAX_SCORE,

    MENU_OPTION_EXIT, MENU_QUERY);

    }



    private static String rowOfHyphens(int length) {


    char [] row = new char[length];

    Arrays.fill(row, '-');

    return new String(row);

    }



    private static final char NEW_LINE = '\n';



    private static final String NUM_TESTS_QUERY =

    "How many tests will the students take?";

    private static final String NUM_TESTS_TITLE =

    "Number of Tests?";



    private static final String NUM_STUDENTS_QUERY =

    "How many students are in this class?";

    private static final String NUM_STUDENTS_TITLE =

    "Number of Students?";



    private static final String STUDENT_NAME_QUERY_FORMAT =

    "The name of student number %d of %d?";

    private static final String STUDENT_NAME_TITLE =

    "Name of Student?";


    private static fanal String MENU_FORMAT =

    "%1$s %n%2$s %n%3$s %n%4$s %n%5$s %n%6$s %n%7$s";

    private static final String MENU_DIRECTIVE =

    "Please Choose one of the following Options";

    private static final String MENU_OPTION_PRINT =

    "(P) Print the results for all the students";

    private static final String MENU_OPTION_SORT_SCORES =

    "(A) Sorting the scores in ascending order for a particluar test";

    private static final String MENU_OPTION_SORT_STUDENTS =

    "(B) Sort the students by name";

    private static final String MENU_OPTION_MAX_SCORE =

    "(M) Find Student with the max score";

    private static final String MENU_OPTION_EXIT =

    "(E) Exit the Program";

    private static final String MENU_QUERY =

    "Enter one of the following P, A, B, or E";

    private static final String MENU_TITLE =

    "Choose a Letter Option";



    private static final String WHICH_TEST_QUERY_FORMAT =

    "Which test out of %d would you like to sort by?";

    private static final String WHICH_TEST_TITLE =

    "The number of the Test";



    private static final String STUDENT_INFO =

    "Student information: ";

    private static final String TEST_SCORE_FORMAT =

    "Test %d Score: %d%n";



    private static final String SORTED_LIST_DESCRIPTION_FORMAT =

    "This is the sorted list for test %d%n";

    private static final String STUDENT_SCORE_FORMAT =

    "%d %s%n";



    private static final String STUDENT_NAMES_MESSAGE =

    "This is the names of the students alphabetitized\n";



    private static final String MAX_SCORE_MESSAGE_FORMAT =

    "Student %d has maximum for quiz %d";

    }



    class Student {



    public String lastName;

    public int[] testScores;



    public Student(String name, int testCount) {

    lastName = name;

    testScores = new int[testCount];

    makeScoresOfTests();

    }



    public void makeScoresOfTests() {

    for(int i=0; i < testScores.length; i++) {

    testScores[i]= (int)(Math.random() * 101);

    }

    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,422
    Rep Power
    25

    Default Re: Sorting Student test results

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain what your problems are with the code.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 01-18-2013, 06:32 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •