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
name1 ... ... ...
name2 ... ... ...
name3 ... ... ...
name4 ... ... ...
================================================================================== 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.
Assign student names (real name) to 1-D array and 2 quiz scores to the 2-D array.
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.
6. A header line should be printed before the student records, and a line “-------------------“ should be printed every 2 students.
name1 ... ... ...
name2 ... ... ...
name3 ... ... ...
name4 ... ... ...
=================================================================================
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");
}
///////
} }
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:-