Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

its a classic! help with area of shapes!

 
N Doidge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, just need a bit of help here as ive been staring at this code for 4 days and not making much headway.
Also its apperantly a classic *drumrolls* area of shapes! with x,y location

Basicly in a nutshell its going to be an array of 20 (at the moment set at 2) of class shapes with subclass exented ( circle, square, retcangle and polygon)
with X,Y location. Also stuck on polygons

At the moment i think that i have made a uttermistake and now damaged the code. As now circle seems to provide two output after one input!
area of the circle 0 is 6358.5
area of the Square 0 is 6358.5
any help would be highly appreciated



 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, it's a classic all right. Pretty much every beginner makes this mistake.

Your lines 31 to 35 are wrapped in braces (like this: { ... }). So the if-statement at #31 controls everything within the braces. But your lines 39 to 42 are not, so the if-statement at #39 only controls #40. Lines 41 and 42 are executed regardless of whether the if-condition at #39 is true or false.

Moral of the story? Always use the braces around the blocks which are to be controlled by an if-statement. You may think it's okay to omit them if there's only one statement to be controlled (which wasn't the case here) but it really isn't. Someone will come along who is unfamiliar with the code (like you, three months later) and add a second statement without adding the missing braces.
 
N Doidge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers paul! your a star! I think that I can see/what you mean regarding the wrapped in braces now



It now seems that its populating the array correctly now
for example two inputs for Circle:
area of the circle 0 is 9156.24
area of the circle 1 is 9847.04

two inputs for square:

area of the Square 0 is 1849.0
area of the Square 1 is 4356.0

one circle and one square:

area of the circle 0 is 6358.5
area of the Square 1 is 4225.0

right so only 2 more shapes to add here and then work how to get location to appear correctly (getting some strange output here 26c623af) with also the polygon workout done lol
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override the "toString" method in your Location class.

It's impossible for the JVM to know what you really want to print for a user-defined class. But since every class is a child of the Object class, they all inherit its implementation of that method, which basically prints the memory address (that hex code you see).

Create a toString method, and define what you want it to really print, and it should work.
 
N Doidge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:You need to override the "toString" method in your Location class.

It's impossible for the JVM to know what you really want to print for a user-defined class. But since every class is a child of the Object class, they all inherit its implementation of that method, which basically prints the memory address (that hex code you see).

Create a toString method, and define what you want it to really print, and it should work.


ahhhh right, now its falling into place lol.
Now only problem is how and where do I place the tostring method within location class?
I have been trying for about a hour now and seem to failing into a pile of information on tostrings methods without any success

 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Doidge wrote:Now only problem is how and where do I place the tostring method within location class?

Well, first of all it's "toString", not "tostring". But I don't see the problem. You already have some methods declared in the Location class. Now you want to declare another one. So just put the new method right after the existing ones. Why is there a question about that?
 
N Doidge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all it's "toString", not "tostring". But I don't see the problem. You already have some methods declared in the Location class. Now you want to declare another one. So just put the new method right after the existing ones. Why is there a question about that?


Sorry about that Paul, adding more confusion lol of using "tostring" on here.

Just was unsure on how to approach that task, really a newbie on java, so kid gloves with myself.

Also would I need to change the location class, double x, double y to int? and use


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need to change the doubles to an int?

If you haven't seen them yet: oracle has some great tutorials about Java. You can find them here. They cover overriding (which is what you want to do with the toString() method).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the toString() method should do is create and return a String representation of your object. if I had a Dog class, i might have it return the dog's name "Fido". I would NOT have it return "The dog's name is Fido" since at some point, I might want to do

System.out.println("Name of the dog is " + myDog);
or
System.out.println(myDog + " went to obedience school.");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic