Jump to content

Need help on a basic matter

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Daniel Savard

Daniel Savard

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

I'm trying to build a basic java program to test what i know.
So far, it's not going anywhere.. lol :)

See my code below, as I have 1 main class called HOUSE, and subclasses called living.. kitchen.. and whatever
I have input variable like lenght and width and trying to "getarea". the result i'm getting is NULL.. and i don'T know why

Any ideas, and thanks for any support you may provide.

/**
*
* @author Daniel
*/
public class AlexHouse1 {
private static int Height = 12; // The height is the same for all rooms
private static int Lenght;
private static int Width;
private static int floors;
private static int HouseNumber = 33; // The House civic number
private static int HouseRooms = 4; // Number of room into the House
private static int YearConstruction = 2001;
private static String HouseStreet = "My street name"; // The Street name
private static String HouseCity = "Toronto"; // City Name
private static String HouseSize;
int temperature; // It's temperature, but i have not assiged value.
String HouseColor = "yellow"; // Outside color of the house


public class AlexHouse1Size {
public int Lenght = 0;
public int Width = 0;
public AlexHouse1Size(int a, int b){
Lenght = a;
Width = b;
}


public int getArea() {
return Lenght * Width;
}
AlexHouse1Size HouseSize = new AlexHouse1Size(12,15);

}






public class LivingRoom extends AlexHouse1 { // Subclaass Living Room
int LivingRoomLenght;
int LivingRoomWidth;
int LivingRoomLHeight = 8;
int LivingRoomSize;
String LivingRoomColor = "beige";
int LivingRoomTemperature;

}


public static void main(String[] args) {
AlexHouse1 House = new AlexHouse1(); // To create AlexHouse "object"



System.out.println("Alex House have a total of " + HouseRooms + " rooms");
System.out.println("and she was built in " + YearConstruction);
System.out.println("The house total height is " + Height);
System.out.println("The house size is " + HouseSize);
}

}

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
You are getting zero because HouseSize is not initialized to anything. So Java initializes it to NULL.

Also, having you're main function in the AlexHouse class is not a good idea. This should be in a seperate class that declares an AlexHouse1 object and calls methods on it. You declared an AlexHouse1 object in main but are not using it. It's also redundant to do that since it's in the class and you have access to the private fields anyways.

public static void main(String[] args) {
        AlexHouse1 House = new AlexHouse1(); // To create AlexHouse "object"



        System.out.println("Alex House have a total of " + HouseRooms + " rooms");
        System.out.println("and she was built in " + YearConstruction);
        System.out.println("The house total height is " + Height);
        System.out.println("The house size is " + HouseSize);
    }

Should be in a Main class and use accessor methods instead of direct access to the fields.

To fix the NULL problem just make the HouseSize variable equal to something.

Example:

HouseSize = "Large"


#3
Daniel Savard

Daniel Savard

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks!
I think i will start over from scratch as I think this is a mess.

Ok, what i'm trying to do is to describe my house.

Let's consider AlexHouse is my main class. this should not be on the same file as the main method, or it does not matter?
also i understand that to add more rooms to my main class AlexHouse, i add to do like

"public class LivingRoom extends AlexHouse1 {
Class description;}

Is it prerable to have separate file ?

Thanks again!

#4
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts
Nope it doesn't have to, I actually prefer having my main method in a separate class.
That's just me. :P

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Just a thought. Shouldn't Living, Kitchen, etc inherit from a generic class Room? Then House would have an array of Room :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Daniel use code tags. Select your code and click #.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

fread said:

Daniel use code tags. Select your code and click #.

You can use the 'Edit Post' button, too, if you'd like; that way you don't have to make a whole new post (or half- a new post :D) just to add the code tags.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users