Question
answer the questions through out tthe code ; public int compareTo(Student rhs) { //10 what does the compareTo return? //11 What is this compareTo method
answer the questions through out tthe code ;
public int compareTo(Student rhs) {
//10 what does the compareTo return?
//11 What is this compareTo method for?
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;
}
}
roster:
package SimpleRosterPackage;
import java.util.TreeSet;
//12 What is the purpose for this class?
public class Roster {
//13 What is a TreeSet?
//14 What is
private TreeSet
private String className;
public Roster (String cn) {
className = cn;
students = new TreeSet
}
public String toString () {
String toReturn = className + ": ";
// Waht is the following for loop for?
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?
Student toReturn = students.floor(tester);
if (toReturn == null)
return null;
//16 Hard question: how are we comparing Students to test if they are equal?
if (toReturn.equals(tester))
return toReturn;
return null;
}
public void addStudent (Student s) {
//17 What does this addStudent method do?
students.add(s);
}
}
rostertester:
package SimpleRosterPackage;
public class RosterTester {
public static void main(String[] args) {
// create a empty list of students
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?
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...
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?
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started