Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

answer the questions through out tthe code ; student: package SimpleRosterPackage; import java.util.ArrayList; //1 what is the purpose for this class? //2 What method does

answer the questions through out tthe code ;

student:

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

public Student (String ln, String fn) {

lastName = ln;

firstName = fn;

//3 What is happening in the following line?

//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 (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?

//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 for?

private TreeSet students;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

OUTCOME 3 Describe pay equity and strategies for implementing it.

Answered: 1 week ago