Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Why is my code just returning zeros?

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Why is my code just returning zeros?

    Hey guys.. I am trying to learn constructors, and made a program to try and do so. It's random lol, but that's not the point. I try to get it to tell me the size, location, and power of a chess piece. When I run it though, rather then get the numbers, I get all zero's. That isn't good :( Can someone tell me what I am doing wrong? Thanks

    Java Code:
    class chess {
    int size;
    int location;
    int power;
    chess(int s, int l, int p) {
    s = size;
    l = location;
    p = power;
    }
    
    
    public static void main(String[] args) {
    chess bishop = new chess(5, 1, 4);
    chess pawn = new chess(1, 2, 1);
    chess queen = new chess(6, 1, 10);
    queen.printPower();
    queen.printSize();
    queen.printLocation();
    pawn.printPower();
    pawn.printSize();
    pawn.printLocation();
    bishop.printPower();
    bishop.printLocation();
    bishop.printSize();
    }
    void printPower() {
    System.out.println(power);
    }
    void printLocation() {
    System.out.println(location);
    }
    void printSize() {
    System.out.println(size);
    }
    
    }

  2. #2
    Asglv is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Why is my code just returning zeros?

    Can you show me what actually occurs when you run the script. Maybe a picture?

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Why is my code just returning zeros?

    I am so confused... I thought maybe I did something wrong, so I tried another random program... It doesn't just return zero; it also returns null
    Java Code:
    class apple{
    int size;
    String color;
    apple(int s, String c) {
    s = size;
    c = color;
    }
    public static void main(String[] args) {
    apple red = new apple(5, "red");
    apple green = new apple(3, "green");
    System.out.println(red.size + red.color);
    System.out.println(green.size + green.color);
     }
    }

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Why is my code just returning zeros?

    Quote Originally Posted by Asglv View Post
    Can you show me what actually occurs when you run the script. Maybe a picture?
    yes.
    Rather then return the 9 values that I named, when I run the executable, it says:
    java chess
    0
    0
    0
    0
    0
    0
    0
    0
    0

    Am I incorrectly naming it or something :/

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Why is my code just returning zeros?

    Am I not correctly assigning the values maybe?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why is my code just returning zeros?

    Am I not correctly assigning the values maybe?
    Yes, you're right.

    In the constructor you are saying things like:

    Java Code:
    s = size;
    but that assigns the value of size to s. Instead you should be assigning the value of s that was passed to the constructor to size. Try all the assignments in the constructors the other way around.

    ---

    The way you have written the code is a bit unreadable and that can be confusing. Classes start with a capital letter, so Chess. Of course the constructor and the file name will also have to change: the class Chess goes in the file Chess.java.

    Also code should be indented. Everything in a block - between { and } - should be indented (four spaces is common). Your code should look like this:

    Java Code:
    class Apple{
        int size;
        String color;
    
        Apple(int s, String c) {
            size = s;
            color = s;
        }
    
        public static void main(String[] args) {
            Apple red = new Apple(5, "red");
            Apple green = new Apple(3, "green");
            System.out.println(red.size + red.color);
            System.out.println(green.size + green.color);
        }
    }
    [Edit] Added some space between the methods too. That also helps readability. Making the code readable will encourage people to help with it.
    Last edited by pbrockway2; 01-13-2013 at 08:36 AM.

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Why is my code just returning zeros?

    Quote Originally Posted by pbrockway2 View Post
    Yes, you're right.

    In the constructor you are saying things like:

    Java Code:
    s = size;
    but that assigns the value of size to s. Instead you should be assigning the value of s that was passed to the constructor to size. Try all the assignments in the constructors the other way around.

    ---

    The way you have written the code is a bit unreadable and that can be confusing. Classes start with a capital letter, so Chess. Of course the constructor and the file name will also have to change: the class Chess goes in the file Chess.java.

    Also code should be indented. Everything in a block - between { and } - should be indented (four spaces is common). Your code should look like this:

    Java Code:
    class Apple{
        int size;
        String color;
    
        Apple(int s, String c) {
            size = s;
            color = s;
        }
    
        public static void main(String[] args) {
            Apple red = new Apple(5, "red");
            Apple green = new Apple(3, "green");
            System.out.println(red.size + red.color);
            System.out.println(green.size + green.color);
        }
    }
    [Edit] Added some space between the methods too. That also helps readability. Making the code readable will encourage people to help with it.
    You are the best! Thank you so much! Now it worked!!!!! Ah, I can now move on. May the sun shine on you all day :p

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why is my code just returning zeros?

    Great - I'm glad you've got that sorted out.

    The thing to remember is that " foo=bar" goes from right to left to mean "assign bar to foo". Some languages write this as "foo <- bar" which is a little clearer to my way of thinking, but you get used to the (almost universal) use of the equals sign.
    MW130 likes this.

  9. #9
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Why is my code just returning zeros?

    I wrote a chess program for fun before.

    I found it useful to have the following classes:

    Java Code:
    public abstract class Piece {
    	
    	public abstract double getValue();
    
    }
    
    class Pawn extends Piece {
    
    	@Override
    	public double getValue() {
    		return 1.0;
    	}
    	
    }
    
    class King extends Piece {
    
    	@Override
    	public double getValue() {
    		return 999;
    	}
    	
    }
    Having a subclass for each type of piece was very useful when determining the legal moves. The ChessBoard object would just look at every square and find a Piece and ask the Piece for its legal moves. This worked out pretty well.

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2011, 02:25 AM
  2. Trimming trailing zeros in a byte
    By Aaron_Sharp in forum New To Java
    Replies: 1
    Last Post: 01-06-2011, 11:04 AM
  3. Replies: 3
    Last Post: 12-10-2008, 01:38 AM
  4. How to display numbers with leading zeros
    By Java Tip in forum java.lang
    Replies: 1
    Last Post: 06-14-2008, 06:36 PM
  5. How to display numbers with leading zeros
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:34 PM

Tags for this Thread

Posting Permissions

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