1

I need a generic base class called building that stores the number of floors a building has, the number of rooms, and its total square footage. I also need a derived class called house that inherits building and stores the number of bedrooms and bathrooms, and another derived class named office that too inherits building and stores the number of fire extinguishers and telephones.

I have to create a Java Application program that demonstrates this class hierarchy and provide all necessary methods. The classes have to be tested using the following main method.

public class TestBuildings 
{

 public static void main(String[] args) 
 {
    building building1 = new building(1, 10, 6000);
    building building2 = new building();
    System.out.println();

    house house1 = new house(1, 5, 3000);
    house house2 = new house(2, 2);
    System.out.println();

    office office1 = new office(1, 2, 15000);
    office office2 = new office(2, 6, 15000, 6, 30);
    System.out.println();

    //change building properties
    building2.setFloors(3);
    building2.setRooms(12);
    building2.setFeet(20000);
    System.out.println("Building 2: \n" + building2.toString() + "\n\n");

    //change house properties
    house1.setBedrooms(3);
    house1.setBaths(3);
    house2.setFloors(2);
    house2.setRooms(10);
    house2.setFeet(4200);
    System.out.println("House 1: \n" + house1.toString() + "\n\n");
    System.out.println("House 2: \n" + house2.toString() + "\n\n");

    //change office properties
    office1.setFireExt(4);
    office1.setTelephones(50);
    System.out.println("Office 1: \n" + office1.toString() + "\n\n");
 }
}

This is what I have for class building

public class building 
{
 private int floors;
 private int rooms;
 private int sqft;

 public void setFloors(int floors)
 {
    this.floors = floors;
 }

 public int getFloors()
 {
    return floors;
 }

 public void setRooms(int rooms)
 {
    this.rooms = rooms;
 }

 public int getRooms()
 {
    return rooms;
 }

 public void setFeet(int sqft)
 {
    this.sqft = sqft;
 }

 public int getFeet()
 {
    return sqft;
 }
}

This is my class house

public class house extends building
{
 private int bedrooms;
 private int baths;

 public void setBedrooms(int bedrooms)
 {
    this.bedrooms = bedrooms;
 }

 public int getBedrooms()
 {
    return bedrooms;
 }

 public void setBaths(int baths)
 {
    this.baths = baths;
 }

 public int getBaths()
 {
    return baths;
 }
}

And finally, class office

public class office extends building
{
 private int fire;
 private int phones;

 public void setFireExt(int fire)
 {
    this.fire = fire;
 }

 public int getFireExt()
 {
    return fire;
 }

 public void setTelephones(int phones)
 {
    this.phones = phones;
 }

 public int getTelephones()
 {
    return phones;
 }
}

My question, what to I have to add, subtract, or modify in my three classes to make the main method work? Do I need arrays in there somehow?

Any help will be greatly appreciated. Thanks!

1
  • And....? What's the problem you're having? Does the main method currently not work? If not, what is it supposed to be doing, and what is it doing? Also, naming conventions are your friends. house extends building --> House extends Building Commented Nov 16, 2015 at 5:14

2 Answers 2

2

You have to create constructors with parameters for your building, house and office. For example, for building:

public building(int floors, int rooms, int sqft) {
    this.floors = floors;
    this.rooms = rooms;
    this.sqft = sqft;
}

Constructing building without specified parameters has no sence for me - so the building should have specified floors, rooms, sqft, at least you should provide some default values:

public class building {
    private int floors = 1;
    private int rooms = 1;
    private int sqft = 1; // Have no idea what is default value here
    ...
}

Then you define house as class extended from building. This means that your class house is a building and floors, rooms and sqft must be specified to construct it:

 public class house extends building {
    private int bedrooms;
    private int baths;

    public house(int floors, int rooms, int sqft) {
       super(floors, rooms, sqft);
    }
    ...
 }

Again, having constructor of house with just bedrooms and baths has no sence, sinse there is no information about floors,rooms,sqt specified, so you cannot intitialize building correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct answer, but it doesn't explain how he can use super(floors, rooms, sqft) from office, and house.
Can you please explain the use of super(floors, rooms, sqft) from office and house? @WalterM
I get it now. Thanks! @Nyavro
-1
public class building implements java.io.Serializable
{
 private int floors;
 private int rooms;
 private int sqft;

 public void setFloors(int floors)
 {
    this.floors = floors;
 }

 public int getFloors()
 {
    return floors;
 }

 public void setRooms(int rooms)
 {
    this.rooms = rooms;
 }

 public int getRooms()
 {
    return rooms;
 }

 public void setFeet(int sqft)
 {
    this.sqft = sqft;
 }

 public int getFeet()
 {
    return sqft;

 }
}

1 Comment

this 3 class should implements serializable building ,house , office

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.