Respuesta :
A Java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle.
Explanation:
Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle.
Depending on that input - the program must call either the Triangle method or the Rectangle method.
import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
class AreaOfTriangle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double b= s.nextDouble();
System.out.println("Enter the height of the Triangle:");
double h= s.nextDouble();
//Area = (width*height)/2
double area=(b*h)/2;
System.out.println("Area of Triangle is: " + area);
}
}