Assignment
import java.util.Scanner;
public class Kelculator {
// Method to calculate area of a circle
public static void calculateCircleArea(double radius) {
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is: " + area);
}
// Method to calculate area of a square
public static void calculateSquareArea(double side) {
double area = side * side;
System.out.println("The area of the square is: " + area);
}
// Method to calculate area of a rectangle
public static void calculateRectangleArea(double length, double width) {
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
// Method to calculate area of a triangle
public static void calculateTriangleArea(double base, double height) {
double area = 0.5 * base * height;
System.out.println("The area of the triangle is: " + area);
}
// Method to calculate area of a trapezoid
public static void calculateTrapezoidArea(double base1, double base2, double height) {
double area = 0.5 * (base1 + base2) * height;
System.out.println("The area of the trapezoid is: " + area);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose a shape to calculate area:");
System.out.println("1. Circle");
System.out.println("2. Square");
System.out.println("3. Rectangle");
System.out.println("4. Triangle");
System.out.println("5. Trapezoid");
System.out.print("Enter the number of your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: // Circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
calculateCircleArea(radius);
break;
case 2: // Square
System.out.print("Enter the side length of the square: ");
double side = scanner.nextDouble();
calculateSquareArea(side);
break;
case 3: // Rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
calculateRectangleArea(length, width);
break;
case 4: // Triangle
System.out.print("Enter the base length of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
calculateTriangleArea(base, height);
break;
case 5: // Trapezoid
System.out.print("Enter the length of the first base of the trapezoid: ");
double base1 = scanner.nextDouble();
System.out.print("Enter the length of the second base of the trapezoid: ");
double base2 = scanner.nextDouble();
System.out.print("Enter the height of the trapezoid: ");
double heightTrapezoid = scanner.nextDouble();
calculateTrapezoidArea(base1, base2, heightTrapezoid);
break;
default:
System.out.println("Invalid choice. Please select a number between 1 and 5.");
}
Comments
Post a Comment