import static java.lang.System.out;
import java.util.Scanner;
public class StuTest
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int[][] testScores; // [students][tests]
String[] stuNames;
testScores = createArray();
if(testScores != null)
{
stuNames = new String[testScores.length];
populateNames(stuNames);
populateTestScores(stuNames,testScores);
printStudentReport(stuNames,testScores);
printTestReport(stuNames,testScores);
out.println("\n\nGoodbye!\n\n");
}
else
{
out.println("Array not successfully created - exiting");
}
}
// Code your 5 methods below..
// Ask user to enter the number of students, and then asks user to enter the number of tests.
private static int numberOfStudents;
private static int numberOfTests;
public static int[][] createArray()
{
System.out.print("Enter the number of students: ");
numberOfStudents = keyboard.nextInt();
System.out.print("Enter the number of tests: ");
numberOfTests = keyboard.nextInt();
System.out.println();
System.out.println();
int[][] stuArray = new int[numberOfStudents][numberOfTests];
return stuArray;
}
// Ask for the name of each student for each number of students
public static String[] populateNames(String[] stuNames)
{
for(int x = 0; x < stuNames.length; x++)
{
System.out.print("Enter the name for student " + (x + 1) + ": ");
keyboard = new Scanner(System.in);
stuNames[x] = keyboard.nextLine();
}
return stuNames;
}
// Display "Entering Scores for (Student Name)" then input the score for each test.
public static int[][] populateTestScores(String[] stuNames, int[][] testScores)
{
for(int y = 0; y < stuNames.length; y++)
{
for(int u = 0; u < stuNames.length; u++)
{
System.out.println("Entering scores for " + stuNames[y]);
System.out.print(" Enter score for Test " + testScores + ": ");
testScores[y][u] = keyboard.nextInt();
}
}
return testScores;
}
// Print Student Report
public static void printStudentReport(String[] stuNames, int[][] testScores)
{
// Print the name of each student and the scores for each of the student's tests
System.out.println("STUDENT REPORT");
for(int z = 1; z <= stuNames.length; z++)
{
System.out.println("Report for " + stuNames[z]);
for(int a = 1; a <= numberOfTests; a++)
{
System.out.println(" Test " + a + ": " + testScores[a][z]);
}
// Print the average of the student's tests
int p = 0;
for(int o = 1; o <= testScores.length; o++)
{
p += testScores[o][z];
}
p /= testScores.length;
System.out.println(" " + stuNames[z] + "'s average is " + p);
}
// Print Test Report
}
public static void printTestReport(String[] stuNames, int[][] testScores)
{
System.out.println("TEST REPORT");
// Print the results for each individual test, and the average for each test
for(int b = 1; b <= testScores.length; b++)
{
System.out.println("Report for Test" + b);
for(int c = 1; c <= numberOfStudents; c++)
{
System.out.println(" Test " + c + ": " + testScores[c][b]);
}
int p = 0;
for(int o = 1; o <= testScores.length; o++)
{
p += testScores[o][b];
}
p /= testScores.length;
System.out.println(" Test" + b + " average is " + p);
}
}
}