Tino Intro To Java

Last modified: January 09, 2024

Lab 2b House With Input

In this lab, you will create a House class and read user input with the Scanner class.

Create the House Class with User Input

Create a new class named in the format PX_LastName_FirstName_House

At the top of your program, write a multi-line comment stating

  • Your name
  • Intro Java
  • Lab 2b - HouseWithInput
  • Period
  • Date

Declare some instance variables to keep track of

  • number of doors
  • number of windows
  • number of bedrooms
  • asking price (cost of the house set by house owner)

Then, add 2 constructors:

  • A default constructor that initializes the attributes as follows:

    • 10 doors
    • 8 windows
    • 3 bedrooms
    • 435000 asking price
  • A second constructor that accepts parameters for number of doors, windows, bedrooms, and asking price

Then, add the following getter methods:

  • A method that returns the number of doors
  • A method that returns the number of windows
  • A method that returns the number of bedrooms
  • A method that returns the asking price

Add the following setter method:

  • A setter method that sets the asking price

Finally, add the following methods:

/** Calculate and return the market value for this house
    Market value is $300,000 plus $55,000 per bedroom
    plus $3,000 per window */
public double calculateMarketValue() {
    // Add your code here
}

// Adds more windows to the house
public void addWindows(int num) {
    // Add your code here
}

// Adds more doors to the house
public void addDoors(int num) {
    // Add your code here
}

/** Adds more bedrooms to the house
    Each new bedroom adds one new door and one new window */
public void addBedrooms(int num) {
    // Add your code here
}

/** returns true if the asking price is less than the
    calculated market value and false otherwise */
public boolean isBelowMarketValue() {
    // Add your code here
}

/** returns a String specifying this house's
     - number of bedrooms
     - market value
     - asking price */
public String toString() {
    // Add your code here
}

Create the Driver Class

Write a driver to test your House class using Scanner. Name your driver using the format PX_LastName_FirstName_HouseDriver.

Create a default house and print out the result of calling toString() on the house.

  • Print out the number of doors, number of windows, number of bedrooms, and asking price using the getters
  • Prompt the user for the number of number of doors, windows and bedrooms, and for the asking price.
  • Create a house using the values the user entered.
  • Print out the result of calling toString() on the house created by the user.
  • Print out whether the house is above, below or at market value
  • Add 1 bedroom to the house the user created.
  • Prompt the user for a new asking price.
  • Change asking price for the user created house using the setter.
  • Print out "After adding a bedroom: "
  • Print out the result of calling toString() on the house created by the user.
  • Print out whether the house is above, below or at market value

Here is some sample output (your wording can vary):

A 3 bedroom house with a market value of $489000.0 is for sale with asking price $435000.0
Doors: 10 Windows: 8 Bedrooms: 3 Asking Price: $435000.0
How many doors do you want? 5
How many windows do you want? 8
How many bedrooms do you want? 2
What is your asking price? 378000
A 2 bedroom house with a market value of $434000.0 is for sale with asking price $378000.0
The asking price is below market value.
What is your new asking price? 567000
After adding a bedroom: 
A 3 bedroom house with a market value of $492000.0 is for sale with asking price $567000.0
The asking price is above market value.

Submission

  • Make sure you have named your classes correctly using the format PX_LastName_FirstName_House and PX_LastName_FirstName_HouseDriver where X is your period. For example, if your name is Michael Wang and you are in 2nd period, the classes should be named P2_Wang_Michael_House and P2_Wang_Michael_HouseDriver.
  • Make sure you changed all references in your code accordingly so the code will still compile.
  • If you changed the class names in BlueJ, the file names should have been automatically changed to match.
  • Look in the file explorer/finder inside your project folder and find the corresponding java files named in the format PX_LastName_FirstName_House.java and PX_LastName_FirstName_HouseDriver.java. You should submit those two files below.
  • You can drag and drop or click on the Browse button and select both files so they are both submitted in a single submission.

You must Sign In to submit to this assignment

Dark Mode

Outline