aspose file tools*
The moose likes Beginning Java and the fly likes its a classic! help with area of shapes! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "its a classic! help with area of shapes!" Watch "its a classic! help with area of shapes!" New topic
Author

its a classic! help with area of shapes!

N Doidge
Greenhorn

Joined: Jan 24, 2011
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 24, 2011
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
N Doidge
Greenhorn

Joined: Jan 24, 2011
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 24, 2011
Posts: 4
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

Joined: Oct 25, 2008
Posts: 2700

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


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: its a classic! help with area of shapes!