Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PLEASE THANK YOU SO MUCH! In the next questions, write a complete class def

ID: 3734119 • Letter: J

Question

JAVA PLEASE THANK YOU SO MUCH!

In the next questions, write a complete class definition for a class called School. A school is described by: a name and the number of students. Use good principles of class design and encapsulation.

Use good principles of class design, encapsulation, and inheritance. Reuse code whenever possible.

Question 1

Write the class header and the instance data variable for the School class.

Question 2

Write two constructors:

one constructor sets the name and number based on a parameters

a second constructor sets the name based on a parameter and uses the default value of being a school with 0 students

Question 3

Write appropriate getter/setter methods. Include validity checks where appropriate.

Question 4

Write a toString method that generates a text representation in the following format:

Name: Milton Elementary

Number of students: 400

In the next questions, 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, the number of students in the school, and whether or not the school is a specialized school (e.g., a science magnet school or a performing arts school).

Use good principles of class design, encapsulation, and inheritance. Reuse code whenever possible.

Question 1

Write the class header and the instance data variables for the HighSchool class.

Question 2

Write one constructor that sets the name, number of students, and whether or not the school is specialized based on parameters

Question 3

Write appropriate getter/setter methods. Include validity checks where appropriate.

Question 4

Write a toString method that generates a text representation using the following formatting.

Name: Clark High School

Number of students: 947

Specialized: true

Question 5

Write an equals method for the HighSchool class. Two high schools are logically equivalent if they have the same name (ignoring capitalization), number of students, and specialization status.

Question 6

Have the HighSchool class implement the Comparable interface.

Write the new class header.

Write the full compareTo method. High schools are compared/ordered first by name and then by the number of students.

Question 7

Write code that would go inside of a main method.

Creates an array of 5 School objects that contains some School and some HighSchool objects.

Use a loop to print a text representation of each object.

Use a loop and polymorphism to count the number of high schools that are specialized.

Explanation / Answer

public class School { private String name; private int numberOfStudents; public School(String name, int numberOfStudents) { this.name = name; this.numberOfStudents = numberOfStudents; } public School(String name) { this.name = name; this.numberOfStudents = 0; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumberOfStudents() { return numberOfStudents; } public void setNumberOfStudents(int numberOfStudents) { if(numberOfStudents < 0) { numberOfStudents = 0; } this.numberOfStudents = numberOfStudents; } @Override public String toString() { return String.format("Name: %s Number of students: %d", name, numberOfStudents); } }