Demo - Aggregation Example
package ClassDemo;
/*
* Class handles an Address
*/
public class Address {
private String streetAddress;
private String city;
private String state;
private String zip;
//Getters and Setters
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
/** Constructor
* @param streetAddress
* @param city
* @param state
* @param zip
*/
public Address(String streetAddress, String city, String state, String zip) {
super();
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
this.zip = zip;
}
/**Default Constructor
*/
public Address() {
this.streetAddress = "Unknown Street Address";
this.city = "Unknown City";
this.state = "Unknown State";
this.zip = "Unknown Zip";
}
/**Copy Address Constructor to handle the reference (value passed by reference)
*/
public Address(Address pAdd)
{
this.streetAddress = pAdd.streetAddress;
this.city = pAdd.city;
this.state = pAdd.state;
this.zip = pAdd.zip;
}
/*
* Functions/Methods
*/
public String PrintAddress()
{
String myReturn = "";
myReturn += "\t" + getStreetAddress() + "\n";
myReturn += "\t" + getCity() + ", " + getState() + ", " + getZip();
return myReturn;
}
}
The provided Java code is a class definition for an Address
class in the package ClassDemo
. This class is used to model a real-world address, which typically includes attributes like street address, city, state, and zip code.
Here's a breakdown of the class and its components:
Fields: There are four private fields (
streetAddress
,city
,state
,zip
) representing the parts of an address.Getters and Setters: There are getter and setter methods for each field. These methods are used to retrieve (get) and change (set) the value of the fields.
Constructor with parameters: This is a constructor that takes four parameters. It initializes a new
Address
object with the provided street address, city, state, and zip code. Thesuper()
call is unnecessary in this case becauseAddress
does not explicitly extend any other class other thanObject
, which is implicitly extended by all classes in Java.Default Constructor: This is a no-argument constructor that initializes a new
Address
object with the default values (all set to "Unknown").Copy Constructor: This constructor takes an
Address
object as a parameter. It initializes a newAddress
object with the same values as the provided object. This is used to create a copy of anAddress
object.PrintAddress Method: This method returns a formatted string representation of the
Address
object. It concatenates the values of thestreetAddress
,city
,state
, andzip
fields and returns this string. This can be useful for displaying the address information in a readable format.
In summary, this Address
class provides a way to model addresses within a Java application, including functionality to create new addresses, modify them, and print them in a standard format.
Person Class
package ClassDemo;
/**
* Class contains First name, Last name, and age
* Class uses aggregation to also contain Work/Home address
*/
public class Person {
//Attributes/fields
private String firstName;
private String lastName;
private int personAge;
private Address HomeAddress; //this is the Address Class
private Address WorkAddress; //Address Class
/**************************************
* Constructors
**************************************/
//Constructors (Default)
public Person()
{
firstName = "None";
lastName = "None";
personAge = 0;
HomeAddress = new Address();
WorkAddress = new Address();
}
//Constructors (Populates data with arguments)
public Person(String pFirst, String pLast, int pAge, Address pWork, Address pHome)
{
setFirstName(pFirst);
setLastName(pLast);
setPersonAge(pAge);
//to ensure we have the values and not just a refernce to the Address objects passed
this.HomeAddress = new Address(pHome);
this.WorkAddress = new Address(pWork);
}
/**************************************
* Getters/Setters
**************************************/
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getPersonAge() {
return personAge;
}
public void setPersonAge(int personAge) {
this.personAge = personAge;
}
public Address getWorkAddress() {
return WorkAddress;
}
public void setWorkAddress(Address pWork) {
this.WorkAddress = new Address(pWork);
}
public Address getHomeAddress() {
return HomeAddress;
}
public void setHomeAddress(Address pHome) {
this.HomeAddress = new Address(pHome);
}
/**************************************
* Methods
**************************************/
public String PrintData()
{
String myReturn = "";
myReturn += "Name: " + getFirstName() + " " + getLastName();
myReturn += " Age: " + getPersonAge();
//Home address
myReturn += "\nHome Address:\n";
myReturn += HomeAddress.PrintAddress();
//Work address ";
myReturn += "\nWork Address:\n";
myReturn += WorkAddress.PrintAddress();
return myReturn;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", personAge=" + personAge + "]";
}
} // END Person Class
The provided Java code defines a Person
class within the ClassDemo
package. This class is designed to model a real-world person, capturing attributes such as first name, last name, age, home address, and work address.
Here's a detailed breakdown of the class and its components:
Fields: The class has five private fields:
firstName
,lastName
,personAge
,HomeAddress
, andWorkAddress
.firstName
andlastName
are Strings representing the person's name.personAge
is an integer for the person's age.HomeAddress
andWorkAddress
are objects of theAddress
class representing the person's home and work addresses.Default Constructor: The default constructor initializes a new
Person
object with default values. It sets thefirstName
andlastName
to "None", thepersonAge
to 0, and initializesHomeAddress
andWorkAddress
with the defaultAddress
constructor.Constructor with parameters: This constructor initializes a new
Person
object using the provided parameters. It uses the setters for the fieldsfirstName
,lastName
, andpersonAge
, and the copy constructor ofAddress
class to create newAddress
objects forHomeAddress
andWorkAddress
. This ensures that the address objects stored within thePerson
object are distinct from the ones passed in, which helps to maintain data integrity and avoid issues with shared references.Getters and Setters: There are getter and setter methods for each field. These methods are used to retrieve (get) and change (set) the values of the fields.
PrintData Method: This method returns a string representation of the
Person
object. It concatenates the values of thefirstName
,lastName
,personAge
fields, and the string representations of theHomeAddress
andWorkAddress
objects. The addresses are fetched using thePrintAddress()
method from theAddress
class.toString Method: This method is overridden from the
Object
class. It returns a string representation of thePerson
object, including thefirstName
,lastName
, andpersonAge
fields. The addresses are not included in this representation.
Overall, the Person
class models a person with their name, age, and address details (both home and work). The class is designed to ensure that each Person
object maintains its own distinct Address
objects, helping to prevent errors that could arise from mutable shared state.
package ClassDemo;
public class PersonDemo {
public static void main(String[] args) {
Person myPerson = new Person();
//create 2 addresses
Address myWorkAddress = new Address("1223 Fredricksburg Ave", "San Antonio", "Texas", "78451");
Address myHomeAddress = new Address("45 2nd Street", "San Antonio", "Texas", "74123");
//create a person and populate the addresses
myPerson = new Person("Kevin", "Roark", 39, myWorkAddress, myHomeAddress);
//print data
System.out.println(myPerson.PrintData() );
}
}
This provided Java code is part of the ClassDemo
package and defines a PersonDemo
class with a main method. The purpose of this class is to demonstrate the use of the Person
and Address
classes.
Here's a breakdown of what's happening in the main
method:
A
Person
objectmyPerson
is created using the default constructor, which sets the fields to their default values as defined in thePerson
class.Two
Address
objects,myWorkAddress
andmyHomeAddress
, are created. The constructors are called with string arguments that set the street address, city, state, and zip code for each address.Then, the
myPerson
object is assigned a newPerson
object. The constructor is called with string arguments for the first and last names, an integer for the age, and the previously createdmyWorkAddress
andmyHomeAddress
objects. Because thePerson
constructor creates newAddress
objects from the ones provided,myPerson
will have its own distinct address objects that match the ones created here.Finally, the
PrintData
method ofmyPerson
is called. This method returns a string that contains the first name, last name, age, and home and work address of thePerson
object. This string is then printed to the console.
In summary, this class demonstrates creating a Person
object with specific data, and then printing that data to the console. The Person
object is composed of a name, age, and two Address
objects, all of which are defined in the code.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark