In: Computer Science
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:
firstName lastName
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:
Mary Smith
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:
Sarah Lee
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:
Rebecca Dovi
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:
John Doe
printing student:
Sallie Smithers
Grade Level: 7
ID: 1
printing highschoolstudent:
Bert Smith
Grade Level: 11
ID: 2
GPA: 3.67
printing school:
Faculty:
Ada Lovelace
Subject: Mathematics
Albert Einstein
Subject: Physics
Grace Hopper
Subject: Computer Science
Alan Turing
Subject: Mathematics
Marie Curie
Subject: Chemistry
Dolly Madison
Subject: Government
Maya Angelou
Subject: English Composition
Student Body:
Jem Finch
Grade Level: 11
ID: 3
GPA: 3.4
Scout Finch
Grade Level: 4
ID: 4
Natalie Adams
Grade Level: 11
ID: 5
GPA: 2.4
Boo Radley
Grade Level: 12
ID: 6
GPA: 1.7
Atticus Finch
Grade Level: 12
ID: 7
GPA: 4.8
Elaine Benes
Grade Level: 9
ID: 8
Patrick Henry
Grade Level: 11
ID: 9
just seniors:
Boo Radley
Grade Level: 12
ID: 6
GPA: 1.7
Atticus Finch
Grade Level: 12
ID: 7
GPA: 4.8
1. Submit your Person class here.
NOTE: You MUST use the class name "Person" for this submission.
student_runner_school.java
import java.io.IOException;
import java.util.ArrayList;
public class student_runner_School
{
public static void main(String arg[]) throws IOException
{
Person p = new Person("John", "Doe");
System.out.println("printing person:\n");
System.out.println(p);
Student s = new Student("Sallie", "Smithers", 7);
System.out.println("\n\nprinting student:\n");
System.out.println(s);
System.out.println("\n\nprinting highschoolstudent:\n");
HighSchoolStudent h = new HighSchoolStudent("Bert", "Smith", 11,
3.67);
System.out.println(h);
ArrayList<Student> stu = new
ArrayList<Student>();
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", 11));
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("\n\nprinting school: \n");
System.out.println(sch);
System.out.println("\n\njust seniors: \n" + sch.getGradeLevel(12));
}
}
Person.java
public class Person {
private String firstName;
private String lastName;
//Constructor that takes in String parameters
representing the first and last names
public Person(String fName, String lName)
{
firstName = fName;
lastName = lName;
}
//Returns a string in the following format:
//lastName, firstName
public String toString()
{
return (lastName + ", " +
firstName);
}
}
HighSchoolStudent.java
public class HighSchoolStudent extends Student{
private 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.
public HighSchoolStudent(String fName, String lName,
int gLevel, double inputGpa)
{
super(fName, lName, gLevel);
if(inputGpa >= 0 &&
inputGpa <= 5)
gpa =
inputGpa;
else
gpa = 0;
}
// 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: ...".
public String toString()
{
String whole =
super.toString();
whole = whole + "\n " +
"GPA: " + gpa;
return whole;
}
}
Teacher.java
public class Teacher extends Person{
// A String representing the academic subject taught
by the teacher.
private String subject;
// The first and last names should be set by calling
the constructor of the parent class.
public Teacher(String fName, String lName, String
subject1)
{
super(fName, lName);
subject = subject1;
}
// Returns a two line String with Teacher info
formatted as follows:
// Dovi, Rebecca
// Subject: Computer Science
// Note: there are three spaces before "Subject:
...".
public String toString()
{
String whole =
super.toString();
whole = whole + "\n " +
"Subject: " + subject;
return whole;
}
}
School.java
import java.util.ArrayList;
public class School {
// A list of students at the school.
private ArrayList<Student> students;
// A list of teachers at the school.
private ArrayList<Teacher> teachers;
// A constructor that specifies teachers and students
at a school.
public School(ArrayList<Student> students1,
ArrayList<Teacher> teachers1)
{
students = students1;
teachers = teachers1;
}
// 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.
public String getGradeLevel(int level)
{
String whole = "";
for(Student student1:
students)
{
if(student1.getLevel() == level)
whole = whole + student1 + "\n";
}
return whole;
}
// Returns a multi-line 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}
public String toString()
{
String wholeString = "";
String wholeStudents = "";
for(Student student1:
students)
{
wholeStudents =
wholeStudents + student1 + "\n";
}
String wholeTeachers = "";
for(Teacher teacher1:
teachers)
{
wholeTeachers =
wholeTeachers + teacher1 + "\n";
}
wholeString = "Faculty:" + "\n" +
wholeTeachers + "\n\n";
wholeString = wholeString +
"Student Body:" + "\n" + wholeStudents;
return wholeString;
}
}
Student.java
public class Student extends Person{
private static int studentId = 1;
private int level;
private int id;
// 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.
public Student(String fName, String lName, int
gLevel)
{
super(fName, lName);
if(gLevel >= 0 && gLevel
<= 12)
level =
gLevel;
else
level = 0;
id = studentId;
studentId++;
}
// Returns the student's grade level.
public int getLevel()
{
return level;
}
// 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 #: ...".
public String toString()
{
String whole =
super.toString();
whole = whole + "\n " +
"Grade Level: " + level;
whole = whole + "\n " +
"ID #: " + id;
return whole;
}
}