Hackerss.com

hackerss
hackerss

Posted on

Create a class for a rectangle in Java

Create a class for a rectangle - Java:


/**
 * This class creates a rectangle.
 * @author 
 * @version 1.0
 */
public class Rectangle {
   /**
   * This method calculates the area of a rectangle.
   * @param length the length of the rectangle.
   * @param width the width of the rectangle.
   * @return double Obtains the area of the rectangle.
   */   
   public double area(double length, double width) {
       //Variable to store the area
       double area = 0;
       //Calculate the area
       area = length * width;
       //Return the area
       return area;
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      Rectangle  rectangle= new Rectangle ();
      //Example
      double area = rectangle.area(10, 20);
      //Output
      System.out.println("Area of 10 and 20 is :" + area); //200
   }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)