Create a dynamic array on the heap Allow the user to choose
Create a dynamic array on the heap. Allow the user to choose the size of the array. The array will store a struct. The struct will have five fields, name, exam1, exam2, exam3, and exam4. The program will have the following menu:
Add a student (this populates all five fields for the student)
Display all student records (shows a list of names and grades)
Display student average (shows a list of name and average)
Display class average
Quit
Solution
class Student {
private String name;
private Double exam1;
private Double exam2;
private Double exam3;
private Double exam4;
public Student(String name, Double exam1, Double exam2, Double exam3, Double exam4) {
this.name = name;
this.exam1 = exam1;
this.exam2 = exam2;
this.exam3 = exam3;
this.exam4 = exam4;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getExam1() {
return this.exam1;
}
public void setExam1(String Exam1) {
this.exam1 = exam1;
}
public String getExam2() {
return this.exam2;
}
public void setExam2(String Exam2) {
this.exam2 = exam2;
}
public String getExam3() {
return this.exam3;
}
public void setExam3(String Exam3) {
this.exam3 = exam3;
}
public String getExam4() {
return this.exam4;
}
public void setExam4(String Exam4) {
this.exam4 = exam4;
}
}
public class MainClass {
public static void main(String[] a) {
//Add student to array list
ArrayList<Student> student = new ArrayList<Student>();
student.add(new Student(\"Kevin\", 75,85,55,59));
student.add(new Student(\"Peter\", 90,80,80,60));
student.add(new Student(\"Dwayne\", 90,60,80,70));
student.add(new Student(\"John\", 66,70,75,84));
student.add(new Student(\"Rick\", 52,53,54,72));
int student_average = 0;
int class_total = 0;
int class_average = 0;
// To Display name and grades of students
for(int x = 0; x<student.size();x++){
System.out.println(\"Name of Student :: \" +student.getName(x));
System.out.println(\"Exam 1 :: \"+ student.getExam1(x));
System.out.println(\"Exam 2 :: \"+student.getExam2(x));
System.out.println(\"Exam 3 :: \"+student.getExam3(x));
System.out.println(\"Exam 4 :: \"+student.getExam4(x));
}
//calculating the average of each Student\'s marks
for(int x = 0; x<student.size();x++){
System.out.println(\"Name of Student :: \" +student.getName(x));
student_average = (student.getExam1(x) + student.getExam2(x) +student.getExam3(x) +student.getExam4(x))/4 ;
System.out.println(\"Name of Student :: \" +student.getName(x) + \"Average :: \" + student_average);
class_total = class_total + student_average;
}
//calculating the average of full class
class_average = class_total/student.size();
}
}



