Post

Created by @nathanedwards
 at November 1st 2023, 4:57:04 pm.

AP Computer Science Exam Question

Variables and Constants

Question:

You are developing a program that calculates the area of different shapes. Implement a Java program that contains a main method and the following methods:

  1. A method named calculateAreaOfCircle that takes the radius of a circle as a parameter and returns the area of the circle.
  2. A method named calculateAreaOfRectangle that takes the length and width of a rectangle as parameters and returns the area of the rectangle.
  3. A method named calculateAreaOfTriangle that takes the base and height of a triangle as parameters and returns the area of the triangle.

In the main method, prompt the user to enter the dimensions of a circle, a rectangle, and a triangle, and display their respective areas.

Use the following formulas to calculate the areas:

  • Circle: Area = π * radius^2
  • Rectangle: Area = length * width
  • Triangle: Area = (base * height) / 2

You may assume that the user will only provide valid dimensions (radius, length, width, base, and height).

Write the complete Java program to solve this problem.

Example Output:

Enter the radius of the circle: 4
Enter the length and width of the rectangle: 5 6
Enter the base and height of the triangle: 3 8

Area of the circle: 50.27
Area of the rectangle: 30
Area of the triangle: 12.0

Please write your solution code below:

Answer:

import java.util.Scanner;

public class AreaCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the radius of the circle: ");
        double circleRadius = scanner.nextDouble();

        System.out.print("Enter the length and width of the rectangle: ");
        double rectangleLength = scanner.nextDouble();
        double rectangleWidth = scanner.nextDouble();

        System.out.print("Enter the base and height of the triangle: ");
        double triangleBase = scanner.nextDouble();
        double triangleHeight = scanner.nextDouble();

        double circleArea = calculateAreaOfCircle(circleRadius);
        double rectangleArea = calculateAreaOfRectangle(rectangleLength, rectangleWidth);
        double triangleArea = calculateAreaOfTriangle(triangleBase, triangleHeight);

        System.out.println("\nArea of the circle: " + circleArea);
        System.out.println("Area of the rectangle: " + rectangleArea);
        System.out.println("Area of the triangle: " + triangleArea);
    }

    public static double calculateAreaOfCircle(double radius) {
        return Math.PI * Math.pow(radius, 2);
    }

    public static double calculateAreaOfRectangle(double length, double width) {
        return length * width;
    }

    public static double calculateAreaOfTriangle(double base, double height) {
        return (base * height) / 2;
    }
}

Explanation:

  1. The Scanner class is imported to allow user input.
  2. In the main method, we prompt the user to enter the dimensions of the circle, rectangle, and triangle.
  3. User inputs are stored in respective variables.
  4. Using the provided formulas, calculateAreaOfCircle, calculateAreaOfRectangle, and calculateAreaOfTriangle methods calculate the areas.
  5. The computed areas are stored in variables.
  6. Finally, the areas of the circle, rectangle, and triangle are displayed on the console.