Posted By

joshnunn on 08/12/09


Tagged

basic class command test constructor main query rectangle learning area private perimeter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cnsaturn


RectangleTest.java


 / Published in: Java
 

URL: http://nunnone.com

This is a test driver class. It instantiates a few rectangles with varying lengths and widths and then prints their areas and perimeters.

What is odd is the output I get from NetBeans for the "stupid" rectangle. The program prints its value as "1280.3200000000002".

  1. public class RectangleTest {
  2.  
  3. public static void main(String[] args) {
  4. Rectangle small = new Rectangle(2.0,3.0);
  5. Rectangle noArgs = new Rectangle();
  6. Rectangle large = new Rectangle(20.4,30.6);
  7. Rectangle notDouble = new Rectangle(4,6);
  8. Rectangle stupid = new Rectangle(3.2,400.1);
  9.  
  10. System.out.println("Small rectangle: A=" + small.getArea() + " P="+small.getPerimeter());
  11. System.out.println("Default rectangle: A=" + noArgs.getArea() + " P="+noArgs.getPerimeter());
  12. System.out.println("Large rectangle: A=" + large.getArea() + " P="+large.getPerimeter());
  13. System.out.println("Rectangle not written as double: A=" + notDouble.getArea() + " P="+notDouble.getPerimeter());
  14. System.out.println("Stupidly proportioned rectangle: A=" + stupid.getArea() + " P="+stupid.getPerimeter());
  15. }
  16.  
  17. }

Report this snippet  

You need to login to post a comment.