A Java question. You are given a Student class. A Student has a name and an Arra
ID: 3856059 • 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
import java.util.ArrayList;
import java.util.Collections;
/**
* Models a classroom with a collection
* of students
*/
public class Classroom
{
private ArrayList<Student> studentList;
/**
* Constructor for Classroom with
* no arguments
*/
public Classroom(){
this.studentList=new ArrayList<Student>();
}
/**
* add a student to classroom
*/
public void add(Student s){
studentList.add(s);
}
/**
* gets the name of the first student in the Classroom whose average greater than all
*/
public String hasAverageGreaterThan(double target){
boolean flag = true;
String name="";
for(int i=0; (i<studentList.size()) && flag ;i++){
if(studentList.get(i).getAverage() > target){
flag=false;
name=studentList.get(i).getName();
}
}
return name;
}
/**
* returns a list containing the names of all the Students in this Classroom.
*/
public ArrayList<String> getStudents(){
ArrayList<String> names= new ArrayList<String>();
for(int i=0; i<studentList.size();i++){
names.add(studentList.get(i).getName());
}
return names;
}
/**
* gets the Student with the highest average in this classroom or null there are no students
*/
public Student bestStudent(){
ArrayList<Double> avg= new ArrayList<Double>();
double maxAvg;
int id=-1;
for(int i=0; i<studentList.size();i++){
maxAvg=studentList.get(i).getAverage();
avg.add(maxAvg);
if(maxAvg == Collections.max(avg)) id=i;
}
if (id == -1) return null;
else return studentList.get(id);
}
/**
* @overrides
*/
public String toString() {
String s = studentList.toString();
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.