Question: I have a MyStudent Class and a Driver class that is used to create a deep copy of the MyStudent objects and then print each

I have a MyStudent Class and a Driver class that is used to create a deep copy of the MyStudent objects and then print each students name and average score using the Driver class. I am having trouble fully understanding it. I am looking for someone to comment in or at least explain block by block or even line by line what the code is doing. Thank you in advance.

//MyClass

public class MyStudent{ private int[] scores; private int age; private String name; public MyStudent(int numScores){ age = 0; name = ""; scores = new int[numScores]; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public int[] getScores(){ return scores; } public void setScore(int score, int location){ scores[location] = score; } public String getName(){ return name; } public void setName(String name){ this.name = name; } }

//MyDriver

import java.util.Random; public class Driver{ public static MyStudent[] copy(MyStudent[] original) { MyStudent[] arr = new MyStudent[original.length]; for (int i = 0; i < arr.length; i++) { // MyStudent student = new MyStudent(original[i].getScores().length); student.setAge(original[i].getAge()); student.setName(original[i].getName()); for (int j = 0; j < original[i].getScores().length; j++) { student.setScore(original[i].getScores()[j], j); } arr[i] = student; } return arr; }

public static double printAverages(MyStudent[] original) { double total = 0, count = 0; for (int i = 0; i < original.length; i++) { double sum = 0; for (int j = 0; j < original[i].getScores().length; j++) { sum += original[i].getScores()[j]; } double avg = sum / original[i].getScores().length;

total += avg; count++;

System.out.println(original[i].getName() + ":" + avg); }

return total / count; } public static void main(String args[]){ Random rand = new Random();

MyStudent[] students = new MyStudent[4]; for(int i=0; i<4; i++){ //create a new student students[i] = new MyStudent(10); //give that student a random age between 10 and 60 students[i].setAge(rand.nextInt(51) + 10); for(int j=0; j<10; j++){ //give that student random scores between 0 and 50 students[i].setScore(rand.nextInt(51), j); } }

students[0].setName("John"); students[1].setName("Jane"); students[2].setName("Jen"); students[3].setName("Jake"); MyStudent[] newList = copy(students); printAverages(newList); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!