Solved programming problem

Calculate Area and Perimeter of a Rectangle in Java.

Source code

Main Method:

 
package online2;

import java.util.Scanner;

/**

 *

 * @author Sabit

 */

public class TestRectangle 

{

    public static void main(String[] args)

    {   

        double l,w;

        Scanner S = new Scanner(System.in);

        

        System.out.printf("Enter length: ");

        l = S.nextDouble();

        

        System.out.printf("Enter Width: ");

        w = S.nextDouble();

        

        Rectangle R = new Rectangle();

        

        

        R.setParameter(l, w);

        

        System.out.println("Area: "+R.getArea());

        System.out.println("Perimeter: "+R.getPerimeter());

    }

}

Rectangle class:

package online2;

/**

 *

 * @author Sabit

 */

public class Rectangle

{

    public double l,w;

    

    public void setParameter(double l, double w)

    {

        this.l = l;

        this.w = w; 

    }

    

    public double getArea()

    {

        double area = w*l;

        

        return area;

    }

    

    public double getPerimeter()

    {

        double peri = (2*(l+w));

       

        return peri; 

    }

}

Output

Enyer length:4

Enter width:3

Area:12.0

Parameter:14.0

Join the discussion

Add your comment