John Jacksons

Greenhorn

Posts: 11

posted 4 years ago

Helllo Everyone!!

I have the following code:

I'm nearly there, but not completely sure how to finish? I appreciate advice. Thanks again!

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

John Jacksons

Greenhorn

Posts: 11

posted 4 years ago

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.

Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook