Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class named Address.java that represents an address with the following f

ID: 3842626 • Letter: W

Question

Write a class named Address.java that represents an address with the
following fields and methods with suitable access modifiers:
Two string fields for street and city, an integer field for zip code.
The Address class has the following methods:
1. Address(String street, String city, int zip): assigns the parameters to
the class data fields.
2. Address( Address D) : a copy constructor.
3. getStreet() : returns the street value
4. getCity(): returns the city value.
5. getZip(): returns the zip value.
6. toString(): returns a string of the Address object in the following form:
street. City, zip
example:
Wadeljoz. Jerusalem, 1234
7. isSameCity(Address a): returns true if this Address object and a object
has same city.
B)Write another class named Person.java that has the following:
1. A string field name.
2. An Address field add.
Class Person has the following methods:
1. Person(String name, Address add) : assigns the parameters to the class
data fields.
2. Person(String name, String street, String city, int zip): assigns the
parameters to the class data fields.
3. getName(): return the name of person.
4. getAddress(): returns the address of person.
5. setAddress(Address add): assigns the parameter to the this object
Address.
6. toString(): returns a string in the following form:
Person name: Ahmad
Address: Wadeljoz. Jerusalem, 1234
7. LiveInTheSameCity(Person p): returns true if Person object p has the
same city value of this Person object.
2
a) Write a tester class Test.java that initialize and print Address and
Person objects for Saeed Halawany who lives in (Wadeljoz,
Jerusalem, 1234), examine and output if Saeed lives in the same city
where you live.

Explanation / Answer

Answer -> I wrote this java program according to the requirements of the question .

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

package my_chegg_package;

public class Address {
   public String street;
   public String city;
   public int zipCode;
   Address(String street, String city, int zipCode)
   {   
       this.street=street;
       this.city=city;
       this.zipCode=zipCode;  
   }
   Address(Address c) {
   System.out.println("Copy constructor called");
   street = c.street;
   city = c.city;
   zipCode = c.zipCode;
   }
   public String getStreet() {
       return street;
   }
   public void setStreet(String street) {
       this.street = street;
   }
   public String getCity() {
       return city;
   }
   public void setCity(String city) {
       this.city = city;
   }
   public int getZipCode() {
       return zipCode;
   }
   public void setZipCode(int zipCode) {
       this.zipCode = zipCode;
   }
boolean isSameCity(Address a)
{
   if(city.equals(a.city))
   {
       return true;
   }
   else
   {
       return false;
   }
}
@Override
   public String toString(){
       return street+","+city+","+zipCode;
       }
}

package my_chegg_package;

public class Person {
   public String name;
   Address address;
   public Person(String name,Address address)
   {
       this.name=name;
       this.address = address;
   }
   public Person(String name, String street, String city, int zipCode)
   {
       this.name=name;
       address.street=street;
       address.city=city;
       address.zipCode=zipCode;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }
   @Override
   public String toString(){
       return "Person Name : "+ name+" "+"Address : "+address.street+" ,"+address.city+" , "+address.zipCode;
       }
}

package my_chegg_package;

public class Test {

  
   public static void main(String[] args) {
       Address addressObj = new Address("Wadeljoz","Jerusalem", 1234);
      
       Person personObj = new Person("Saeed Halawany",addressObj);
      
       System.out.println(personObj);
      
       Address myAddress = new Address("stl road","Seatle",4673);
      
       if(myAddress.isSameCity(addressObj))
       {
           System.out.println(" my and saeed city are same");
       }
       else
       {
           System.out.println(" my and saeed city are not same");
       }
   }

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT -

Person Name : Saeed Halawany
Address : Wadeljoz ,Jerusalem , 1234

my and saeed city are not same

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote