Question

[CODE] Write a class encapsulating the concept of a house, assuming a house has the following...

[CODE] Write a class encapsulating the concept of a house, assuming a house has the following attributes: value (in dollars), a city location, and number of bedrooms.

Your class name should be “House” and your code should be in a file called “House.java”. You will need to create this file. In this class, include:

  • Private instance variables for the previously-mentioned attributes

  • A default constructor

  • An overloaded constructor

  • Accessor and mutator methods (i.e., get and set methods)

  • The toString method

  • The equals method
    Your default constructor should typically initialize the instance variables to some default values that you feel are appropriate (let’s just go with a $100,000, 2-bedroom home in Azusa). The overloaded constructor should pass in values for value, city location and number of bedrooms which are used to initialize the class’ instance variables.

    The as your client class (House_Client.java) has been provided to test all the methods in your class. So, you would want to test the default and overloaded constructors, as well as accessors, mutators, toString and equals methods. Since this is the first time you’ve ever thought about creating a client to test a user-defined class, it is provided below.

House_Client:

import java.text.DecimalFormat;

public class House_Client {

public static void main(String[] args) {

// TODO Auto-generated method stub

// Decimal format....do not need cents b/c negligible

// compared to hundreds of thousands of dollars

DecimalFormat df = new DecimalFormat("$#,##0");

// Testing default constructor and toString()

House defaultHouse = new House();

System.out.println(defaultHouse.toString());

System.out.println(); // Spacing

// Testing overloaded constructor and toString()

House bigHouse = new House(1400000, "Pasadena", 6);

System.out.println(bigHouse.toString());

// Tests the getters

System.out.println("Before modifying big house using setters:");

System.out.println("Big house value: " + df.format(bigHouse.getValue()));

System.out.println("Big house location: " + bigHouse.getLocation());

System.out.println("Big house bedrooms: " + bigHouse.getNumBedrooms());

// Apply the setters....will test their functionality

// by calling getters again next...

bigHouse.setValue(3200000);

bigHouse.setLocation("Hollywood");

bigHouse.setNumBedrooms(14);

System.out.println(); // Spacing

// Tests that the setters did their jobs

System.out.println("After modifying big house using setters:");

System.out.println("Big house value: " + df.format(bigHouse.getValue()));

System.out.println("Big house location: " + bigHouse.getLocation());

System.out.println("Big house bedrooms: " + bigHouse.getNumBedrooms());

System.out.println(); // Spacing

// Test the equals() method on case that should return false

System.out.print("The default house and big house ");

if (defaultHouse.equals(bigHouse))

System.out.println("are the SAME houses!");

else

System.out.println("are DIFFERENT houses!");

System.out.println(); // Spacing

// Test the equals() method on case that should return true

House identicalBigHouse = new House(3200000, "Hollywood", 14);

System.out.print("The identical big house and big house ");

if (identicalBigHouse.equals(bigHouse))

System.out.println("are the SAME houses!");

else

System.out.println("are DIFFERENT houses!");

}

}

Expected Output:

This house is in Azusa, is worth $100,000 and has 2 bedrooms.

This house is in Pasadena, is worth $1,400,000 and has 6 bedrooms. Before modifying big house using setters:
Big house value: $1,400,000
Big house location: Pasadena

Big house bedrooms: 6

After modifying big house using setters: Big house value: $3,200,000
Big house location: Hollywood
Big house bedrooms: 14

The default house and big house are DIFFERENT houses!
The identical big house and big house are the SAME houses!

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

House.java

import java.text.DecimalFormat;

public class House {
   //declaring private variavles
   private int value;
   private String location;
   private int numBedrooms;
  
   //getter and setters for each variavle
   public int getValue() {
       return value;
   }

   public void setValue(int value) {
       this.value = value;
   }

   public String getLocation() {
       return location;
   }

   public void setLocation(String location) {
       this.location = location;
   }

   public int getNumBedrooms() {
       return numBedrooms;
   }

   public void setNumBedrooms(int numBedrooms) {
       this.numBedrooms = numBedrooms;
   }
  
  
   //toString method
   public String toString(){
       DecimalFormat df = new DecimalFormat("$#,##0");
       String s = "This house is in " + getLocation() +", is worth " + df.format(getValue()) +" and"
               + " has " + getNumBedrooms() +" bedrooms.";
       return s;
              
   }
  
   //equals method that checks if two hpuses are equal
   public boolean equals(House h)
   {
       if(this.getLocation().equals(h.getLocation()) && this.getNumBedrooms() == h.getNumBedrooms() && this.getValue() == h.getValue())
           return true;
       return false;
   }

  
   //default constructor
   House()
   {
       setValue(100000);
       setLocation("Azusa");
       setNumBedrooms(2);
   }
   //parameterised constructor
   House(int value, String location, int numBedrooms)
   {
       setValue(value);
       setLocation(location);
       setNumBedrooms(numBedrooms);
   }
}

Add a comment
Know the answer?
Add Answer to:
[CODE] Write a class encapsulating the concept of a house, assuming a house has the following...
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
  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • write a class encapsulating the concept of a student assuming that the student has the following...

    write a class encapsulating the concept of a student assuming that the student has the following attributes the name of student the average of the student the rite a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student's GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods, toString() and equals(). Also include a method returning the...

  • Write a class encapsulating the concept of daily temperatures for a week with a single dimensiona...

    Write a class encapsulating the concept of daily temperatures for a week with a single dimensional array of temperatures. Write the following methods: • A constructor accepting an array of seven temperatures as a parameter. • Accessor, mutator, toString() and equals() methods • A method returning how many temperatures were below freezing. • A method returning an array of temperatures above 100 degrees. • A method returning the largest change in temperature between any two consecutive days. • A method...

  • Write a class encapsulating the concept of a television set, assuming a television set has the...

    Write a class encapsulating the concept of a television set, assuming a television set has the following attributes: a brand and a price. include a constructor, the accessors and mutators, and methods to string and equals. Write a client class to test all the methods in your class.

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used...

    I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used in the lecture to complete the below exercises. Do the following: 1. Create two instance variables, lat and lng, both of which should be doubles. 2. Write the default constructor. 3. Write the non-default constructor. 4. Write 2 accessor methods, one for each instance variable. 5. Write 2 mutator methods, one for each instance variable. 6. Write a method that will return the location...

  • 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...

  • House Class Specification The House class represents a house. A house has an address (address instance...

    House Class Specification The House class represents a house. A house has an address (address instance variable), a year it was built (built instance variable), and the names of residents of the house (residents instance variable). The declaration of each variable follows. private String address; private int built; private StringBuffer residents; The class methods are: 1. Constructor - Takes a string (representing the address) and an integer (representing the year the house was built) as parameters, and initializes the corresponding...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

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