In: Computer Science
Modify the attached files to do the following in java:
1) Add all necessary Getters and Setters to the Class file.
2) Add code to compare two instances of the class to see which one comes before the other one based on the zipcode.
//Address1.java
public class Address
{
// attributes
private String street, aptNum, city, state;
private int zip;
// constructors
public Address(String street, String aptNum, String city, String state, int zip)
{
this.street = street;
this.aptNum = aptNum;
this.city = city;
this.state = state;
this.zip = zip;
}
public Address(String street, String city, String state, int zip)
{
this(street, "", city, state, zip);
}
// write the getters and setters for this on your own! :)
/**
Determines if given Address object comes before calling Address
object, based on the zip code
@param other Address object to compare to
@return whether the given object comes before the calling object
*/
public boolean comesBefore(Address other)
{
/*if(other.zip < this.zip)
{
return true;
}
else{
return false;
}*/
return(other.zip < this.zip);
}//End comesBefore
// define and return the string representation of an address
public String toString()
{
String str = street + " "+ aptNum + "\n"+ city + ", "+ state + " "+ zip;
return str;
// alternatively:
// return street + " " + aptNum + "\n" + city + ", " + state + " " + zip;
}//EndOf toString
public boolean equals(Address other)
{
// check if the street, aptNum, city, state, and zip are the same
if(this.street.equals(other.street) && this.aptNum.equals(other.aptNum) &&
this.city.equals(other.city) && this.state.equals(other.state) &&
this.zip == other.zip)
{
return true;
}
else{
return false;
}
/*return (this.street.equals(other.street) && this.aptNum.equals(other.aptNum) &&
this.city.equals(other.city) && this.state.equals(other.state) &&
this.zip == other.zip);*/
}//Endof equals
}//EndOf Class Address
//AddressDriver1
public class AddressDriver
{
public static void main(String[] args)
{
Address a = new Address("2000 Clayton State Blvd", "Morrow", "GA", 30303);
Address b = new Address("2000 Clayton State Blvd", "227", "Morrow", "GA", 30260);
// toString method stuff
System.out.println(a);
System.out.println(b);
String words = "The address is: \n"+ a;
System.out.println(words);
// equals method stuff
Address c = new Address("2000 Clayton State Blvd", "Morrow", "GA", 30303);
if(a.equals(c))
{
System.out.println("Those addresses are the same.");
}
else{
System.out.println("Those addresses are NOT the same.");
}
}
}
CODE
public class Address {
// attributes
private String street, aptNum, city, state;
private int zip;
// constructors
public Address(String street, String aptNum, String city, String state, int zip)
{
this.street = street;
this.aptNum = aptNum;
this.city = city;
this.state = state;
this.zip = zip;
}
public Address(String street, String city, String state, int zip)
{
this(street, "", city, state, zip);
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getAptNum() {
return aptNum;
}
public void setAptNum(String aptNum) {
this.aptNum = aptNum;
}
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 int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
// write the getters and setters for this on your own! :)
/**
Determines if given Address object comes before calling Address
object, based on the zip code
@param other Address object to compare to
@return whether the given object comes before the calling object
*/
public boolean comesBefore(Address other) {
return(other.zip < this.zip);
}//End comesBefore
// define and return the string representation of an address
public String toString()
{
String str = street + " "+ aptNum + "\n"+ city + ", "+ state + " "+ zip;
return str;
// alternatively:
// return street + " " + aptNum + "\n" + city + ", " + state + " " + zip;
}//EndOf toString
public boolean equals(Address other)
{
// check if the street, aptNum, city, state, and zip are the same
if(this.street.equals(other.street) && this.aptNum.equals(other.aptNum) &&
this.city.equals(other.city) && this.state.equals(other.state) &&
this.zip == other.zip)
{
return true;
}
else{
return false;
}
/*return (this.street.equals(other.street) && this.aptNum.equals(other.aptNum) &&
this.city.equals(other.city) && this.state.equals(other.state) &&
this.zip == other.zip);*/
}//Endof equals
}//EndOf Class Address
//AddressDriver1