Starting from:
$35

$29

Assignment #1 Solution

This is a lot about writing classes with private fields, constructors, getter and setter methods, plus a couple of other methods and a driver.




All the classes need to be in <bpackage assignment01</b. All the classes - Person, HighSchool, HighSchoolStudent, University, UniversityStudent, Employee, Company and Driver - are public classes, each one in its own file (Person.java, HighSchool.java, HighSchoolStudent.java, University.java, UniversityStudent.java, Employee.java, Company.java, and Driver.java).




 

<bPerson</b has private fields <bString name</b, <bLocalDate birthdate</b (you need to import <bjava.time.LocalDate</b for your class), and <bObject[] history = new Object[3]</b. This is an array, which we will be covering pretty soon in class—for now just follow all the instructions about working with arrays.




<bHighSchool</b has a private field <bString name</b (this is the high school name)

<bUniversity</b has private fields <bString name</b (this is the university name) and <bString city</b (the city where the university is located).

<bCompany</b has private fields <bString name</b (this is the company name) and <bString city</b (the city of the headquarters of the company).




<bHighSchoolStudent</b has private fields <bHighSchool highSchool</b and <bPerson person</b.

<bUniversityStudent</b has private fields <bUniversity university</b and <bPerson person</b.

<bEmployee</b has private fields <bCompany company</b, <bdouble salary</b, and <bPerson person</b.




Provide constructors to set all the fields of all the classes and provide *getter* methods for all the fields. The only tricky constructor is <bPerson</b, here it is:




```java

public Person(String aName, int day, int month, int year) {

name = aName;

birthdate = LocalDate.of(year, month, day);

}

```




A *getter* method would be (the name of a *getter* starts with “get” and then has the name of the field but with the first letter changed to upper cases):




```java

LocalDate getBirthdate( ) {

return birthdate;

}

```




<bEmployee</b needs a *setter* method to change <bsalary</b. It is a <bvoid</b method called <bsetSalary</b that has a <bdouble</b parameter, which is the new value of <bsalary</b.




In the constructor of <bHighSchoolStudent</b, you need to include the instruction

person.getHistory()[0] = this;

 

In the constructor of <bUniversityStudent</b, you need to include the instruction

person.getHistory()[1] = this;




In the constructor of <bEmployee</b, you need to include the instruction

person.getHistory()[2] = this;




In <bHighSchoolStudent</b, provide a method <bpublic UniversityStudent goToUniversity(University univ)</b, which returns a new <bUniversityStudent</b object, with <buniv</b as its <buniversity</b field and the same <bperson</b field as this <bHighSchoolStudent</b’s <bperson</b field.




In <bUniversityStudent</b, provide a method <bpublic Employee getAJob(Company comp, double startingSalary)</b, which returns a new <bEmployee</b object, with <bcomp</b as its <bCompany</b field, <bstartingSalary</b as its <bsalary</b and the same <bperson</b field as this <bUniversityStudent</b’s <bperson</b field.










In the class <bPerson</b, complete the method <bprintHistory</b (since there are things that we have not got to, part of the code is provided):







```java

public void printHistory() {

System.out.println("History of " + name);

if(history[0] != null) {

// in the next line we "cast" history[0] to a HighSchoolStudent object

HighSchoolStudent hss = (HighSchoolStudent)history[0];

// in the next line the char '\t' provides a tab indent

System.out.println("\tHigh school: " + hss.getHighschool().getName());

}

if(history[1] != null) {

//Use the similar approach to "cast" history[1] to a UniversityStudent object.

//From that object, the getUniversity method gives you the university and from that

//the getter methods will give the name and city of the university.

//In the System.out.println, start with "\tUniversity: " + followed by the name of the university

//next comes + " in " + followed by the city where the university is located.

//Example: University: Duke in Durham, NC

}

if(history[2] != null) {

Employee e = (Employee)history[2];

//we have not explained the format or printf methods that were in Lab 1, so

//this is provided:

System.out.format("\tJob at %s in %s, with a salary of %.2f\n",

e.getCompany().getName(), e.getCompany().getCity(), e.getSalary());

}

}

```




Create a class <bDriver</b with a main method--this must be <bpublic static void main(String[] args)</b.










[yes this is tedious but it has to be done--I found I had made a couple of mistakes in typing up the assignment code]










(You are permitted to use arrays for the following different groups but it is not required.)

Make FOUR <bPerson</b objects with different <bname</bs.




Make THREE <bHighSchool</b objects, with different <bname</bs (by the way, did you hear that a local high school Maine-Endwell won the Little League World championship last week by beating Korea?).




Make THREE <bHighSchoolStudent</b objects using the 3 different <bHighSchool</b objects and 3 of the different <bPerson</b objects.




Make TWO <bUniversity</b objects, for example <bnew University("Duke", "Durham, NC")</b could be one, and use the <bgoToUniversity</b method of TWO of the <bHighSchoolStudent</b objects to make two <bUniversityStudent</b objects.




Make one <bCompany</b object and use it with the <bgetAJob</b of one of the <bUniversityStudent</b objects to make an <bEmployee</b object (be optimistic and provide a 6-figure salary).




Call <bprintHistory()</b on each of the four <bPerson</b objects. Separate the <bprintHistory</b calls by

<bSystem.out.println("---------------------");</b




to separate the outputs. The different <bPerson</b objects will have 1, 2, 3, and 4 lines of output.

More products