Main topics: Composition - Aggregation Inheritance Exercise This week we will be
ID: 3728789 • Letter: M
Question
Main topics:
Composition - Aggregation
Inheritance
Exercise
This week we will be practicing writing an extended class, and a driver class for a numerous class project.
Getting Started
To start this exercise, you should:
1. Open eclipse and start a new Java project named Lab06
2. Add a Class (named Person) to this project, and copy the contents of the Person.java file provided into it.
3. Add a Class (named Student) to this project, and copy the contents of the Student.java file provided into it.
4. Add a Class (named Principle) to this project, the contents of which you will be writting from scratch.
5. Add a Class (named School) to this project, and copy the contents of the School.java file provided into it.
6. Add a Class (named SchoolDriver) to this project, the contents of which you will be writting from scratch.
Requirements
Person.java
A very simple class which models some of the functionality of a person. This class is complete and must not be modified.
Student.java
A very simple class which extends the Person class to also model some of the functionality of a student. This class is complete and must not be modified.
Principle.java
A very simple class which extends the Person class to also model some of the functionality of a principle. The extra data member this time is a salary. This class you must write, such that:
1. You write the standard default and specifying constructors, which utilize the base Person classes’ default and specifying constructors respectively.
2. You write the standard accessor and mutator.
3. You override Person’s toString method.
School.java
A very simple class which models some of the functionality of a school. Pay close attention as to how the sole principle and the various students are incorporated - which is composition? - which is aggregation? This class is complete and must not be modified.
SchoolDriver.java
A simple driver class to test all of the above classes. This class you must write, such that:
1. You construct two different schools.
2. You construct and add a different student to each school.
3. You construct one additional student, and add them to both schools.
4. You produce output that is at least functionally equivalent to the following:
School’s Name: Somewhere East
Principle’s Name: John Hawlks - $35000.0 Yearly
Jimmy - GPA - 3.5
Jenny - GPA - 3.8
School’s Name: Somewhere West
Principle’s Name: Amy Hope - $48000.0 Yearly
Jenny - GPA - 3.8
Billy - GPA - 3.8
____________________________________________
Person class
package Inheritance;
public class Person
{
private String name;
public Person()
{
this("");
}
public Person(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public String toString()
{
return getName();
}
}
___________________________________________
School class
import java.util.ArrayList;
public class School
{
private String name;
private Principle principle;
private ArrayList<Student> students;
public School()
{
this("", "", 0.0);
}
public School(String sName, String pName, double salary)
{
this.setName(sName);
this.principle = new Principle(pName, salary);
this.students = new ArrayList<Student>();
}
private void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void addStudent(Student student)
{
this.students.add(student);
}
public void schoolAudit()
{
System.out.println("School's Name: " + this.getName());
System.out.println(" Principle's Name: " + this.principle.toString());
for ( Student student : students )
System.out.println(" " + student.toString());
}
}
__________________________________________
Student class
package Inheritance;
public class Student extends Person
{
private double gpa;
public Student()
{
super();
this.gpa = 4.0;
}
public Student(String name, double gpa)
{
super(name);
this.gpa = gpa;
}
public double getGpa()
{
return this.gpa;
}
public String toString()
{
return super.toString() + " : " + this.getGpa();
}
}
Explanation / Answer
Person.java
public class Person
{
//Declaring instance variables
private String name;
//Zero argumented constructor
public Person()
{
this("");
}
//Parameterized constructor
public Person(String name)
{
this.name = name;
}
//getters and setters
public String getName()
{
return this.name;
}
public String toString()
{
return getName();
}
}
______________
Principle.java
public class Principle extends Person {
// Declaring instance variables
private double salary;
// Parameterized constructor
public Principle(String name, double salary) {
super(name);
this.salary = salary;
}
// getters and setters
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " - $" + salary + " Yearly";
}
}
__________________
School.java
import java.util.ArrayList;
public class School
{
//Declaring instance variables
private String name;
private Principle principle;
private ArrayList<Student> students;
//Zero argumented constructor
public School()
{
this("", "", 0.0);
}
//Parameterized constructor
public School(String sName, String pName, double salary)
{
this.setName(sName);
this.principle = new Principle(pName, salary);
this.students = new ArrayList<Student>();
}
//getters and setters
private void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
//This method will add the student to the ArrayList
public void addStudent(Student student)
{
this.students.add(student);
}
public void schoolAudit()
{
System.out.println("School's Name: " + this.getName());
System.out.println(" Principle's Name: " + this.principle.toString());
for ( Student student : students )
System.out.println(" " + student.toString());
}
}
__________________
Student.java
public class Student extends Person
{
//Declaring instance variables
private double gpa;
//Zero argumented constructor
public Student()
{
super();
this.gpa = 4.0;
}
//Parameterized constructor
public Student(String name, double gpa)
{
super(name);
this.gpa = gpa;
}
// getters and setters
public double getGpa()
{
return this.gpa;
}
//toString method is used to display the contents of an object inside it
public String toString()
{
return super.toString() + " - GPA - " + this.getGpa();
}
}
__________________
SchoolDriver.java
public class SchoolDriver {
public static void main(String[] args) {
//Creating an Instance of School class
School school1=new School("Somewhere East","John Hawlks", 35000.0);
//adding students to School
school1.addStudent(new Student("Jimmy",3.5));
school1.addStudent(new Student("Jenny",3.8));
//Creating an Instance of School class
School school2=new School("Somewhere West","Amy Hope", 48000.0);
//adding students to School
school2.addStudent(new Student("Jenny",3.8));
school2.addStudent(new Student("Billy",3.8));
school1.schoolAudit();
school2.schoolAudit();
}
}
______________________
Output:
School's Name: Somewhere East
Principle's Name: John Hawlks - $35000.0 Yearly
Jimmy - GPA - 3.5
Jenny - GPA - 3.8
School's Name: Somewhere West
Principle's Name: Amy Hope - $48000.0 Yearly
Jenny - GPA - 3.8
Billy - GPA - 3.8
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.