Question: //Lab 5: Inheritance import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.util.ArrayList; import org.junit.Before; import org.junit.Test; public class PersonTests { ArrayList list = new ArrayList ();

//Lab 5: Inheritance import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.util.ArrayList; import org.junit.Before; import org.junit.Test; public class PersonTests { ArrayList list = new ArrayList(); Person p1; Person p2; Person p3; Person e1; // UNCOMMENT THIS AFTER CREATING YOUR STUDENT CLASS // Student s1; @Before public void setUp() { p1 = new Person("Mai", "3156 Grove Rd, Somewhere"); p2 = new Person("Steve", "001 Terrace Road, Streetsville"); p3 = new Person("Jimmy", "12345 Six Street, Right here"); list.add(p1); list.add(p2); list.add(p3); e1 = new Employee("Don", "6562 Trask Way, Elsewhere", "Front Desk", 2110); list.add(e1); // UNCOMMENT THIS LINE AND FILL IN THE STUDENT CONSTRUCTOR AFTER CREATING YOUR STUDENT CLASS // s1 = new Student(); // list.add(s1); } @Test public void testPersonEmployee() { // This test makes sure that the toString() methods for the provided classes work as intended. assertEquals("toString", "{Person: name=Mai, homeAddress=3156 Grove Rd, Somewhere|", list.get(0).toString()); assertEquals("toString", "{Empl: n=Don, ha=6562 Trask Way, Elsewhere, wa=Front Desk, id=2110}", list.get(3).toString()); } @Test public void testPersonEmployeeStudent() { // TODO: Verify the personhood of every object in the ArrayList. (There should be 5 objects.) /* Hint: part of the equals() methods that you have written for HW2 and HW3 likely included * verifying that the parameter was of a certain Object type. You can use that same approach * in this test method, even if the object you pass is an instance_of a subclass of Person. */ // MAKE SURE TO COMMENT OUT THE LINE BELOW WHEN YOU'RE DONE fail("Have you implemented testPersonEmployeeStudent() yet? You're not done until you've commented out this line!"); } @Test public void testCollectionsSort() { // TODO: Use Collections.sort(/*List*/) to sort the list of Person objects. /* Hint: make sure you've implemented Comparable and Comparator, as those are important * for Collections.sort(List) to use. */ // MAKE SURE TO COMMENT OUT THE LINE BELOW WHEN YOU'RE DONE fail("Have you implemented testPersonEmployeeStudent() yet? You're not done until you've commented out this line!"); } // ACTIVITY 5 // @Test // public void testGetClass() { // System.out.println("p3.getName(): " + p3.getClass().getName()); // System.out.println("p3.getSimpleName(): " + p3.getClass().getSimpleName()); // System.out.println("p3.getCanonicalName(): " + p3.getClass().getCanonicalName()); // } } 
//Lab 5: Inheritance public class Person { protected String name; protected String homeAddress; public Person(String name, String address) { this.name = name; this.homeAddress = address; } // NOTE: Best practice is to include a default constructor in the superclass. public Person() { this.name = "?"; this.homeAddress = "?"; } public String getMailingAddress() { return homeAddress; } public String toString() { return "{Person: name=" + name + ", homeAddress=" + homeAddress + "|"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return homeAddress; } public void setAddress(String address) { this.homeAddress = address; } public static void main(String[] args) { Person p1 = new Person("p1", "addr1"); System.out.println(p1); } } 
//Lab 5: Inheritance public class Employee extends Person { private int employeeId; private String workAddress; public Employee(String n, String ha, String wa, int id) { super(n, ha); this.employeeId = id; this.workAddress = wa; } @Override public String getMailingAddress() { return workAddress; } public String toString() { return "{Empl: n=" + name + ", ha=" + homeAddress + ", wa=" + workAddress + ", id=" + employeeId + "}"; } public static void main(String[] args) { // For testing code. } } 

Activity #0: Download lab version of Person inheritance hierarchy program

Download Person.java, Employee.java, and PersonTests.java (Resources > Lab Files > Lab 5)

Create a Java project in Eclipse (e.g. Lab5)

Add those three files to your project's src folder. Try drag-and-drop but, if you get errors, try creating 3 new files in this project with the same name and copy-and-paste the contents into them

Right-click the project and select "Properties" > "Java Build Path" > "Libraries" tab > "Add Library" > "JUnit" > "JUnit 4" > "Finish" > "Okay"

If the above doesn't seem to be working well, you can also right-click on any of the error-causing import statements > "Fix project setup" > "OK"

Once this is done, the project should compile without errors and 1 of the 3 JUnit tests in PersonTests should pass.

Activity #1: Inheriting from Person

Create a class Student that is a subclass of Person. Every Student object will have a campusAddress (String) and a gpa value (double). The default constructor should initialize gpa to be 0.0 and the campusAddress to an empty String.

Back in the PersonTests class, uncomment the lines that deal with Student objects:

Run the JUnit tests now. Do they all pass? How would you change Student so that the existing JUnit will pass? Make that change in your code before moving on.

Activity #2: instanceof JUnit Test

At the end of the testPersonEmployeeStudent() in PersonTests.java, add JUnit code that uses instanceof to verify that every object in the arraylist is of type Person. Does instanceof work? Why or why not?

Activity #3: Person Comparable

Make the Person class implement Comparable by writing a compareTo() method. Make your implementation compare based on the name field. Make sure everything in PersonTest still runs as intended.

Is it okay if something is both Comparable and another class? Do some of the other objects have types other than Comparable and Person?

Activity #4: Person Comparator

We're going to write a Comparator class and use it to sort objects of type Person. But first, write a JUnit test that creates some Person objects with different names and addresses, puts them into an ArrayList, and then calls Collections.sort(). Utilize asserts to verify that the alphabetically-first name comes first in the sorted list (this incorporates what you did in Activity #3).

Next, create a class called CmpByAddress that implements the Comparator interface as described in lecture. It should compare two Person objects by the homeAddress field.

Make a new JUnit that also makes a list of Persons and sorts it, but this time use the CmpByAddress comparator as the 2nd parameter to Collections.sort(). Add asserts that verify that the alphabetically-first address comes first in the sorted list.

Activity #5: Object Reference

What if you really need to know the real, defined type of an object that is pointed to by an object-reference? Javas Object class has a few ways to print the name of an objects class as a string.

Un-comment the code under ACTIVITY 5 in PersonTests.java and make sure you understand how it works.

Activity #6: Built-In equals()

Eclipse comes with a built-in way to write an equals() method. Take one of your classes, go to the "Source" menu in Eclipse, and choose the command that creates a equals() method for you. Look at that code and make sure you and your partner understand it.

What is the benefit of this implementation? Think about how you could compare an Employee and a Student object. Can you use the Person.equals() method from Eclipse to do so?

Fields that contain Student objects

Lines that add Student objects to the list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!