In: Computer Science
Java:
Declare a two-dim array that represents five students with four test scores.
Assign 100 for all tests to all students.
Change the 3rd student’s test 2 to 50.
Change the last student’s last test to 87
Print out all test scores.
Calculate the total points of all tests of all students
//Java code
public class Main {
public static void main(String[] args)
{
/**
* Declare a two-dim array that represents five students with four test scores.
*/
String[][] array = new String[5][5];
/**
* Assign 100 for all tests to all students.
*/
for (int i = 0; i <array.length ; i++) {
array[i][0]="Student"+(i+1);
for (int j = 1; j <array[i].length ; j++) {
array[i][j] = "100";
}
}
//Change the 3rd student’s test 2 to 50.
array[2][2] = "50";
//Change the last student’s last test to 87
array[4][4] = "87";
System.out.println("\n===========================================================================");
//heading
System.out.println(String.format("%10s %10s %10s %10s %10s","Student","Score1","Score2","Score3","Score4"));
//Print out all test scores.
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[i].length ; j++) {
System.out.print(String.format("%10s",array[i][j]));
}
System.out.print("\n");
}
//Calculate the total points of all tests of all students
int[] sum = new int[5];
for (int i = 0; i <array.length ; i++) {
for (int j = 1; j <array[i].length ; j++) {
sum[i]+=Integer.parseInt(array[i][j]);
}
}
System.out.println("\n===========================================================================");
//Print report
//heading
System.out.println(String.format("%10s %10s %10s %10s %10s %10s","Student","Score1","Score2","Score3","Score4","Total"));
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[i].length ; j++) {
System.out.print(String.format("%10s ",array[i][j]));
}
System.out.print(String.format("%10s ",sum[i]));
System.out.print("\n");
}
}
}
//output

//If you need any help regarding this solution ......... please leave a comment ........ thanks