Java Arithmetic Operator calculate area and perimeter of a rectangle

Question

We would like to write a program to display the area and perimeter of a rectangle with the width of 4.5 and height of 7.9.

You can use the following formula:

area  =  width  *  height 

Code structure you can use:

public class Main {
  public static void main(String[] args) {
    System.out.println("Area = ");
    
    System.out.println("Perimeter = ");
  
  }
}


public class Main {
  public static void main(String[] args) {
    System.out.println("Area = ");
    System.out.println(4.5 * 7.9);
    System.out.println("Perimeter = ");
    System.out.println((4.5 + 7.9) * 2);
  }
}

Note

We can create method for the calculation:



public class Main {
  public static void main(String[] args) {
    System.out.println("area: " + area(4.5, 7.9));
    System.out.println("perimeter: " + perimeter(4.5, 7.9));
  }/*from   w w  w.  j a  v  a2s .co  m*/

  private static double area(double width, double height) {
    return width * height;
  }

  private static double perimeter(double width, double height) {
    return 2 * (width + height);
  }
}



PreviousNext

Related