Assume that you have three classes called Student, Address, Name Student class h
ID: 3606635 • Letter: A
Question
Assume that you have three classes called Student, Address, Name
Student class has the following member variables: int starid, String department
Address class has the following member variables: String street, string city, and int zip
Name class has the following member variable: string fName, char mName, and String lName
Write the java code for:
1.1 each of the above classes
1.2 Apply the concept of aggregation and write code for class relationship among the above classes
1.3 Add a sub class called GraduateStudent inheriting some features from Student class, write code for a class relationship including the newly added subclass.
Explanation / Answer
class Address
{
private String street;
private String city;
private int zip;
public Address(String street,String city,int zip) //argument constructor
{
this.street = street;
this.city = city;
this.zip = zip;
}
public String toString()
{
return " Street : "+ street + " city : "+ city + " Zip : "+zip;
}
}
class Name
{
private String fName;
private char mName;
private String lName;
public Name(String fName,char mName,String lName)
{
this.fName = fName;
this.mName = mName;
this.lName = lName;
}
public String toString()
{
return " "+fName +" "+mName+" "+lName;
}
}
class Student
{
private int stuid;
private String department;
private Address addr; //aggregation
private Name nm; //aggregation
public Student(int stuid,String department,Address addr,Name nm)
{
this.stuid = stuid;
this.department = department;
this.addr = addr;
this.nm = nm;
}
public String toString()
{
return " Student ID : "+stuid +" Name : "+nm +" Department : "+ department + " Address "+addr ;
}
}
class GraduateStudent extends Student //inheritance
{
private String subject;
public GraduateStudent(int stuid,String department,Address addr,Name nm,String subject)
{
super(stuid,department,addr,nm);
this.subject = subject;
}
public String toString()
{
return " Graduate Student : "+ super.toString() + " Subject : "+subject;
}
}
class Test
{
public static void main (String[] args)
{
Name nm = new Name("Andrew",'D',"Johnson");
Address addr = new Address("3422 Down street","Hillside,NJ",7205);
GraduateStudent gs = new GraduateStudent(1008,"Computer Science",addr,nm,"Java");
System.out.println(gs);
}
}
Output: