Пример #1
0
  public static void main(String[] args) {
    // square
    Square square = new Square(5.6);
    System.out.println("area: " + square.getArea());
    System.out.println("perimeter: " + square.getPerimeter());

    // rectangle
    Rectangle rectangle = new Rectangle(1.2, 3.4);
    System.out.println("area: " + rectangle.getArea());
    System.out.println("perimeter: " + rectangle.getPerimeter());

    // circle
    Circle circle = new Circle(1.2);
    System.out.println("area: " + circle.getArea());
    System.out.println("perimeter: " + circle.getPerimeter());

    // triangle
    Triangle triangle = new Triangle(1.2, 1.2, 1.2);
    System.out.println("area: " + triangle.getArea());
    System.out.println("perimeter: " + triangle.getPerimeter());

    // shape
    Shape shape = new Circle(1);
    System.out.println("area: " + shape.getArea());
    System.out.println("perimeter: " + shape.getPerimeter());
  }
Пример #2
0
  public static void main(String[] args) {
    Square square = new Square(4);
    Triangle triangle = new Triangle(3, 2, 3, 7.2, 5, 4.5);
    Rectangle rectangle = new Rectangle(6, 4);
    Circle circle = new Circle(8.5);

    System.out.println(square.getArea());
    System.out.println(triangle.getArea());
    System.out.println(triangle.getWidth());
    System.out.println(triangle.getHeight());
    System.out.println(rectangle.getArea());
    System.out.println(circle.getArea());
  }
Пример #3
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);
  }
  private static boolean testTriangle() {
    boolean pass = true;
    int test = 1;
    Triangle tri;
    Point a = new Point(4, 0);
    Point b = new Point(-2, -1);
    Point c = new Point(1, 4);

    System.out.println("Triangle tests...");

    tri = new Triangle(a, b, c, Color.cyan, false);
    pass &= test(approx(tri.getArea(), 13.5, 0.000001), test++);
    pass &= test(tri.getColor().equals(Color.cyan), test++);

    tri.setColor(Color.black);
    pass &= test(tri.getColor().equals(Color.black), test++);
    pass &= test(!tri.getFilled(), test++);

    tri.setFilled(true);
    pass &= test(tri.getFilled(), test++);
    pass &= test(tri.getVertexA().equals(new Point(4, 0)), test++);
    pass &= test(tri.getVertexB().equals(new Point(-2, -1)), test++);
    pass &= test(tri.getVertexC().equals(new Point(1, 4)), test++);

    a = new Point(7, -3);
    tri.setVertexA(a);
    pass &= test(tri.getVertexA().equals(new Point(7, -3)), test++);

    b = new Point(13, 56);
    tri.setVertexB(b);
    pass &= test(tri.getVertexB().equals(new Point(13, 56)), test++);

    c = new Point(-3, 23);
    tri.setVertexC(c);
    pass &= test(tri.getVertexC().equals(new Point(-3, 23)), test++);
    pass &= test(approx(tri.getArea(), 373.0, 0.000001), test++);

    tri.move(new Point(-5, -7));
    pass &= test(tri.getVertexA().equals(new Point(2, -10)), test++);
    pass &= test(tri.getVertexB().equals(new Point(8, 49)), test++);
    pass &= test(tri.getVertexC().equals(new Point(-8, 16)), test++);

    a = new Point(2, -10);
    b = new Point(8, 49);
    c = new Point(-8, 16);

    Triangle tri2 = new Triangle(a, b, c, Color.black, true);
    pass &= test(tri.equals(tri2), test++);

    Triangle tri3 = new Triangle(new Point(3, -9), b, c, Color.black, false);
    pass &= test(!tri2.equals(tri3), test++);

    tri3 = new Triangle(a, new Point(7, 48), c, Color.black, false);
    pass &= test(!tri2.equals(tri3), test++);

    tri3 = new Triangle(a, b, new Point(-7, 17), Color.black, false);
    pass &= test(!tri2.equals(tri3), test++);

    tri3 = new Triangle(a, b, c, Color.red, false);
    pass &= test(!tri2.equals(tri3), test++);

    pass &= test(!tri2.equals(null), test++);
    pass &= test(!tri2.equals(new String("Whatever")), test++);

    return pass;
  }
Пример #5
0
 @Test
 public void AreaTest() {
   Triangle A = new Triangle(3, 4, 5);
   Triangle B = new Triangle(6, 8, 10);
   assertTrue(A.getArea() == B.getArea());
 }