Using Dr Java Write a method that that takes two input param
Using Dr. Java,
Write a method that that takes two input parameter representing the lengths of the sides of a rectangle, and returns the perimeter of the rectangle.
Write a method that that takes two input parameters representing the lengths of the sides of a rectangle, and returns the area of the rectangle.
Now modify main to ask the user for the input of the sides of the rectangle, then calculate the area and perimeter of the rectangle, and then display the results.
Solution
// RectAreaPerimeter.java
import java.util.Scanner;
class RectAreaPerimeter
{
public static double getArea(double length, double breadth)
{
return length * breadth;
}
public static double getPerimeter(double length, double breadth)
{
return 2 * (length + breadth);
}
public static void main(String arg[])
{
Scanner scanner = new Scanner(System.in);
RectAreaPerimeter rect = new RectAreaPerimeter();
System.out.print(\"Enter length: \");
double length = scanner.nextDouble();
System.out.print(\"Enter length: \");
double breadth = scanner.nextDouble();
System.out.println(\"Area: \" + rect.getArea(length,breadth));
System.out.println(\"Perimeter: \" + rect.getPerimeter(length,breadth));
}
}
/*
output:
Enter length: 3.4
Enter length: 5.6
Area: 19.04
Perimeter: 18.0
*/
