Results 1 to 4 of 4
- 03-14-2010, 09:57 PM #1
Member
- Join Date
- Aug 2009
- Location
- South Sweden
- Posts
- 24
- Rep Power
- 0
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) )
It gives the output:Java 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); } }
But I'd like it to be like:Java Code:Chair is broken: false Chair rating: null Chair is broken: false Chair rating: null
Any help or explanations what's wrong?Java Code:Chair is broken: [B]true [/B]Chair rating: [B]High[/B] Chair is broken: false Chair rating: [B]Low[/B]
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 :)
Java 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); } }Last edited by Mattedatten; 03-14-2010 at 10:10 PM.
- 03-15-2010, 02:28 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Thanks for marking the thread solved. But I want to point something to you. Once you find a solution please post it in different post, rather editing the original post. It's bit of unclear to find the answer otherwise for other members.
- 03-15-2010, 06:17 AM #3
Member
- Join Date
- Aug 2009
- Location
- South Sweden
- Posts
- 24
- Rep Power
- 0
- 03-15-2010, 04:54 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's pretty cool. Keep it up. ;)
Similar Threads
-
what wrong with my java code
By MAXZZXAM in forum New To JavaReplies: 3Last Post: 11-19-2009, 09:38 AM -
How to run java classes?
By crunchybear in forum Advanced JavaReplies: 7Last Post: 10-20-2008, 10:57 AM -
Wrong version of javac/java
By amir650 in forum EclipseReplies: 0Last Post: 09-12-2008, 11:33 PM -
abt java classes
By colbay in forum New To JavaReplies: 1Last Post: 07-11-2008, 04:12 PM -
Downloading part of a file with java? What have I done wrong?
By JavaHead08 in forum NetworkingReplies: 1Last Post: 05-30-2008, 10:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks