Question

Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

  1. Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields.

Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString.

  1. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both of type StreetHouse.

Create two constructors. The first one has an integer argument for initializing streetNumber. It assigns zeros to numerical fields, “No name yet” to homeowner, and null to the neighbors. The second one has arguments for initializing numerical fields and a string argument for a homeowner. Use super constructor when creating the constructor for this class. Add accessors for all fields and mutators for all fields except yearBuilt. Add the overriding toString() method.

  1. Define a class Street with the following fields: streetName of the type String, houses as array of type House, and numberOfHouses of the type int. Be aware the variable numberOfHouses represents the current number of houses and may be less than the length of the array.

Create a constructor with two fields: name as String and total of the type int. It should create a street with the given name and the array of the length total filled with null houses .

Create a method

      public static boolean buildHouse (House h)

which tries to add a house to the array houses and returns true if it is successful, false otherwise.

Create a method print() for printing the list of houses for the street.

  1. Write a driver program to test all your methods.
  2. In Java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

class House {

private int numberOfUnits;

private int yearBuilt;

private double assessedPrice;

public House() {

}

public House(int noOfUnits, int year, double price) {

numberOfUnits = noOfUnits;

yearBuilt = year;

assessedPrice = price;

}

/**

* @return the numberOfUnits

*/

public int getNumberOfUnits() {

return numberOfUnits;

}

/**

* @return the assessedPrice

*/

public double getAssessedPrice() {

return assessedPrice;

}

/**

* @param numberOfUnits the numberOfUnits to set

*/

public void setNumberOfUnits(int numberOfUnits) {

this.numberOfUnits = numberOfUnits;

}

/**

* @param assessedPrice the assessedPrice to set

*/

public void setAssessedPrice(double assessedPrice) {

this.assessedPrice = assessedPrice;

}

/**

* @param yearBuilt the yearBuilt to set

*/

public void setYearBuilt(int yearBuilt) {

this.yearBuilt = yearBuilt;

}

public String toString() {

return "No Of Units: " + numberOfUnits + ", Year Built: " + yearBuilt + ", Assessed Price: " + assessedPrice;

}

}

class StreetHouse extends House {

private int streetNumber;

private String homeowner;

private StreetHouse leftNeighbour;

private StreetHouse rightNeighbour;

public StreetHouse(int streetNo) {

super();

streetNumber = streetNo;

setNumberOfUnits(0);

setYearBuilt(0);

setAssessedPrice(0.0);

homeowner = "No name yet";

leftNeighbour = null;

rightNeighbour = null;

}

public StreetHouse(int noOfUnits, int year, double price, int streetNo, String owner) {

super(noOfUnits, year, price);

streetNumber = streetNo;

homeowner = owner;

}

/**

* @param streetNumber the streetNumber to set

*/

public void setStreetNumber(int streetNumber) {

this.streetNumber = streetNumber;

}

/**

* @param homeowner the homeowner to set

*/

public void setHomeowner(String homeowner) {

this.homeowner = homeowner;

}

/**

* @param leftNeighbour the leftNeighbour to set

*/

public void setLeftNeighbour(StreetHouse leftNeighbour) {

this.leftNeighbour = leftNeighbour;

}

/**

* @param rightNeighbour the rightNeighbour to set

*/

public void setRightNeighbour(StreetHouse rightNeighbour) {

this.rightNeighbour = rightNeighbour;

}

/**

* @return the streetNumber

*/

public int getStreetNumber() {

return streetNumber;

}

/**

* @return the homeowner

*/

public String getHomeowner() {

return homeowner;

}

/**

* @return the leftNeighbour

*/

public StreetHouse getLeftNeighbour() {

return leftNeighbour;

}

/**

* @return the rightNeighbour

*/

public StreetHouse getRightNeighbour() {

return rightNeighbour;

}

public String toString() {

String result = super.toString();

result += "\nStreet Number: " + streetNumber + "\nHome Owner: " + homeowner + "\n";

if (leftNeighbour != null)

result += "Left Neighbour:\n" + leftNeighbour.toString() + "\n";

if (rightNeighbour != null)

result += "Right Neighbour:\n" + rightNeighbour.toString();

return result;

}

}

class Street {

private String streetName;

private House[] houses;

private int numberOfHouses;

public Street(String name, int total) {

streetName = name;

numberOfHouses = 0;

houses = new House[total];

}

public boolean buildHouse(House h) {

if (numberOfHouses >= houses.length)

return false;

houses[numberOfHouses] = h;

numberOfHouses++;

return true;

}

public void print() {

System.out.println("List of houses");

for (int i = 0; i < numberOfHouses; i++) {

System.out.println("House " + (i + 1));

System.out.println(houses[i]);

}

System.out.println();

}

}

public class HouseDriver {

public static void main(String[] args) {

Street st = new Street("L B Nagar", 5);

StreetHouse house1 = new StreetHouse(100, 2015, 20000.00, 1234, "ANUNAGA");

StreetHouse house2 = new StreetHouse(2345);

house2.setHomeowner("NAGARAJU");

house2.setNumberOfUnits(200);

house2.setYearBuilt(2016);

house2.setAssessedPrice(500.00);

house2.setLeftNeighbour(house1);

st.buildHouse(house1);

st.buildHouse(house2);

st.print();

}

}

Add a comment
Know the answer?
Add Answer to:
Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...
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
  • create a Person class with two fields (name(String), age(int)) create all the required methods such as...

    create a Person class with two fields (name(String), age(int)) create all the required methods such as the accessors, mutators, toString(),constructors etc. Use the comparable interface and implement the compareTo method such that the person objects can be compared and sorted according to their age.

  • Programming Language is Java 1. Create a class to describe a car. Include fields like: make,...

    Programming Language is Java 1. Create a class to describe a car. Include fields like: make, model, year and color. Add all the accessors and mutators for these fields. Add a constructor that can initialize the fields. 2. Create a class that will enable you to handle standard mail communication with a customer. Add all accessors and mutators, as well as a constructor. Add a method named fullAddress that will return the address formatted for a standard mail. Add a...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

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

  • Program Challenge 10 Design a Ship class that the following members: ·         A field for the...

    Program Challenge 10 Design a Ship class that the following members: ·         A field for the name of the ship (a string). ·         A field for the year that the ship was built (a string). ·         A constructor and appropriate accessors and mutators. ·         A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: ·         A field for the...

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

  • Create a Mattress class should have fields for size (king, queen, twin), manufacturer, and price, and...

    Create a Mattress class should have fields for size (king, queen, twin), manufacturer, and price, and a constructor should set default values for each. Write set methods only for size and manufacturer. The set method for size adds $200 for king or $100 for queen. Add a toString() method that returns the data from all of the fields. A child class named AdjustableMattress extends Mattress and adds a field for adjustment type (air or mechanical), with air being the default....

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

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

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