Implement the following UML diagram. - numStudents: int//total number of student
ID: 3854852 • Letter: I
Question
Implement the following UML diagram. - numStudents: int//total number of students - name: String//name of school + School (name: String) + getName (): String + setName (n: String): void getNumStudents (): int + setNumStudents (n: int): void + toString (): String - principalName: String//name of high school's principal - numTeachers: int//number of teachers + HighSchool (name: String, numS: int, p: String, numT: int) + getPrincipal (): String + setPrincipal (p: String): void + getNumTeachers (): int + setNumTeachers (nt: int): void + calculateRatio (): double//ratio of teachers: students + toString (): StringExplanation / Answer
Below is the code for the question. In case of any issues, please post a comment. If happy with the answer, please rate it. Thank you.
School.java
public class School {
private int numStudents;
private String name;
public School(String name)
{
this.name = name;
}
public int getNumStudents() {
return numStudents;
}
public void setNumStudents(int n) {
this.numStudents = n;
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
public String toString()
{
return "Name: " +name +", No. of Students: " + numStudents;
}
}
HighSchool.java
public class HighSchool extends School {
private String principalName;
private int numTeachers;
public HighSchool(String name, int numS, String P, int numT)
{
super(name);
setNumStudents(numS);
this.principalName = P;
this.numTeachers = numT;
}
public String getPrincipalName() {
return principalName;
}
public void setPrincipalName(String p) {
this.principalName = p;
}
public int getNumTeachers() {
return numTeachers;
}
public void setNumTeachers(int nt) {
this.numTeachers = nt;
}
public double calculateRatio()
{
return getNumTeachers() * 1.0 / getNumStudents();
}
public String toString()
{
return super.toString() + ", Principal: " + principalName +
", No. of Teachers: " + numTeachers + ", Ratio: " + calculateRatio();
}
}
SchoolTest.java
public class SchooTest {
public static void main(String[] args) {
School school = new School("Groton School");
school.setNumStudents(50);
System.out.println("school = " + school);
HighSchool highschool = new HighSchool("Troy High School", 100, "George", 5);
System.out.println("highschool = " + highschool);
}
}
output
school = Name: Groton School, No. of Students: 50
highschool = Name: Troy High School, No. of Students: 100, Principal: George, No. of Teachers: 5, Ratio: 0.05