In: Computer Science
The total surface area is the sum of the triangular area and rectangular area. Complete the following code to compute the total surface area of the shape. ??????? ???? = 12 ∗ ???? ∗ ????h? + ???? ∗ ????h
This Java program prompts for and reads in the value of height, base, and width in feet.This program uses two methods: Train_area and Rect_area to calculate the area of the triangle and the area of the rectangle, respectively. The following parameters: height, base, and width have been declared as floating-point number on the main method.
The static method declaration, and method call for Trian_area have been shown on the code. The parameters: base, and height have been passed to Trian_area method to calculate the area of the triangular area. The value returned by Trian_area method has been assigned to a variable named area1
Task 1: Now, you need to work on the method named Rect_area which calculates the rectangular area of shape. You need to write static method declaration, and method call for Rect_area method. You need to pass local variable base, and width to theRect_area method.TheRect_area method returns the area of the rectangle to the main. You will assign the value returned by this method to area2. You will print the area of the rectangle on the terminal according to sample output.
Task 2: Now, add rectangle area and triangle area on the main to calculate the total surface area of the shape. Now, print total surface area of the shape on the terminal according to sample output.
Sample output 1:
Enter the value of base, height and width in feet: 10 30 20
Total surface area of the shape: 350.0 square feet
CODE -
import java.util.Scanner;
public class surf_area
{
// Method to calculate triangular area of shape
static float Trian_area(float base, float height)
{
float area = 0.5f * base * height;
return area;
}
// Method to calculate rectangular area of shape
static float Rect_area(float base, float width)
{
float area = base * width;
return area;
}
// Main method
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
float base, height, width;
// Take base, height, and width as input from user
System.out.print("Enter the value of base, height and width in feet: ");
base = keyboard.nextInt();
height = keyboard.nextInt();
width = keyboard.nextInt();
// Call functions to calculate triangular area and rectangular area
float area1 = Trian_area(base, height);
float area2 = Rect_area(base, width);
// Calculate total surface area
float totalSurfaceArea = area1 + area2;
// Display total surface area
System.out.println("Total surface area of the shape: " + totalSurfaceArea + " square feet");
keyboard.close();
}
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.