Create a class "House", with an attribute "area", a constructor that sets its value and a method
"ShowData" to display "I am a house, my area is 200 m2" (instead of 200, it will show the real
surface). Include getters an setters for the area, too.
• The "House" will contain a door. Each door will have an attribute "color" (a string), and a method
"ShowData" wich will display "I am a door, my color is brown" (or whatever color it really is).
Include a getter and a setter. Also, create a "GetDoor" in the house.
• A "SmallApartment" is a subclass of House, with a preset area of 50 m2.
• Also create a class Person, with a name (string). Each person will have a house. The method
"ShowData" for a person will display his/her name, show the data of his/her house and the data
of the door of that house.
public class House {
private double area;
private Door door;
public House(double area, Door door) {
this.area = area;
this.door = door;
}
public void showData() {
System.out.println("I am a house, my area is " + area + " m^2");
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public Door getDoor() {
return door;
}
public void setDoor(Door door) {
this.door = door;
}
}
public class Door {
private String color;
public Door(String color) {
this.color = color;
}
public void showData() {
System.out.println("I am a door, my color is " + color);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class SmallApartment extends House {
public SmallApartment(Door door) {
super(50, door);
}
}
public class Person {
private String name;
private House house;
public Person(String name, House house) {
this.name = name;
this.house = house;
}
public void showData(){
System.out.println(name);
house.showData();
house.getDoor().showData();
}
}
Comments
Leave a comment