A Java question. You are given a Student class. A Student has a name and an Arra
ID: 3857493 • Letter: A
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 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<String> getStudents() gets an ArrayList<String> 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
Student.java
Explanation / Answer
ClassroomTester.java
import java.util.ArrayList;
public class ClassroomTester
{
public static void main(String[] args)
{
ArrayList<Double> grades1 = new ArrayList<Double>();
grades1.add(82.0);
grades1.add(91.5);
grades1.add(85.0);
Student student1 = new Student("Srivani", grades1);
ArrayList<Double> grades2 = new ArrayList<Double>();
grades2.add(95.0);
grades2.add(87.0);
grades2.add(99.0);
grades2.add(100.0);
Student student2 = new Student("Carlos", grades2);
ArrayList<Double> grades3 = new ArrayList<Double>();
grades3.add(100.0);
grades3.add(98.0);
grades3.add(100.0);
grades3.add(97.0);
Student student3 = new Student("Maria", grades3);
ArrayList<Double> grades4 = new ArrayList<Double>();
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<Double> 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<Double> 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;
}
}
Classroom.java
import java.util.ArrayList;
public class Classroom {
ArrayList<Student> list = new ArrayList<Student>();
public Classroom(){
}
public void add(Student s){
list.add(s);
}
public String hasAverageGreaterThan(double target) {
boolean found = false;
String name = "";
for(int i =0;i<list.size() && !found; i++){
Student s= list.get(i);
if(s.getAverage() > target){
found = true;
name = s.getName();
}
}
return name;
}
public ArrayList<String> getStudents() {
ArrayList<String> nameList = new ArrayList<String>();
for(Student s: list){
nameList.add(s.getName());
}
return nameList;
}
public Student bestStudent() {
if(list.isEmpty()) {
return null;
}
double average = list.get(0).getAverage();
int index = 0;
for(int i =0;i<list.size(); i++){
Student s= list.get(i);
if(s.getAverage() > average){
average = s.getAverage();
index = i;
}
}
return list.get(index);
}
public String toString() {
return list.toString();
}
}
Output:
[[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]]]
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]]]
>90 GPA: Carlos
Expected: Carlos
>99 GPA:
Expected:
Maria
Expected: Maria
[Srivani, Carlos, Maria, Fred]
Expected: [Srivani, Carlos, Maria, Fred]
[]
Expected: []
>90 GPA:
Expected:
[]
Expected: []
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.