Hey guys I'm a bit stuck here with my inheritance
package Lab3; public class Building { /* {author=John Roche sb12218, version=1.1}*/ // Attributes : Instance variables public Integer width; public Integer length; public Integer number_of_floors; //constructor to intialise data members public Building (int l, int w, int floors){ length=l; width=w; number_of_floors=floors; } //this method returns the total area of the building public double area(){ return length * width * number_of_floors; } //Set Methods public void setLength(int l) { length =l; } public void setWidth(int w) { width =w; } public void setNumber_Of_Floors(int floors) { number_of_floors =floors; } //Full set of Get methods public Integer getLength() { return length; } public Integer getWidth() { return width; } public Integer getNumber_Of_Floors() { return number_of_floors; } }
package Lab3; public class House extends Building { /* {author=John Roche sb12218, version=1.1}*/ // Attributes : Instance variables public Integer noBedroom; // use superclass constructor public House ( ){ super( 0, 0, 0); } public House(int l, int w, int floors) { super(l, w, floors); } public House (int l, int w, int floors, int nBrooms){ super(l, w, floors); noBedroom = nBrooms; } //Set Methods public void setNoBedroom(int nBrooms) { noBedroom = nBrooms; } //Full set of Get methods public Integer getNoBedroom() { return noBedroom; } }
package Lab3; public class BlockOfFlats extends House { /* {author=John Roche sb12218, version=1.1}*/ // Attributes : Instance variables public BlockOfFlats(int l, int w, int floors) { super(l, w, floors); } int noFlats; public Integer rent; static final double RENT_PER_M2 = 100; // parameterless constructor public BlockOfFlats ( ) { } // constructor to initialise all data members public BlockOfFlats (int l, int w, int floors, int nBrooms, int nFlats) { super(l, w, floors, nBrooms); noFlats = nFlats; } public double calculateRent(){ return area()* RENT_PER_M2; } }
package Lab3; import Lab2.DVD; public class Lab3test { /** * @param args */ public static void main(String[] args) { // Create 2 object variables of type CD Building building; House house; BlockOfFlats flats; // Allocate some memory for each new object building = new Building(0, 0, 0); house = new House(); flats = new BlockOfFlats(); // Set the title by accessing the attribute directly building.setWidth(200); building.setLength(300); building.setNumber_Of_Floors(2); house.setNoBedroom(4); // Output each objects title value System.out.println("Number of Floors = " + building.getNumber_Of_Floors()); System.out.println("Building Area = " + building.area()); System.out.println("Number of Rooms = " + house.getNoBedroom()); System.out.println("Rent = " + flats.calculateRent()); } }
my output is
Number of Floors = 2
Building Area = 120000.0
Number of Rooms = 4
Rent = 0.0
for some reason the rent is not being calculated in the BlockOfFlats class I'm not sure why
everything look ok to me
any help?