• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rectangle in Java  RSS feed

 
John Jacksons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helllo Everyone!!

I have the following problem:
In your Java1_Homework working set, create a project named java1_Project11. Copy ReturnDemo.java from java1_Lesson11 and paste it into this new project.
1.Give it Instance Variables
int height = 60;
int width = 42;(Note that this use of Instance variables means you might want to edit the areaRectangle method's parameters--specifically, you should not need any parameters because they will be known for the whole class.)


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


3.Be sure to have an applet that has a paint method that first draws a rectangle using
g.drawRect( 25, 25, width, height); // always gives original upper left corner of (25,25)then calls your new methods and prints results as shown (the results are if the values for height were 40 and width were 30):
area of rectangle is 1200
perimeter of rectangle is 140
rectangle center is (40,45)

Note that your values should be different because you do not have a height of 40 and a width of 30, but you can use these numbers to assure yourself of correct calculations in testing.

4.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 ( , )


I have the following code:



I'm nearly there, but not completely sure how to finish? I appreciate advice. Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the ", which should probably be ," in one of your drawString calls, what is going wrong.
There are limits to how much we can tell you about homework.
 
John Jacksons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are my formulas correct? I've been told no.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tested them? What did you get? What did you expect?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jacksons wrote:Are my formulas correct? I've been told no.


I have a question. Is Center X and Center Y supposed to report the location of the center of the rectangle? Or is it supposed to report what is half the width and what is half the height?

Henry
 
John Jacksons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's the center of the rectangle (location), but am not completely positive
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code you have the rectangle starting at (25,25). Then how can the centerX and centerY be halves of width and height respectively? Try out drawing the co-ordinate system in a sheet of paper and trace the rectangle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!