I just can\'t seem to get number 2 right everything else is fine. Inside the mai
ID: 3585784 • Letter: I
Question
I just can't seem to get number 2 right everything else is fine.
Inside the main() method (10%)
a. Define a 1-D array that can store 5 student names.
b. Define a 2-D array that store the 5 students (rows) grade record that each student has 3
data fields (columns): 2 quiz scores and an average of the 2 quizzes. (10%)
c. Call method defined in item 3 to get average of each student.
d. Call method defined in item 4 to sort student records.
e. Call method defined in item 5 to print out student records.
2. Inside the main() method - Assign student names to 1-D array and 2 quiz scores to the 2-D array.
(10%) (Note: Hard code the assigned values in your program, do not take the input from
keyboard.)
3. Write a method that calculates the average of 2 quizzes for each student. (10%)
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. (10%)
5. 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. (10%)
6. A header line should be printed before the student records, and a line “-------------------“ should be
printed every 2 students. (10%)
7. The following output is an example:
# Name Q1 Q2 Avg
1. name1 ... ... ...
2. name2 ... ... ...
3. name3 ... ... ...
4. name4 ... ... ...
5. name5 ... ... ...
Explanation / Answer
As per your requirement I am answering 2.:-
For assigning names to 1-D array, it must be an array of String
String name[] = new String[5];
To assign names:-
// to take input manually from user use scanner class in java by importing java.util.*;
// Define object of scanner class
Scanner scan = new Scanner(System.in);
for(int i=0;i<5;i++)
{
name[i] = scan.nextLine();
scan.next(); // to take the curser to next new line input
}
for assigning quiz scores, it must be 5X2 array for sutudents 5 rows required and 2 entries in each.
float scores[][]=new float[5][2];
Let us assign, first both quiz scores of 1st student then of 2nd then of 3rd and so on;
for(int i=0;i<5;i++)
{
for(int j=0;j<2;j++)
{
score[i][j] = scan.nextFloat();
}
}