In: Computer Science
Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height.
Area and Perimeter Calculator Enter width: 3
Enter height: 5
If your shape is a rectangle, its area is 15.0 Its perimeter is 16.0
If your shape is a triangle, its area is 7.5
If it is a right triangle, its perimeter is 8.0 + square root of 34.0
Needs to be in Java
Explanation:
Here is the code which has the Scanner object and takes the width and height from the user.
Then it prints the area and perimeter of rectangle and triangle.
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Area and
Perimeter Calculator");
System.out.print("Enter width:
");
double width =
sc.nextDouble();
System.out.print("Enter height:
");
double height =
sc.nextDouble();
System.out.println("If your shape
is a rectangle, its area is "+(width*height));
System.out.println("Its Perimeter
is "+(2*(width+height)));
System.out.println("If your shape
is a triangle, its area is "+((width*height)/2.0));
System.out.println("If it is a
right triangle, its perimeter is "+(width+height)+" + square root
of "+(width*width + height*height));
}
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!