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);
}
}
6 replies to this topic
#1
Posted 15 December 2011 - 07:38 AM
|
|
|
#2
Posted 15 December 2011 - 09:26 AM
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.
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:
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
Posted 15 December 2011 - 09:41 AM
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!
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
Posted 15 December 2011 - 07:10 PM
Nope it doesn't have to, I actually prefer having my main method in a separate class.
That's just me. :P
That's just me. :P
#5
Posted 16 December 2011 - 12:19 PM
Just a thought. Shouldn't Living, Kitchen, etc inherit from a generic class Room? Then House would have an array of Room :)
#6
Posted 16 December 2011 - 02:33 PM
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
Posted 16 December 2011 - 02:56 PM
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


Sign In
Create Account

Back to top









