Results 1 to 9 of 9
- 01-13-2013, 07:52 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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); } }
- 01-13-2013, 07:53 AM #2
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
Re: Why is my code just returning zeros?
Can you show me what actually occurs when you run the script. Maybe a picture?
- 01-13-2013, 08:00 AM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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); } }
- 01-13-2013, 08:01 AM #4
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-13-2013, 08:18 AM #5
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Why is my code just returning zeros?
Am I not correctly assigning the values maybe?
- 01-13-2013, 08:34 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
Re: Why is my code just returning zeros?
Yes, you're right.Am I not correctly assigning the values maybe?
In the constructor you are saying things like:
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.Java Code:s = size;
---
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:
[Edit] Added some space between the methods too. That also helps readability. Making the code readable will encourage people to help with it.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); } }Last edited by pbrockway2; 01-13-2013 at 08:36 AM.
- 01-13-2013, 10:40 AM #7
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-13-2013, 08:53 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
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.
- 01-14-2013, 06:27 AM #9
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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:
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.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; } }
Similar Threads
-
Help. Create 2D array method that returns index of row that contains the most zeros.
By jlss4e in forum New To JavaReplies: 3Last Post: 08-21-2011, 02:25 AM -
Trimming trailing zeros in a byte
By Aaron_Sharp in forum New To JavaReplies: 1Last Post: 01-06-2011, 11:04 AM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 01:38 AM -
How to display numbers with leading zeros
By Java Tip in forum java.langReplies: 1Last Post: 06-14-2008, 06:36 PM -
How to display numbers with leading zeros
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:34 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks