Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 getStudents() gets an ArrayList containing the names of all the Students in this Classroom. 5. public Student bestStudent() gets the Student with the highest average in this classroom or null there are no students 6. public String toString() gets a string represent ion using ArrayList's toString method

Provide Javadoc

----------------------------------------------------------------------------------------------------------------------------------------------------------------

ClassroomTester.java

import java.util.ArrayList; public class ClassroomTester { public static void main(String[] args) { ArrayList grades1 = 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 ArrayList grades; /** * 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

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

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

2. How do they influence my actions?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago