Question
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables.
Write a class named Classroom which manages Student objects.
You will provide the following:
1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target or the empty string. Do not use break. Do not return from the middle of the loop. Use a boolean flag if you need to terminate early. 4. public ArrayList
Provide Javadoc
----------------------------------------------------------------------------------------------------------------------------------------------------------------
ClassroomTester.java
import java.util.ArrayList; public class ClassroomTester { public static void main(String[] args) { ArrayListgrades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList grades3 = new ArrayList<>(); grades3.add(100.0); grades3.add(98.0); grades3.add(100.0); grades3.add(97.0); Student student3 = new Student("Maria", grades3); ArrayList grades4 = new ArrayList<>(); grades4.add(80.0); grades4.add(70.0); grades4.add(82.0); grades4.add(75.0); Student student4 = new Student("Fred", grades4); Classroom myClass = new Classroom(); myClass.add(student1); myClass.add(student2); myClass.add(student3); myClass.add(student4); System.out.println(myClass); System.out.println("Expected: [[Student:name=Srivani,grades=[82.0, 91.5, 85.0]], [Student:name=Carlos,grades=[95.0, 87.0, 99.0, 100.0]], [Student:name=Maria,grades=[100.0, 98.0, 100.0, 97.0]], [Student:name=Fred,grades=[80.0, 70.0, 82.0, 75.0]]]"); System.out.println(">90 GPA: " + myClass.hasAverageGreaterThan(90.0)); System.out.println("Expected: Carlos"); System.out.println(">99 GPA: " + myClass.hasAverageGreaterThan(99)); System.out.println("Expected: "); Student best = myClass.bestStudent(); if (best != null) { System.out.println(best.getName()); System.out.println("Expected: Maria"); } System.out.println(myClass.getStudents()); System.out.println("Expected: [Srivani, Carlos, Maria, Fred]"); //test with an empty classroom myClass = new Classroom(); System.out.println(myClass); System.out.println("Expected: []"); System.out.println(">90 GPA: " + myClass.hasAverageGreaterThan(90.0)); System.out.println("Expected: "); best = myClass.bestStudent(); if (best != null) { System.out.println(best.getName()); } System.out.println(myClass.getStudents()); System.out.println("Expected: []"); } }
Student.java
import java.util.ArrayList; /** * Models a student with a name and collection * pf grades */ public class Student { private String name; private ArrayListgrades; /** * Constructor for Student with name * and list of grades * @param name the name of the student * @param list the list of grades */ public Student(String name, ArrayList list) { this.name = name; this.grades = list; } /** * Gets the name of the student * @return the student's name */ public String getName() { return name; } /** * Gets the average of this student's grades * @return the average or 0 if there are no grades */ public double getAverage() { double sum = 0; for ( double g : grades) { sum = sum + g; } double average = 0; if (grades.size() > 0) { average = sum / grades.size(); } return average; } /** * @overrides */ public String toString() { String s = "[Student:name=" + name + ",grades=" + grades.toString() +"]"; return s; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started