Exemplo n.º 1
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);
  }
  public static void main(String args[]) {
    // Declare variables
    double aRadius;
    double aVolume;

    // Declare objects
    MainWindow mWindow;
    InputBox iBox;
    OutputBox oBox;

    Sphere aSphere;

    // Create objects
    mWindow = new MainWindow();
    iBox = new InputBox(mWindow);
    oBox = new OutputBox(mWindow);

    aSphere = new Sphere();

    // Use objects
    mWindow.show();

    // get Input
    aRadius = iBox.getDouble("Please enter the radius of the sphere: ");
    aSphere.setTheRadius(aRadius);

    // debug code
    System.out.println("value input:" + aRadius);
    System.out.println("the radius value in the sphere object:" + aSphere.getTheRadius());

    // Process
    aSphere.computeVolume();
    aVolume = aSphere.getTheVolume();

    // Output
    oBox.show();
    oBox.print(" The volume of a sphere with a radius of  " + aRadius + " is " + aVolume);
  }