Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

School Class: public class School { private String name; private int numberOfStudents; public School(String name, int numberOfStudents) { this.name = name; this.numberOfStudents = numberOfStudents; }

School Class:

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); } }

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions