Question

In: Computer Science

A Java question. You are given a Student class. A Student has a name and an...

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

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: []");
        
    }
}

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;
    }

}

Solutions

Expert Solution

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 Solutions

IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
How can I write a separate java class to change a given name or a given...
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails). Note: It should change the name or email that is saved in the txt file. Here is my code so far: User class public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:  Add init() method of the class that initializes string member variables to empty strings and numeric values to 0. (2)  Add the method populate() to the class. The method is used to assign values to the member variables of the class. (2)  Add the method display() to the class. The method is used...
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
This question concerns writing a Student class to model a student's name, composed of a first...
This question concerns writing a Student class to model a student's name, composed of a first name, middle name and last name. The Student class should meet the following specification: Class Student A Student object represents a student. A student has a first name, middle name and last name. Methods public void setNames(String first, String middle, String last) // Set the first, middle and last names of this Student object. public String getFullName() // Obtain the full name of this...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter,...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter, FileReader and BufferedReader to write a program that has functional menu: Menu ------------------------------------------------- Add a list of Students and save to File Loading list of Students from a File Display the list of Students descending by Name Display the list of Students descending by Mark Exit Your choice: _ + Save to File: input information of several students and write that information into a...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT