Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I just need help with my jagged array Java code. My code is completed and works,

ID: 3905869 • Letter: I

Question

I just need help with my jagged array Java code. My code is completed and works, but I'm trying to make it more in a static method meaning converting my code under each case into separate methods and calling upon those methods. So instead of a bunch of code under a CASE, its just the name of the method and the code for that method is in its own public static.

------------------------------------------

My code:

import java.util.*;

public class RaggedArray {

public static void main(String[] args) {

int students[][] = new int[25][];

int index=0;

int maxExams=0;

int choice;

Scanner sc = new Scanner(System.in);

do {

choice = getChoice();

switch (choice) {

case 1:

System.out.println("Please enter how many exams this student has taken?");

int noOfExams=sc.nextInt();

students[index]=new int[noOfExams];

System.out.println("Please enter the each of those exam scores one by one");

if(maxExams<noOfExams)

maxExams=noOfExams;

for(int i=0;i<noOfExams;i++) {

students[index][i]=sc.nextInt();

}

index++;

break;

case 2:

System.out.println("*** Average by Student Id ***");

System.out.println("Student Id Average Score");

for(int i=0;i<index;i++) {

System.out.print((i+1)+" ");

int sum=0;

for(int j=0;j<students[i].length;j++) {

sum=sum+students[i][j];

}

double avg=sum*1.0/students[i].length;

System.out.printf("%.2f",avg);

System.out.println();

}

break;

case 3:

System.out.println("*** Average by Exam Number ***");

System.out.println("Exam Number Average Score");

for (int i = 0; i < maxExams; i++) {

int sum = 0, total = 0;

for (int j = 0; j < index; j++) {

if (students[j].length >= i + 1) {

total++;

sum = sum + students[j][i];

}

}

double avg = sum * 1.0 / total;

System.out.print((i + 1) + " ");

System.out.printf("%.2f", avg);

System.out.println();

}

break;

case 4:

int sumtotal = 0;

for (int i = 0; i < index; i++) {

int sum = 0;

for (int j = 0; j < students[i].length; j++) {

sum = sum + students[i][j];

}

double avg = sum * 1.0 / students[i].length;

sumtotal += avg;

}

double avgTotal = sumtotal * 1.0 / index;

System.out.printf("Current class Average for all exams - " + avgTotal);

break;

case 5:

System.out.println("This program was hard to make. Thank you.");

break;

default:

System.out.println("***Invalid selection, pick again.*** ");

} // end switch

} while (choice != 5);

} // end of menu

public static int getChoice() {

int result;

Scanner input = new Scanner(System.in);

System.out.println(" 1-Input grades for next student");

System.out.println("2-Display the exam Average by student");

System.out.println("3-Display the exam Average by exam");

System.out.println("4-Display the current class average for all exams");

System.out.println("5-Quit");

System.out.print("Enter selection: ");

result = input.nextInt();

return result;

} // end of getChoice

} // end of class RaggedArray

Explanation / Answer

import java.util.*;

public class RaggedArray {

//Declaring variables as static so that they can use inside the static methods, because static member can only access static member.

//non-static members are called by using the object of that class inside a static method.

static int students[][] = new int[25][];

static int index=0;

static int maxExams=0;

static int choice;

static Scanner input = new Scanner(System.in);

//Driver method to test our program which only calls one method to perform action.

public static void main(String[] args) {

performAction();

}

//This method displays menu to the user and asks for the choice from the user

public static int getChoice() {

int result;

System.out.println(" 1-Input grades for next student");

System.out.println("2-Display the exam Average by student");

System.out.println("3-Display the exam Average by exam");

System.out.println("4-Display the current class average for all exams");

System.out.println("5-Quit");

System.out.print("Enter selection: ");

result = input.nextInt();

return result;

} // end of getChoice

//This method perform the specific action based on the choice

public static void performAction() {

do {

choice = getChoice();

switch (choice) {

case 1:

//Calls gradeInput method to take the grade for the respective subject

gradeInput();

break;

case 2:

//Displays average score by student

displayAverageStudent();

break;

case 3:

//Displays average score by exam

displayAverageExam();

break;

case 4:

//Displays average score by current class

displayCurrentClassAvg();

break;

case 5:

//Quitting

System.out.println("This program was hard to make. Thank you.");

break;

default:

//Default message to the user if user types other than 1-5

System.out.println("***Invalid selection, pick again.*** ");

} // end switch

} while (choice != 5);

}

//This methods takes the grade based on the number of subject

public static void gradeInput() {

System.out.println("Please enter how many exams this student has taken?");

int noOfExams=input.nextInt();

students[index]=new int[noOfExams];

System.out.println("Please enter the each of those exam scores one by one");

if(maxExams<noOfExams)

maxExams=noOfExams;

for(int i=0;i<noOfExams;i++) {

students[index][i]=input.nextInt();

}

index++;

}

//Calculating the average by student and displaying the same

public static void displayAverageStudent() {

System.out.println("*** Average by Student Id ***");

System.out.println("Student Id Average Score");

for(int i=0;i<index;i++) {

System.out.print((i+1)+" ");

int sum=0;

for(int j=0;j<students[i].length;j++) {

sum=sum+students[i][j];

}

double avg=sum*1.0/students[i].length;

System.out.printf("%.2f",avg);

System.out.println();

}

}

//Calculating the average by exam and displaying the same

public static void displayAverageExam() {

System.out.println("*** Average by Exam Number ***");

System.out.println("Exam Number Average Score");

for (int i = 0; i < maxExams; i++) {

int sum = 0, total = 0;

for (int j = 0; j < index; j++) {

if (students[j].length >= i + 1) {

total++;

sum = sum + students[j][i];

}

}

double avg = sum * 1.0 / total;

System.out.print((i + 1) + " ");

System.out.printf("%.2f", avg);

System.out.println();

}

}

//Calculating the average by current class and displaying the same

public static void displayCurrentClassAvg() {

int sumtotal = 0;

for (int i = 0; i < index; i++) {

int sum = 0;

for (int j = 0; j < students[i].length; j++) {

sum = sum + students[i][j];

}

double avg = sum * 1.0 / students[i].length;

sumtotal += avg;

}

double avgTotal = sumtotal * 1.0 / index;

System.out.printf("Current class Average for all exams - " + avgTotal);

}

} // end of class RaggedArray

Sample Run:

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

1

Please enter how many exams this student has taken?

4

Please enter the each of those exam scores one by one

50

60

80

90

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

2

*** Average by Student Id ***

Student Id Average Score

1 70.00

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

3

*** Average by Exam Number ***

Exam Number Average Score

1 50.00

2 60.00

3 80.00

4 90.00

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

4

Current class Average for all exams - 70.0

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

6

***Invalid selection, pick again.***

1-Input grades for next student

2-Display the exam Average by student

3-Display the exam Average by exam

4-Display the current class average for all exams

5-Quit

Enter selection:

5

This program was hard to make. Thank you.

SOlution:

RaggedArray.java