Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions