Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Inheritance problem

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Inheritance problem

    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?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java Inheritance problem

    Please edit your post and fix the code tags. Try

    [code=java]

    //... your code goes here

    [/code]

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Inheritance problem

    fixed the [code=java] part

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java Inheritance problem

    OK. Your area and thus your rent is being calculated correctly. You're calculating it on the BlockOfFlats object, and for that object the 0 area is correct.

    What are you expecting that the result should be?

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Inheritance problem

    I was expecting 12000000

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Java Inheritance problem

    Quote Originally Posted by Grot View Post
    I was expecting 12000000
    Please explain why you would expect that. Understand that you're calling the calculateRent() method on the flats variable. Please show where you make settings to the object that this variable references so that rent would return anything but 0.

Similar Threads

  1. problem with inheritance
    By freedom12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 29th, 2013, 04:59 AM
  2. Replies: 1
    Last Post: November 2nd, 2012, 05:56 AM
  3. [SOLVED] Small problem regarding inheritance of classes
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 10th, 2011, 03:11 PM
  4. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 07:13 AM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM