Пример #1
0
 public static void main(String[] args) {
   Object[] objects = {new Triangle(5, 5), new Square(3), new Circle(3)};
   // compute areas
   for (Object o : objects) {
     if (o instanceof Triangle) {
       Triangle t = (Triangle) o;
       System.out.println(t.computeArea());
     }
     if (o instanceof Square) {
       Square s = (Square) o;
       System.out.println(s.computeArea());
     }
     if (o instanceof Circle) {
       Circle c = (Circle) o;
       System.out.println(c.computeArea());
     }
   }
 }
Пример #2
0
  // App class constructor
  public App() {
    // Print out a test message to the console to confirm running program
    // Uses java.lang library System.out to do priting to console
    System.out.println("App constructor success!");

    // Delare and instantiate objects
    MainWindow myWindow = new MainWindow();
    InputBox inBox = new InputBox(myWindow);
    OutputBox outBox = new OutputBox(myWindow);

    // Render the main window
    myWindow.show();

    // Declare and initialise variables to default values
    double baseDimension = -1;
    double heightDimension = -1;
    double area = -1;

    // Get input form the user
    baseDimension = inBox.getDouble("Please enter the size of the base in mm: ");
    heightDimension = inBox.getDouble("Please enter the height in mm: ");

    // Debug output
    System.out.println("The base entered was: " + baseDimension);
    System.out.println("The height entered was: " + heightDimension);

    // Create the Triangle object
    Triangle myTriangle = new Triangle();
    // Set values from user
    myTriangle.setBaseDimension(baseDimension);
    myTriangle.setHeightDimension(heightDimension);
    // Calculate the area
    myTriangle.computeArea();
    area = myTriangle.getArea();

    // Show the feedback dialog
    outBox.show();

    // Return the calculated area
    outBox.println("The triangle area was computed to be: " + area);
  }
Пример #3
0
  public static void main(String[] args) {

    Object[] objects = {new Triangle(5, 5), new Square(3), new Circle(3)};
    // compute areas
    for (Object o : objects) {
      if (o instanceof Triangle) {
        System.out.println("The name of the class is : " + o.getClass().getSimpleName());
        Triangle t = (Triangle) o;
        System.out.println("Area of Triangle : " + t.computeArea());
      }
      if (o instanceof Square) {
        System.out.println("The name of the class is : " + o.getClass().getName());
        Square s = (Square) o;

        System.out.println("Area of Rectangle : " + s.computeArea());
      }
      if (o instanceof Circle) {
        Circle c = (Circle) o;
        System.out.println("The name of the class is : " + o.getClass());
        System.out.println("Area of Circle : " + c.computeArea());
      }
    }
  }