Question

In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

In this assignment, you will implement Address and Residence classes.

Create a new java project.

Part A

  1. Implementation details of Address class:

Add and implement a class named Address according to specifications in the UML class diagram.

  • Data fields: street, city, province and zipCode.
  • Constructors:
  • A no-arg constructor that creates a default Address.
  • A constructor that creates an address with the specified street, city, state, and zipCode
  • Getters and setters for all the class fields.
  • toString() to print out all available information about the current object

  1. A test program for the Address class

To demonstrates the Address class, in main method write a program that:

  1. Prompts the user to enter the street name
  2. Prompts the user to enter city name
  3. Prompts the user to enter the province name
  4. Prompts the user to enter the zip code
  5. Creates an instance called testAddress of type Address with the entered data
  6. Displays this information using toString().

Example of data:

  • Street: 265 Yorkland Blvd.
  • City: Toronto
  • State: Ontario
  • zipCode: M2J 1S5

PART B

  1. Implementation details of Residence class:

Add and implement a class named Residence according the instruction in the UML diagram

  • Data fields: bedrooms, bathrooms, price, squareFeet, yearBuilt and address.
  • Constructors:
  • A no-arg constructor that creates a default Residence.
  • A constructor that creates a Residence with the specified bedrooms, bathrooms, price, squareFeet, yearBuilt and address.

  • Getters and setters for all the class fields.
  • calculateCommission() that returns the commission amount. The commission rate is set up at 2%.
  • toString() that returns the information about the current object( bedrooms, bathrooms, etc.).
  1. A test program for the Residence class

In main method, you already created an instance of Address class called testAddress and prompted the user to enter the address details.

  1. Now prompt the user to enter the numbers of bedrooms and bathrooms, price, square Feet and year built.
  2. Create an instance of the residence class with the entered data (including the address data).
  3. Display the information about the residence using toString().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Address.java

public class Address
{
   private String street;
   private String city;
   private String province;
   private String zipCode;
  
   // default constructor
   public Address()
   {
       street = "";
       city = "";
       province = "";
       zipCode = "";
   }
  
   // parameterized constructor
   public Address(String street, String city, String province, String zipCode)
   {
       this.street = street;
       this.city = city;
       this.province = province;
       this.zipCode = zipCode;
   }
  
   // setters
   public void setStreet(String street)
   {
       this.street = street;
   }
  
   public void setCity(String city)
   {
       this.city = city;
   }
  
   public void setProvince(String province)
   {
       this.province = province;
   }
  
   public void setZipCode(String zipCode)
   {
       this.zipCode = zipCode;
   }
  
   // getters
   public String getStreet()
   {
       return street;
   }
  
   public String getProvince()
   {
       return province;
   }
  
   public String getCity()
   {
       return city;
   }
  
   public String getZipCode()
   {
       return zipCode;
   }
  
   // return String representation
   public String toString()
   {
       return street+", "+city+", "+province+" - "+zipCode;
   }
  
}
//end of Address.java

// Residence.java

public class Residence {
  
   private int bedrooms;
   private int bathrooms;
   private double price;
   private int squareFeet;
   private int yearBuilt ;
   private Address address;
// default constructor
   public Residence()
   {
       bedrooms = 0;
       bathrooms = 0;
       price = 0;
       squareFeet = 0;
       yearBuilt = 0;
       address = new Address();
   }
// parameterized constructor
   public Residence(int bedrooms, int bathrooms, double price, int squareFeet, int yearBuilt, Address address)
   {
       this.bedrooms = bedrooms;
       this.bathrooms = bathrooms;
       this.squareFeet = squareFeet;
       this.yearBuilt = yearBuilt;
       this.price = price;
       this.address = address;
   }
// setters
   public void setBedrooms(int bedrooms)
   {
       this.bedrooms = bedrooms;
   }
  
   public void setBathrooms(int bathrooms)
   {
       this.bathrooms = bathrooms;
   }
  
   public void setSquareFeet(int squareFeet)
   {
       this.squareFeet = squareFeet;
   }
  
   public void setYearBuilt(int yearBuilt)
   {
       this.yearBuilt = yearBuilt;
   }
  
   public void setAddress(Address address)
   {
       this.address = address;
   }
// getters
   public int getBedrooms()
   {
       return bedrooms;
   }
  
   public int getBathrooms()
   {
       return bathrooms;
   }
  
   public double getPrice()
   {
       return price;
   }
  
   public int getSquareFeet()
   {
       return squareFeet;
   }
  
   public int getYearBuilt()
   {
       return yearBuilt;
   }
  
   public Address getAddress()
   {
       return address;
   }
// method to calculate and return commission
   public double calculateCommission()
   {
       return price*0.02;
   }
// toString method
   public String toString()
   {
       return "Address: "+address.toString()+"\nSquare feet: "+squareFeet+"\nYear built: "+yearBuilt+"\nBedrooms: "+bedrooms+
               "\nBathrooms: "+bathrooms+"\nPrice: "+price+"\nCommission: "+calculateCommission();
   }

}
//end of Residence.java

// ResidenceTester.java : Java program to test Address and Residence class

import java.util.Scanner;

public class ResidenceTester {
  
   public static void main(String[] args) {

       String street, city, province, zipCode;
       int bathrooms, bedrooms, yearBuilt, squareFeet;
       double price;
      
       Address testAddress;
      
       Scanner scan = new Scanner(System.in);
       // input details of Address
       System.out.print("Enter the street: ");
       street = scan.nextLine();
       System.out.print("Enter city: ");
       city = scan.nextLine();
       System.out.print("Enter province: ");
       province = scan.nextLine();
       System.out.print("Enter zip code: ");
       zipCode = scan.nextLine();
       // create an object of Address class
       testAddress = new Address(street, city, province, zipCode);
       System.out.println(testAddress); // display Address using toString method of Address class
       Residence residence;
       // input details of Residence
       System.out.print("Enter number of bedrooms: ");
       bedrooms = scan.nextInt();
       System.out.print("Enter number of bathrooms: ");
       bathrooms = scan.nextInt();
       System.out.print("Enter square feet: ");
       squareFeet = scan.nextInt();
       System.out.print("Enter year built: ");
       yearBuilt = scan.nextInt();
       System.out.print("Enter price: ");
       price = scan.nextDouble();
       // create object of Residence class
       residence = new Residence(bedrooms, bathrooms, price, squareFeet, yearBuilt, testAddress);
       System.out.println("Residence:\n"+residence); //display Residence details using toString method of Residence class
   }

}
//end of ResidenceTester.java

Output:

Enter the street: 265 Yorkland Blvd. Enter city: Toronto Enter province: Ontario Enter zip code: M23155 265 Yorkland Blvd., T

Add a comment
Know the answer?
Add Answer to:
In this assignment, you will implement Address and Residence classes. Create a new java project. Part...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString...

    java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString methods, getters and setters. Then create three Ticket instances. • Airport: String name, String city, String country • Route: Airport departure , Airport destination • Date: int day, int month, int year (Day must be between 0-31, month must be between 0-12, year must be between 2020-2025; please consider this when you're implementing setters and constructors). • ClockTime: int hour, int minute (Hour must...

  • Implement a new Generic class called a ShapeBox. This class will have two instance data, a...

    Implement a new Generic class called a ShapeBox. This class will have two instance data, a Shape object (as in the graphics Shape like Circle, Rectangle, Ellipse), and a double to store the Shape's area. The specific type of Shape will be specified via Generics so that your class will need a placeholder for the type of Shape. This placeholder must extend Shape. Additionally, ShapeBox will implement the Comparable interface requiring that you implement a compareTo method to compare this...

  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information...

    ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information that may appear on a mailing label. Design and implement each of the described classes below. public interface AddrLabelInterface { String getAttnName(); String getTitle(); // Mr., Mrs., etc. String getName(); String getNameSuffix(); // e.g., Jr., III String getProfessionalSuffix(); // O.D. (doctor of optometry) String getStreet(); String getSuiteNum(); String getCity(); String getState(); String getZipCode(); } Step 1 Create an abstract AddrLabel class that implements the...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT