26.4k views
1 vote
1 Write 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. Depending on that input - the program must call either the Triangle method or the Rectangle method. Each of those methods must receive 2 input fields from the user - one for base and one for height in order to calculate the area and then to print that area and the height and base from inside the method.

User Ivanna
by
5.9k points

1 Answer

7 votes

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.

Step-by-step 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);

}

}

User BenjaminB
by
6.0k points