Search code examples
javaarrayssortinglines

How to include a sorting method in program?


Here are the requirements for the program. Essentially the output must be as follows while still adhering to the guidelines below. My question is how do I alter my program to meet requirement #4 (which is writing a method that takes the student data in the 1-D and 2-D arrays and prints the student records: names, 2 quiz scores and the average. The output fields should be well-aligned.) My second question is how do I meet requirement #6 (A header line should be printed before the student records, and a line “-------------------“ should be printed every 2 students).

 # Name    Q1   Q2         

  1. name1 ... ... ...

  2. name2 ... ... ...


  1. name3 ... ... ...

  2. name4 ... ... ...


  1. name5 ... ... ...

================================================================================== 1. Inside the main() method, define a 1-D array that can store 5 student names. Define a 2-D array inside the main() that store the 5 students (rows) exam record that each student has 3 data fields (columns): 2 quiz scores and an average of the 2 quizzes.

  1. Assign student names (real name) to 1-D array and 2 quiz scores to the 2-D array.

  2. Calculate the average of 2 quizzes for each student.

4. Write a method that takes the student data (1-D and 2-D arrays) and sort the student records from high to low by the average field.

  1. Write a method that takes the student data (1-D and 2-D arrays) and prints the student records: names, 2 quiz scores and the average. The output fields should be well-aligned.

6. A header line should be printed before the student records, and a line “-------------------“ should be printed every 2 students.

  1. The following output is an example:

Name Q1 Q2 Avg


  1. name1 ... ... ...

  2. name2 ... ... ...


  1. name3 ... ... ...

  2. name4 ... ... ...


  1. name5 ... ... ...

=================================================================================

Here's what I have so far. Im really having trouble with the sorting method and a line “-------------------“ printed every 2 students.

public class studentGrades {

public static void main(String[] args) {

    //create variables to be used inside the "for" loops as counters
    int i;
    int row;
    int column;

    String[] students = {"Peter", "Lydia", "Kate", "Steve", "Alexa"};



    // Create a 2-D array of the int type to stores scores
    int[][] scores = { {82, 90}, {78,90}, {84, 80}, {85, 73}, {81, 93} };


    // Display headings for information to be spaced equally
    System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name",
            "Test1", "Test2", "Sum of tests ", "Average grade\n");
    System.out.print("----------------------------------------\n");


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


        System.out.print(students[i] + " \n");


    }


    // cycle through the each group (outside part of the array)
        for (row=0; row<scores.length; row++){

            //create variables to store computations and set initial value to zero
            int sum = 0;
            double  avg = 0;
            //System.out.print(students[i]);


            // cycle through each set of elements of each group (inside part of array)
            for (column=0; column<scores[row].length; column++){


                // for each set of elements -- add all elements in each group (int sum must be set to 0 each time)
                     sum += scores[row][column];

                     // calculate average by dividing the value in int sum by the number of elements in the group (group size)
                     avg = sum/scores[column].length;



                     // display the values of each row
                 System.out.print(scores[row][column] + "      ");


            }



        // display the sum of each group (row) with an identifying message
            System.out.print(sum + "           " );


            // display the average of each group (row) with an identifying message
           System.out.print(avg);

        //    System.out.print(" -------------------------\n");
           // create new line after each computation
            System.out.println();

            // Create dotted lines to separate each set of values displayed
            System.out.print("----------------------------------------\n");

    }
    ///////

} }


Solution

  • package Examples;
    
    public class MarksQuiz {
        public static void main(String[] args) {
            // create variables to be used inside the "for" loops as counters
            int i;
            int row;
            int column;
            String[] students = { "Peter", "Lydia", "Kate", "Steve", "Alexa" };
            // Create a 2-D array of the int type to stores scores
            int[][] scores = { { 82, 90 }, { 78, 90 }, { 84, 80 }, { 85, 73 }, { 81, 93 } };
    
            // Display headings for information to be spaced equally
            System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name", "Test1", "Test2", "Sum of tests ", "Average grade\n");
            System.out.print("----------------------------------------\n");
    
            //Removed the For loop from Here
            /*for (i = 0; i < students.length; i++) {
                System.out.print(students[i] + " \n");
            }*/
    
            // cycle through the each group (outside part of the array)
            for (row = 0; row < scores.length; row++) {
    
                    System.out.print(students[row]);//Inserted the Print Statement to Fix your Issue
                        // create variables to store computations and set initial value to
                // zero
                int sum = 0;
                double avg = 0;
                // System.out.print(students[i]);
    
                // cycle through each set of elements of each group (inside part of
                // array)
                for (column = 0; column < scores[row].length; column++) {
    
                    // for each set of elements -- add all elements in each group
                    // (int sum must be set to 0 each time)
                    sum += scores[row][column];
    
                    // calculate average by dividing the value in int sum by the
                    // number of elements in the group (group size)
                    avg = sum / scores[column].length;
    
                    // display the values of each row
                    System.out.print(scores[row][column] + "      ");
    
                }
    
                // display the sum of each group (row) with an identifying message
                System.out.print(sum + "           ");
    
                // display the average of each group (row) with an identifying
                // message
                System.out.print(avg);
    
                // System.out.print(" -------------------------\n");
                // create new line after each computation
                System.out.println();
    
                // Create dotted lines to separate each set of values displayed
                System.out.print("----------------------------------------\n");
    
            }
        }
    }
    

    Output:-

    Name Test1 Test2 Sum of tests Average grade

    Peter82 90 172 86.0

    Lydia78 90 168 84.0

    Kate84 80 164 82.0

    Steve85 73 158 79.0

    Alexa81 93 174 87.0