Write a complete class definition for a class called HighSchool. Use good princi
ID: 666153 • Letter: W
Question
Write a complete class definition for a class called HighSchool. Use good principles of class design, encapsulation, and inheritance. Reuse code whenever possible.
The HighSchool class:
is a child class of the School class (written for the previous question)
has one additional instance variable called numStudents
has two constructors:
a default constructor sets the name to an empty String and the number of students to zero
a second constructor sets the name and number of students based on parameters
has a toString method that outputs the name and number of students
has an equals method: two HighSchool objects are "the same" if they have the same name and number of students
implements the Comparable interface: HighSchool objects should be ordered by name and then by number of students (from smallest to largest)
Explanation / Answer
public class Highschool extends School implements Comparable<Highschool>{
int numofStudents;
public Highschool(){
numofStudents=0;
}
public Highschool(int n,String s){
name=s;
numofStudents=n;
}
public String toString(){
return "Name:"+name+" Number of Students:"+numofStudents;
}
public boolean equals(Highschool h1,Highschool h2){
if(h1.name.equals(h2.name)){
if(h1.numofStudents==h2.numofStudents){
return true;
}
else {
return false;
}
}
else {
return false;
}
}
@Override
public int compareTo(Highschool o) {
int i=name.compareTo(o.name);
if(i!=0) return i;
return Integer.compare(numofStudents, o.numofStudents);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.