Question: Can someone help me with the commented questions in java. package SimpleRosterPackage; import java.util.ArrayList; //1 what is the purpose for this class? //2 What method
Can someone help me with the commented questions in java.
package SimpleRosterPackage;
import java.util.ArrayList;
//1 what is the purpose for this class?
//2 What method does the Comparable interface make you implement?
public class Student implements Comparable
private String lastName;
private String firstName;
private ArrayList
public Student (String ln, String fn) {
lastName = ln;
firstName = fn;
//3 What is happening in the following line?
//Creates a arraylist called integer
//4 When does this get executed?
grades = new ArrayList
}
public double getAverage () {
int total = 0;
for (Integer g : grades) {
total += g.intValue();
}
//5 in the following code, why am I returning -1?
//if the grade size doesn't == 0
if (grades.size()==0)
return -1;
return 1.0*total/grades.size();
}
public boolean equals (Student rhs) {
//6 What is rhs?
//7 What is the difference between lastName and rhs.lastName?
//8 What makes two Student objects equal?
//9 Could I use rhs.lastname == lastName instead of .equals?
if (rhs.lastName.equals(lastName)
&& rhs.firstName.equals(firstName))
return true;
return false;
}
public String toString () {
String toReturn = lastName + "," + firstName + "\t";
toReturn += "Grades: "+grades.toString() + "\t";
toReturn += "Average: "+getAverage();
return toReturn;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void addGrade(Integer g) {
grades.add(g);
}
public int compareTo(Student rhs) {
//10 what does the compareTo return?
//returns the student
//11 What is this compareTo method for?
//Returns a - or + on comparing the last names
if (lastName.compareTo(rhs.getLastName()) < 0){
return -1;
}
if (lastName.compareTo(rhs.getLastName()) > 0){
return 1;
}
if (firstName.compareTo(rhs.getFirstName()) < 0){
return -1;
}
if (firstName.compareTo(rhs.getFirstName()) > 0){
return 1;
}
return 0;
}
}
package SimpleRosterPackage;
import java.util.TreeSet;
//12 What is the purpose for this class?
//to empliment the tree storage
public class Roster {
//13 What is a TreeSet?
//class that uses a tree for storage
//14 What is
private TreeSet
private String className;
public Roster (String cn) {
className = cn;
students = new TreeSet
}
public String toString () {
String toReturn = className + ": ";
// What is the following for loop for?
//loops out students in the tree set
for (Student s : students) {
toReturn += s.toString() + " ";
}
return toReturn;
}
public Student getStudent (Student tester) {
//15 What does the floor method of the TreeSet Class do?
//it looks for element, if it doesn't find the element it returns
//the closest one
Student toReturn = students.floor(tester);
if (toReturn == null)
return null;
//16 Hard question: how are we comparing Students to test if they are equal?
//it only spits out the students
if (toReturn.equals(tester))
return toReturn;
return null;
}
public void addStudent (Student s) {
//17 What does this addStudent method do?
//adding new students
students.add(s);
}
}
package SimpleRosterPackage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class RosterTester {
public static void main(String[] args) {
// create a empty list of students
ArrayList empty = new ArrayList();
Roster csci3381 = new Roster ("OO with Java");
// create a single student and print
Student aStudent = new Student("Mouse","Micky");
aStudent.addGrade(95);
System.out.println("printing a single student");
System.out.println(aStudent);
System.out.println();
// add the previously created student to this collection of students
csci3381.addStudent(aStudent );
//18 What do the following lines of code do?
//Adds studnets names to new students
csci3381.addStudent(new Student("Mouse","Minnie") );
csci3381.addStudent(new Student("Duck","Daffy") );
csci3381.addStudent(new Student("Duck","Donald") );
// print out the collection of students (only one has a grade)
//19 Why don't three of the students have any grades?
//20 Why are they not printed out in the order that they were added?
System.out.println("printing entire class one grade added");
System.out.println(csci3381);
System.out.println();
//21 How many Micky Mouses are there after the next line?
Student testParameter = new Student ("Mouse","Micky");
//22 what is the following line of code for?
Student addTo = csci3381.getStudent(testParameter);
addTo.addGrade(100);
System.out.println("printing entire class two grades added");
System.out.println(csci3381);
System.out.println();
//23 How would this behave if I made changed the previous code to:
// Student testParamter = new Student ("Duck","Micky");
// Why is the following code the incorrect way to add a grade to a student...
//You have to card code the grade in
testParameter = new Student ("Duck","Daffy");
testParameter.addGrade(50);
addTo = csci3381.getStudent(testParameter);
System.out.println("printing entire class ??? grades added");
System.out.println(csci3381);
System.out.println();
// this is the BEST way to add a student. first, create a temp parameter
testParameter = new Student ("Fudd","Elmer");
// try to retrieve the Student object, but it doesn't exist
addTo = csci3381.getStudent(testParameter);
//24 What does the following if do?
if (addTo == null) {
addTo = testParameter;
csci3381.addStudent(addTo);
}
//25 Now When addTo is changed, this changes the
// grade of the student in the roster. Why?
//The adds to adds the 90 to elmer fudd
addTo.addGrade(90);
System.out.println("printing entire class 3 grades added");
System.out.println(csci3381);
System.out.println();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
