Inheritance - Multiplayer Coding
Multiplayer Coding Team/Group:
Part A
- Design the UML for 2 new classes, incorporating the Person class (you can modify), that demonstrate:
- One class will demonstrate an “is-a” relationship
- The other class will demonstrate a “has-a” relationship
Part B
- Convert your UML to Java code
Part C
- Create a package called, “myPackage.superpackage”, for your Person class and the new classes you designed
Person.java
package myPackage.superpackage;
public class Person{
private String name;
protected int age;
private final String DEFAULT_NAME = "John";
private final int DEFAULT_AGE = 18;
private HealthCard hc;
public Person(){
name = DEFAULT_NAME;
age = DEFAULT_AGE;
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
public Person(String name, int age, long healthCardNumber, String versionCode){
this.name = name;
this.age = age;
hc = new HealthCard(healthCardNumber, versionCode);
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void setName(String newName){
name = newName;
}
public void setAge(int newAge){
age = newAge;
}
public HealthCard getHC(){
return hc;
}
public String toString(){
return "My name is " + name + " my age is " + age;
}
}
HealthCard.java
package myPackage.superpackage;
public class HealthCard{
private long healthCardNumber;
private String versionCode;
private final String DEFAULT_VERSION = "AB";
private final long DEFAULT_NUMBER = 1234567890l;
public HealthCard(){
this.healthCardNumber = DEFAULT_NUMBER;
this.versionCode = DEFAULT_VERSION;
}
public HealthCard(long healthCardNumber, String versionCode){
this.healthCardNumber = healthCardNumber;
this.versionCode = versionCode;
}
public long getHealthCardNumber(){
return healthCardNumber;
}
public String getVersionCode(){
return versionCode;
}
public void setHealthCardNumber(long healthCardNumber){
this.healthCardNumber = healthCardNumber;
}
public void setVersionCode(String versionCode){
this.versionCode = versionCode;
}
public String toString(){
return healthCardNumber + versionCode;
}
}
Professor.java
package myPackage.superpackage;
public class Professor extends Person{
private int professorID;
public Professor(String name, int professorID, int age){
super(name, age);
this.professorID = professorID;
}
public int getProfessorID(){
return professorID;
}
public void setProfessorID(int professorID){
this.professorID = professorID;
}
@Override
public String toString(){
String retVal = super.toString();
return retVal + " my professor ID is " + professorID;
}
}
Main.java
/*
Team/Group:
Part A
Design the UML for 2 new classes, incorporating the Person class (you can modify), that demonstrate:
One class will demonstrate an “is-a” relationship
The other class will demonstrate a “has-a” relationship
Part B
Convert your UML to Java code
Part C
Create a package called, “myPackage.superpackage”, for your Person class and the new classes you designed
*/
import myPackage.superpackage.HealthCard;
import myPackage.superpackage.Person;
import myPackage.superpackage.Professor;
class Main {
public static void main(String[] args) {
//"is-a" relationship
Professor t1 = new Professor("Yves", 10532, 33);
System.out.println(t1.getName());
System.out.println(t1.getAge());
System.out.println(t1.getProfessorID());
System.out.println(t1);
//"has-a" relationship
Person t2 = new Person("Zhang", 65, 7237567890l, "YH");
System.out.println(t2.getName());
System.out.println(t2.getHC());
System.out.println(t2.getHC().getHealthCardNumber());
System.out.println(t2.getHC().getVersionCode());
}
}
Independent:
Post the UML and the code on your Learning Blog with a DETAILED EXPLANATION of your code, try to use all the programming TERMINOLOGY from this topic
The first line of my Person.java file is placing the class into the package “myPackage.superpackage”. After this, I created the class and set all of my variables. The variables were created based on the specifications set in the UML. The hc variable is related to the HealthCard class. A Person “has-a” HealthCard. I then created the constructors. The first constructor took in no parameters and set both the name and age to the default values. The second constructor takes in a name and age and sets the variables to those values. The final constructor takes in a name, age, health card number, and version code. The name and age are set like they were in the second constructor. The hc variable is initialized by calling the HealthCard constructor. After the constructor are the accessors for the name and age. Then are the mutators for the same variables. There is also an accessor for the hc variable. Finally, there is a toString method that returns a String that states the name and age.
As with the Person.java file, the first line of the HealthCard.java is placing the class into a package. I created the class and the variables that were required. I then created a constructor without any parameters. The health card number and version code are both set to their default values. The next constructor takes in two parameters. The two variables are set to the value of those parameters. After this, there are two accessors for the health card number and version code. There are then mutators to change the values of the variables. Finally, there is a toString method that returns the entire health card by combining both the health card number and the version code.
The Professor.java follows in the footsteps of the other two files and is added to the “myPackage.superpackage” as well. This class is a child of the Person class and has a “is-a” relationship. I then created the variable that was set by the UML. There is only one constructor in this class, it takes in three parameters. The constructor uses “super” to call the constructor from the parent class. It then sets the professor ID parameter to the one set in the parameter. After the constructor is an accessor and mutator for the professor ID variable. This time, the toString method uses “@Override” as I want the output to be different from the parent. I use “super” to get the toString from the parent class and set it to a variable. I then return that variable and the addition of the professor ID.
The Main.java file tests the “is-a” and “has-a” relationships. It creates new objects and then prints different values from the object to demonstrate the relationship.