Results 1 to 4 of 4
  1. #1
    Mattedatten is offline Member
    Join Date
    Aug 2009
    Location
    South Sweden
    Posts
    24
    Rep Power
    0

    Default 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) )

    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);	
    		}
    	}
    It gives the output:
    Java Code:
    Chair is broken: false  Chair rating: null
    Chair is broken: false  Chair rating: null
    But I'd like it to be like:
    Java 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 :)

    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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  3. #3
    Mattedatten is offline Member
    Join Date
    Aug 2009
    Location
    South Sweden
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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.
    Hehe, at first I was writing a new post, but then I realised that that would be double-posting and edited instead

    But thanks for pointing out, now I know that ;)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. what wrong with my java code
    By MAXZZXAM in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 09:38 AM
  2. How to run java classes?
    By crunchybear in forum Advanced Java
    Replies: 7
    Last Post: 10-20-2008, 10:57 AM
  3. Wrong version of javac/java
    By amir650 in forum Eclipse
    Replies: 0
    Last Post: 09-12-2008, 11:33 PM
  4. abt java classes
    By colbay in forum New To Java
    Replies: 1
    Last Post: 07-11-2008, 04:12 PM
  5. Replies: 1
    Last Post: 05-30-2008, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •