Java: This exercise extends the Student class you produced for PP7.2. Modify the
ID: 3681909 • Letter: J
Question
Java:
This exercise extends the Student class you produced for PP7.2.
Modify the Student class of PP7.2 as follows:
Add a studentId instance variable that is an int. Modify all the Student constructors to take a studentId. Add a method int getId() to the Student class. Modify the toString method to include studentId.
Modify Student to implement the Comparable interface. The class header will be
public class Student implements Comparable<Student>
The <Student> syntax above means that our compareTo method header will be
public int compareTo(Student other)
Notice that the formal parameter of compareTo is type Student. If we omit the <Student> on the class header, the formal parameter would be type Object. Knowing that a Student will only be compared to other Student objects makes life easier....
Two Student objects are equal if and only if their studentId values are equal. A Student a is "greater" than Student b if and only if Student a's studentId is numerically greater than Student b's.
Write a test driver with a main method to create several Student objects and test the Comparable function. (Suggestion: keep your studentIds small, like 1- and 2-digit values.) Add the student objects to an ArrayList and print the ArrayList. Then call Collections.sort(myArrayList), assuming your ArrayList of students is called myArrayList, and print the ArrayList again. How has myArrayList changed?
Student class from 7.2:
Explanation / Answer
Hi I have modified Student class.
public class Student implements Comparable<Student>{
// variables declaration
private int studentId;
private String firstName, lastName;
private Address homeAddress, schoolAddress;
private int score1, score2, score3;
// default constructor
public Student(int studentId)
{
score1 = 0;
score2 = 0;
score3 = 0;
this.studentId = studentId;
}// end of default constructor
public Student(int studentId, String firstName, String lastName,
Address homeAddress, Address schoolAddress, int score1, int score2,
int score3) {
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public void setId(int id){
studentId = id;
}
public int getId(){
return studentId;
}
@Override
public String toString() {
return "Id: "+studentId+
" Name: "+firstName+" "+lastName+
" Home Address: "+homeAddress.toString()+
" School Address: "+schoolAddress.toString()+
" Scores: 1."+score1+", 2."+score2+", 3."+score3;
}
@Override
public int compareTo(Student o) {
if(this.studentId < o.studentId)
return -1;
else if(this.studentId == o.studentId)
return 0;
else
return 1;
}
}
// I have defined Address class since it is not given in question
class Address{
private int houseNumber;
private String addressLine1;
private String city;
private int pin;
public Address(int houseNumber, String addressLine1, String city, int pin) {
this.houseNumber = houseNumber;
this.addressLine1 = addressLine1;
this.city = city;
this.pin = pin;
}
@Override
public String toString() {
return "House No.-"+houseNumber+
" "+addressLine1+" "+city+" - "+pin;
}
}
################ StudentTest.java ######################
public class StudentTest {
public static void main(String[] args) {
// first creating address object
Address homeAddr = new Address(12, "Kormangla, Block-3", "Bangalore", 560090);
Address schoolAddr = new Address(43, "Magestic, LalPaNI", "Bangalore", 822101);
// now creating Student
Student s1 = new Student(1, "Pk", "Kumar", homeAddr, schoolAddr, 76, 87, 97);
Student s2 = new Student(2, "Mk", "Jain", homeAddr, schoolAddr, 55, 67, 77);
System.out.println(s1.toString());
System.out.println();
System.out.println(s2.toString());
System.out.println();
System.out.println("s1 > s2 ?: "+ (s1.compareTo(s2)==1?"true":"false"));
}
}
/*
Sample Run:
Id: 1
Name: Pk Kumar
Home Address: House No.-12
Kormangla, Block-3
Bangalore - 560090
School Address: House No.-43
Magestic, LalPaNI
Bangalore - 822101
Scores: 1.76, 2.87, 3.97
Id: 2
Name: Mk Jain
Home Address: House No.-12
Kormangla, Block-3
Bangalore - 560090
School Address: House No.-43
Magestic, LalPaNI
Bangalore - 822101
Scores: 1.55, 2.67, 3.77
s1 > s2 ?: false
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.