use classes and inheritence For this assignment, you will create a hierarchy of
ID: 3668496 • Letter: U
Question
use classes and inheritence
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment.
Person
Variables:
String firstName - Holds the person's first name
String lastName - Holds the person's last name
Methods:
Person(String fName, String lName) - Constructor that takes in String parameters representing the first and last names
String toString() - Returns a String with the following format:
lastName, firstName
Student extends Person
Variables:
int studentId - Using a static variable to keep track of how many students there are, every student should be assigned a unique value for their own studentId.
int level - Represents a student's grade level with possible values ranging from 0 to 12, where 0 represents kindergarten.
Methods:
Student(String fName, String lName, int gLevel) - Constructor that accepts the first and last names and the student level. Student level should be assigned 0 if gLevel is not between 0 and 12 inclusive. The first and last names should be set by calling the constructor of the parent class. The Student constructor also sets the studentId to the next available positive integer. The first Student created should have a studentId of 1, the second will have an ID of 2, third of 3, etc.
int getLevel() - Returns the student's grade level.
String toString() - Returns a three line String with Student info formatted as follows:
Smith, Mary
Grade Level: 2
ID #: 1
Note: there are three spaces before "Grade Level: ..." and "ID #: ...".
HighSchoolStudent extends Student
Variables:
double gpa - Stores a grade point average between 0 and 5 inclusive
Methods:
HighSchoolStudent(String fName, String lName, int gLevel, double gpa) - The first and last names and the level should be set by calling the constructor of the parent class. The GPA should be between 0 and 5 inclusive, otherwise set to 0.
String toString() - Returns a four line String with HighSchoolStudent info formatted as follows:
Lee, Sarah
Grade Level: 9
ID #: 2
GPA: 3.7
Note: there are three spaces before "Grade Level: ...", "ID #: ..." and "GPA: ...".
Teacher extends Person
Variables:
String subject - A String representing the academic subject taught by the teacher.
Methods:
Teacher(String fName, String lName, String subject) - The first and last names should be set by calling the constructor of the parent class.
String toString() - Returns a two line String with Teacher info formatted as follows:
Dovi, Rebecca
Subject: Computer Science
Note: there are three spaces before "Subject: ...".
School
Variables:
ArrayList<Student> students - A list of students at the school.
ArrayList<Teacher> teachers - A list of teachers at the school.
Methods:
School(ArrayList<Student> students, ArrayList<Teacher> teachers) - A constructor that specifies teachers and students at a school.
String getGradeLevel(int level) - Returns a String listing all the schools's students that are at the specified grade level. Returns an empty String if the school has no students at the specified level. See the Sample Run below for the format of the returned String.
String toString() - Returns a multiline String listing the teachers and students at the school. The String is formatted as follows:
Faculty:
{listing of faculty, one on each line}
Student Body:
{listing of students, one on each line}
See the Sample Run below for an example.
Remember, all variables should have an access level of private and all required methods should have an access level of public. Wherever possible, the child class should use a call to the parent's toString and/or constructor methods.
Please download the runner class, student_runner_School.java and verify that the class output matches the sample run that follows. We will use a different runner to grade the program. Remember to change the runner to test different values to make sure your program fits the requirements.
Sample Run of student_runner_School.java:
printing person:
Doe, John
printing student:
Smithers, Sallie
Grade Level: 7
ID #: 1
printing highschoolstudent:
Smith, Bert
Grade Level: 11
ID #: 2
GPA: 3.67
printing school:
Faculty:
Lovelace, Ada
Subject: Mathematics
Einstein, Albert
Subject: Physics
Hopper, Grace
Subject: Computer Science
Turing, Alan
Subject: Mathematics
Curie, Marie
Subject: Chemistry
Madison, Dolly
Subject: Government
Angelou, Maya
Subject: English Composition
Student Body:
Finch, Jem
Grade Level: 11
ID #: 3
GPA: 3.4
Finch, Scout
Grade Level: 4
ID #: 4
Adams, Natalie
Grade Level: 11
ID #: 5
GPA: 2.4
Radley, Boo
Grade Level: 12
ID #: 6
GPA: 1.7
Finch, Atticus
Grade Level: 12
ID #: 7
GPA: 4.8
Benes, Elaine
Grade Level: 9
ID #: 8
Henry, Patrick
Grade Level: 11
ID #: 9
just seniors:
Radley, Boo
Grade Level: 12
ID #: 6
GPA: 1.7
Finch, Atticus
Grade Level: 12
ID #: 7
GPA: 4.8
1. Submit your Person class here.
Explanation / Answer
Main.java
//Header files
import java.io.IOException;
import java.util.ArrayList;
// main class
public class Main
{
public static void main(String arg[]) throws IOException
{
// creating object of Person class
Person p = new Person("John", "Doe");
System.out.println("printing person: ");
System.out.println(p);
// creating object of Student class
Student s = new Student("Sallie", "Smithers", 7);
System.out.println(" printing student: ");
System.out.println(s);
System.out.println(" printing highschoolstudent: ");
// creating object of HighSchoolStudent class
HighSchoolStudent h = new HighSchoolStudent("Bert", "Smith", 11, 3.67);
System.out.println(h);
ArrayList<Student> stu = new ArrayList<Student>();
// adding objects
stu.add(new HighSchoolStudent("Jem", "Finch", 11, 3.4));
stu.add(new Student("Scout", "Finch", 4));
stu.add(new HighSchoolStudent("Natalie", "Adams", 11, 2.4));
stu.add(new HighSchoolStudent("Boo", "Radley", 12, 1.7));
stu.add(new HighSchoolStudent("Atticus", "Finch", 12, 4.8));
stu.add(new Student("Elaine", "Benes", 9));
stu.add(new Student("Patrick", "Henry", 12));
ArrayList<Teacher> tea = new ArrayList<Teacher>();
tea.add(new Teacher("Ada", "Lovelace", "Mathematics"));
tea.add(new Teacher("Albert", "Einstein", "Physics"));
tea.add(new Teacher("Grace", "Hopper", "Computer Science"));
tea.add(new Teacher("Alan", "Turing", "Mathematics"));
tea.add(new Teacher("Marie", "Curie", "Chemistry"));
tea.add(new Teacher("Dolly", "Madison", "Government"));
tea.add(new Teacher("Maya", "Angelou", "English Composition"));
School sch = new School(stu, tea);
System.out.println(" printing school: ");
System.out.println(sch);
System.out.println(" just seniors: " + sch.getGradeLevel(11));
}
}
Student.java
//student class which extends Person class
public class Student extends Person {
// declare variables
int level;
static int count;
int studentId;
// constructor
public Student(String fName,String lName,int gLevel) {
super(fName,lName);
setLevel(gLevel);
count++;studentId = count;
}
public void setLevel(int gL){
if(gL>=0 && gL<=12) level = gL;
else level = 0;
}
public int getLevel() {
return level;
}
// display result
public String toString() {
return super.toString() +" "+" "+"Grade Level: " + level +" ID #: " + studentId;
}
}
Teacher.java
// Teacher class which extends Person
public class Teacher extends Person {
// declare variable
String subject;
// constructor
public Teacher(String fN, String lN,String subject) {
super(fN, lN);
setSubject(subject);
}
// getter
public String getSubject() {
return subject;
}
//setter
public void setSubject(String subject) {
this.subject = subject;
}
// display output
public String toString() {
return super.toString() +" Subject: "+subject;
}
}
School.java
import java.util.ArrayList;
// create class School
public class School {
// create array
ArrayList<Student> students = new ArrayList<Student>();
ArrayList<Teacher> teachers = new ArrayList<Teacher>();
// constructor
public School(ArrayList<Student> s, ArrayList<Teacher> t) {
students = s;
teachers = t;
}
// display output
public String toString2() {
return "Faculty: "+teachers.toString() .replace("[", "").replace("]", "").trim() +" "+"Student Body: "+students.toString().replace(",", "") .replace("[", "").replace("]", "").trim();
}
public String toString() {
String s = "";
s+="Faculty: ";
for(Teacher t: teachers) {
s+=t.toString()+" ";
}
s+="Student Body: ";
for(Student st: students) {
s+=st.toString()+" ";
}
return s;
}
public String getGradeLevel(int level) {
ArrayList<Student> gradestudents = new ArrayList<Student>();
for(int i=0;i<=students.size()-1;i++) {
if(students.get(i).getLevel()==level) {
gradestudents.add(students.get(i));
}
}
if (gradestudents.size() == 0)
return "";
String s = "";
for(Student st: gradestudents) {
s+=st.toString()+" ";
}
return s;
}
}
Person.java
public class Person {
// declare variables
String fname, lname;
// constructor
public Person() {
this("John","Doe");
}
// constructor with variables
public Person(String fN, String lN) {
setLname(lN);
setFname(fN);
}
//result
public String toString(){
return " "+lname+", "+fname;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
HighSchoolStudent.java
public class HighSchoolStudent extends Student{
double gpa;
public HighSchoolStudent(String fName, String lName, int gLevel, double gpa) {
super(fName,lName,gLevel);
setGPA(gpa);
}
public void setGPA(double g) {
if(g>=0.0 && g<=5) gpa = g;
else gpa=0.0;
}
public double getGpa() {
return gpa;
}
public String toString() {
return super.toString() + " GPA: " + gpa;
}
}
Sample output
Compile and Execute Java-8 Online
Help
Shut Down
Tutorials
View
Edit
Project
File
Dev Tools
Tutorials
Learn C++ Learn Java Learn Python Learn Ruby Learn Swift Programming Learn Scala
New Project
root
HelloWorld.java
HighSchoolStudent.java
Person.java
School.java
Student.java
Teacher.java
Terminal
printing person:
Doe, John
printing student:
Smithers, Sallie
Grade Level: 7
ID #: 1
printing highschoolstudent:
Smith, Bert
Grade Level: 11
ID #: 2
GPA: 3.67
printing school:
Faculty:
Lovelace, Ada
Subject: Mathematics
Einstein, Albert
Subject: Physics
Hopper, Grace
Subject: Computer Science
Turing, Alan
Subject: Mathematics
Curie, Marie
Subject: Chemistry
Madison, Dolly
Subject: Government
Angelou, Maya
Subject: English Composition
Student Body:
Finch, Jem
Grade Level: 11
ID #: 3
GPA: 3.4
Finch, Scout
Grade Level: 4
ID #: 4
Adams, Natalie
Grade Level: 11
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.