Java-Classes, what am I doing wrong?
Hey all
I did a thred some days ago where I failed to explaing what I didn't understand, so I kept repeating the same tutorial ( Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language) )
Now I'm trying to do my own version of this, with another "simple" object, a Chair :D
Anyway, i can't get it to work as I wan't it to
What's wrong? :s
(I'm trying to follow this lead: What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts) )
Code:
public class Chair
{
public static void main (String [] arg)
{
//Creatubg 2 chairs
ChairClass Chair1 = new ChairClass();
ChairClass Chair2 = new ChairClass();
//Giving the 1st chair some specifications
Chair1.IsChairBroken(true);
Chair1.ChairRating("High");
Chair1.Print();
//2nd chair info
Chair2.IsChairBroken(false);
Chair2.ChairRating("Low");
Chair2.Print();
}
}
class ChairClass
{
boolean broken;
String rating;
void IsChairBroken(boolean broken)
{
if (broken)
broken = true;
else
broken = false;
}
void ChairRating (String rating)
{
rating = rating;
}
void Print()
{
System.out.println("Chair is broken: " + broken + " Chair rating: " + rating);
}
}
It gives the output:
Code:
Chair is broken: false Chair rating: null
Chair is broken: false Chair rating: null
But I'd like it to be like:
Code:
Chair is broken: [B]true [/B]Chair rating: [B]High[/B]
Chair is broken: false Chair rating: [B]Low[/B]
Any help or explanations what's wrong?
Thanks
// Matt~
[B]EDIT: Thanks for reading, but I found the simple error[/B
Ok, the error was simple, In the chairclass, the new inputs had the same "names" as the already set ones, and that "messed it up" :D
Thanks anyway :)
Code:
public class Chair
{
public static void main (String [] arg)
{
//Creatubg 2 chairs
ChairClass Chair1 = new ChairClass();
ChairClass Chair2 = new ChairClass();
//Giving the 1st chair some specifications
Chair1.IsChairBroken(true);
Chair1.ChairRating("High");
Chair1.Print();
//2nd chair info
Chair2.IsChairBroken(false);
Chair2.ChairRating("Low");
Chair2.Print();
}
}
class ChairClass
{
boolean [B]broken[/B];
String [B]rating[/B];
void IsChairBroken(boolean [B]InputBroken[/B])
{
[B]broken = InputBroken[/B];
}
void ChairRating (String [B]InputRating[/B])
{
[B]rating = InputRating[/B];
}
void Print()
{
System.out.println("Chair is broken: " + broken + " Chair rating: " + rating);
}
}