write a complete class definition for a class called HighSchool, which is a chil
ID: 3680762 • Letter: W
Question
write a complete class definition for a class called HighSchool, which is a child class of the School class you just wrote. A HighSchool is described by its name, whether or not it is private, and the number of grades in the school. Use good principles of class design, encapsulation, and inheritance. Reuse code whenever possible.
Write the class header and the instance data variables for the HighSchool class.
Write two constructors:
a default (no-argument) constructor sets the name of the school to the text "UNKNOWN," sets private to false, and sets the number of grades to 0
a second constructor sets the name, private status, and number of grades based on a parameters
Write appropriate getter/setter methods.
Write a toString method with the following formatting
Name: Mission High (4 grades, private)
Name: Valley High (3 grades)
The word "private" is included only if the high school is private. If it is not, no private status information is included.
Write an equals method. Two high schools are logically equivalent if they have the same name (ignoring capitalization), number of grades, and private status.
Explanation / Answer
Hi Can you please post School class, becaue according to youe question, HighSchool class extends School class, but I can not know structure(Defination) of School class. So please post in comment section.
Hi have written HighSchool calss according to your question(without extending School class)
public class HighSchool {
private String name;
private boolean isPrivate;
private int grade;
public HighSchool() {
name = "UNKNOWN";
grade = 0;
isPrivate = false;
}
public HighSchool(String name, boolean isPrivate, int grade) {
this.name = name;
this.isPrivate = isPrivate;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isPrivate() {
return isPrivate;
}
public void setPrivate(boolean isPrivate) {
this.isPrivate = isPrivate;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Name: "+getName()+"("+getGrade()+" grades"+(isPrivate ? " ,private":" ")+")";
}
@Override
public boolean equals(Object obj) {
if(obj instanceof HighSchool){
HighSchool o = (HighSchool)obj;
return name.equalsIgnoreCase(o.name) &&
grade == o.grade &&
isPrivate == o.isPrivate;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.