Question: import java.util.ArrayList; / * * * A class to track a student's name and a list of numeric grades for * that student. * *

import java.util.ArrayList;
/**
* A class to track a student's name and a list of numeric grades for
* that student.
*
* @author Jim Teresco
* @version Fall 2019
*/
public class StudentGrades {
// we have a student's name and a list of that student's grades
// as the fields for this class
private String name;
private ArrayList grades;
/**
* Construct a new student, who has no grades when starting out.
*
* @param name The student's name.
*/
public StudentGrades(String name){
this.name = name;
grades = new ArrayList();
}
/**
* Add a grade to a for the student.
*
* @param The grade to add to this student's grade list.
*/
public void addGrade(double grade){
grades.add(grade);
}
/**
* Get this student's name.
*
* @return The student's name.
*/
public String getName(){
return name;
}
/**
* Determine equality of StudentGrades objects, defined such that two StudentGrades objects
* are equal if they have the same name field
*
* @param obj The other object.
* @return true If this and obj have the same name field.
*/
public boolean equals(Object obj){
StudentGrades other =(StudentGrades) obj;
return other.name.equals(name);
}
/**
* Returns a string representing the StudentGrade object.
*
* @return Returns a string representing the StudentGrade object.
*/
@Override
public String toString(){
StringBuilder s = new StringBuilder(name +":");
for (double score : grades){
s.append(""+ score);
}
return s.toString();
}
}
import java.util.ArrayList;
import java.util.Scanner;
/**
* Example CourseGrades: a program to keep track of course
* grades for a group of students. Uses ArrayLists in a few
* ways.
*
* @author Jim Teresco
* @version Fall 2019
*/
public class CourseGrades {
// We will have some instance variables to track the
// contents of the list of students and their grades
// and a set of methods to interact with the list, which
// is done from main, below.
private ArrayList studentList;
/**
* Construct an empty CourseGrades.
*/
public CourseGrades(){
studentList = new ArrayList();
}
/**
* Method to add a grade for a (possibly new) student.
*
* @param name The student's name.
* @param grade The grade to add for this student.
*/
public void add(String name, double grade){
StudentGrades sg = findStudentGradesByName(name);
if (sg != null){
sg.addGrade(grade);
}
else {
sg = new StudentGrades(name);
sg.addGrade(grade);
studentList.add(sg);
}
}
/**
* Print out all of the students and their grades.
*/
public void printAll(){
for (StudentGrades sg : studentList){
System.out.println(sg);
}
}
/*
* Private helper method to look up the StudentGrades object
* from the list that matches the name. Returns null if none.
*/
private StudentGrades findStudentGradesByName(String name){
for (StudentGrades sg : studentList){
if (sg.getName().equals(name)) return sg;
}
return null;
}
/**
* This is the main method for your program.
*
* @param args No command line arguments are required.
*/
public static void main(String[] args){
CourseGrades cg = new CourseGrades();
cg.add("Linus",83.5);
cg.add("Snoopy",95);
cg.add("Linus",91);
}
}
make memory diagram for main method of the CourseGrades class is executed and the program is currently executing
the last line in the main method:
cg.add(Linus,91);
right before the call it makes to
sg.addGrade(grade);
returns.

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!