Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math in java help

 
Gray Bee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm in my first programming class and I'm having some trouble with part of this assignment. We have to construct concentric rectangles and take the area of the path between then (just subtract the larger from the smaller areas), and then take the diagonals of the rectangles. I have them constructed, but I'm not sure of the syntax for the math operations. Here is the code I have so far.

public static void main (String[] args) {

Rectangle box = new Rectangle(45,55,100,80);
Rectangle box2 = new Rectangle ((int) box.getX()+10, (int)box.getY()+10, (int)box.getWidth()-20, (int) box.getHeight()-20);

System.out.printf("The coordinates of the bigger rectangle are: {(%.0f,%.0f), (%.0f,%.0f),(%.0f,%.0f), (%.0f,%.0f)}%n",
box.getX(), box.getY(), box.getX()+box.getWidth(), box.getY(),
box.getX()+box.getWidth(), box.getY()+box.getHeight(),
box.getX(), box.getY()+box.getHeight());
System.out.printf("The coordinates of the smaller rectangle are: {(%.0f,%.0f), (%.0f,%.0f),(%.0f,%.0f), (%.0f,%.0f)}%n",
box2.getX(), box2.getY(), box2.getX()+box2.getWidth(), box2.getY(),
box2.getX()+box2.getWidth(), box2.getY()+box2.getHeight(),
box2.getX(), box2.getY()+box2.getHeight());


Any help would be appreciated, thanks!
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gray,

Welcome to JavaRanch. Please use code tags to submit your code so that it looks nice and aligned.

Regards,
John Eipe.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at java documentation to see the list of functions available in Math class.

All the functions defined there are static. So you could call them using a similar syntax like Math.floor(x).

Thanks,
John Eipe.
 
Gray Bee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but never mind, I figured it out finally.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic