aspose file tools*
The moose likes Beginning Java and the fly likes Trying to get values for the perimeter and center of a rectangle. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to get values for the perimeter and center of a rectangle. " Watch "Trying to get values for the perimeter and center of a rectangle. " New topic
Author

Trying to get values for the perimeter and center of a rectangle.

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
My assignment wants me to get and return values for a rectangle.

I'm having difficulty getting the perimeter and center values to show up.

I'm also thinking that my code does not look that great for getting values. so if you have any critiques I would be happy to hear them



here are the instructions just in case:

"Have the class define methods to calculate and return values for:
the area of a rectangle: areaRectangle()
the perimeter of the rectangle: perimeterRectangle()
the X value of the center of the rectangle: centerX()
the Y value of the center of the rectangle: centerY()


Be sure to have an applet that has a paint method that first draws a rectangle using


Be careful to change the y values in your drawString calls so they do not overwrite each other and so that the first one is low enough to not be in the way of the drawn rectangle.

Hint: the drawString for the center of the rectangle will probably use a lot of String concatenation to get the point brackets and comma in ( , ) "

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

kenny gill wrote:I'm also thinking that my code does not look that great for getting values. so if you have any critiques I would be happy to hear them...

OK:
First: I would expect to see two classes: A Rectangle, and an Applet (RectangleApplet?) that uses a Rectangle. The added advantage to that is that you don't have to add 'Rectangle' to the names of all your methods.
A good rule to remember: A Rectangle is NOT (necessarily) an Applet; so it doesn't make sense to tie the two together.

Second: You haven't assigned any values to your 'answer2' and 'answer3' fields, so the likelihood is that you're getting 0 (and can you think of better names? It's part of the programmer's art).

Third: Your call to Graphics.drawRect() uses values copied from the values for 'width' and 'height'. How do you think you might do it better?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to get values for the perimeter and center of a rectangle.