Advertisement
campbe13

RectangleDemo app class

Nov 30th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. /**
  3.    This program is an application class,
  4.    It is used to test/drive the Rectangle logic class
  5.    
  6. */
  7.  
  8. public class RectangleDemo
  9. {
  10.    public static void main(String[] args)
  11.    {
  12.       // Create a Rectangle object, passing 5.0 and
  13.       // 15.0 as arguments to the constructor.
  14.       double width = 1, length;
  15.       Rectangle lot, house, gardenShed, box;
  16.       lot = new Rectangle(100, 200);
  17.       house = new Rectangle(40.0, 50.0);
  18.       gardenShed = new Rectangle(4.0, 8.0);
  19.       System.out.println("Lot\narea:\t "+lot.calcArea() +
  20.             "\tperimiter: " + lot.calcPerimeter()+"\n\nBuildings:");
  21.       System.out.println("Garden Shed\n area:\t "+gardenShed.calcArea() +
  22.             "\t\tperimiter: " + gardenShed.calcPerimeter());
  23.       System.out.println("House\n area:\t "+house.calcArea() +
  24.             "\t\tperimiter: " + house.calcPerimeter());
  25.       System.out.println ("\nArea left to build on "+
  26.             (lot.calcArea() - house.calcArea() - gardenShed.calcArea()));
  27.       /* temp
  28.       while (width > 0) {
  29.         if ((width = acceptDub ("Rectangle width:")) >  0) {
  30.             if ((length = acceptDub ("Rectangle length:")) > 0) {
  31.                 box = new Rectangle(length, width);
  32.                 // Display the length.
  33.                 System.out.println("The box's length is " +
  34.                          box.getLength());
  35.                 // Display the width.
  36.                 System.out.println("The box's width is " +
  37.                          box.getWidth());
  38.                 // Display the area.
  39.                 System.out.println("The box's area is " +
  40.                          box.calcArea());
  41.                          // Display the area.
  42.                 System.out.println("The box's perimiter is " +
  43.                          box.calcPerimeter());
  44.                 }  
  45.         }
  46.       }
  47. */   
  48.         System.out.println("Bye! ");
  49.    }//main()
  50.    
  51.    public static double acceptDub(String msg) {
  52.        String numIn =  JOptionPane.showInputDialog(msg);
  53.        double numOut = 0;
  54.        try {
  55.            numOut = Double.parseDouble(numIn);
  56.        } catch (NullPointerException|NumberFormatException e) {
  57.            System.out.println("Invalid number, can't convert");
  58.        }
  59.        return numOut;
  60.    } //acceptDub(0
  61. } // RectangleDemo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement