A Java question. You are given a Student class. A Student has a name and an Arra
ID: 3856392 • 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
/*
* Implementation of Classroom class that contains
* methods to add student , to get name of the student
* whose average is greater than target average,
* method to get best student and methods to get
* names of the students int the class room object
* and toString method that returns the student objects
* as string.
* */
//Classroom.java
import java.util.ArrayList;
public class Classroom
{
//declaration of ArrayList of type Student
private ArrayList<Student> stdList;
//constructor of class
public Classroom()
{
//instantiage stdList of type ArrayList of type Student
stdList=new ArrayList<Student>();
}
//Method that takes Student s as input argument
public void add(Student s)
{
//add student, s to the stdList
stdList.add(s);
}
//Method that returnst the average student name
//whose average is greater than than target
public String hasAverageGreaterThan(double target)
{
String result="";
boolean found=false;
for (int i = 0; i < stdList.size() &&!found; i++)
{
if(stdList.get(i).getAverage()>target)
{
result=stdList.get(i).getName();
found=true;
}
}
return result;
}
//Method that returns the student names as
//ArrayList of type string
public ArrayList<String> getStudents()
{
ArrayList<String>studentNames=new ArrayList<String>();
for (Student student : stdList)
{
studentNames.add(student.getName());
}
return studentNames;
}
//Method that returns the Student object
//whose average greater than highest average.
public Student bestStudent()
{
Student highestAvgStdudent=null;
if(stdList.size()>0)
{
highestAvgStdudent=stdList.get(0);
boolean found=false;
for (int i = 1; i < stdList.size(); i++)
{
if(stdList.get(i).getAverage()>highestAvgStdudent.getAverage())
{
highestAvgStdudent=stdList.get(i);
}
}
}
return highestAvgStdudent;
}
//Method that returns the student as string value.
public String toString()
{
String descriptionStudents="";
for (Student student : stdList)
{
descriptionStudents+=student.toString();
}
return descriptionStudents;
}
}
---------------------------------------------------------------------------------------------
import java.util.ArrayList;
public class ClassroomTester
{
public static void main(String[] args)
{
ArrayList<Double> grades1 = new ArrayList<>();
grades1.add(82.0);
grades1.add(91.5);
grades1.add(85.0);
Student student1 = new Student("Srivani", grades1);
ArrayList<Double> 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<Double> 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<Double> 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: []");
}
}
---------------------------------------------------------------------------------------------
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;
}
}
---------------------------------------------------------------------------------------------
Sample 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: []
Note : Student.java and ClassroomTester.java have no changes . They are given as supported files.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.