Skip to content

Commit

Permalink
Merge pull request #309 from sricharan200/sricharan200-patch-2
Browse files Browse the repository at this point in the history
Implemented Area Calculator using Java
  • Loading branch information
diwas7777 authored Nov 28, 2023
2 parents ed2957b + 6a34a1a commit 9222290
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Java/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Scanner;

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

while (true) {
System.out.println("Choose a shape to calculate its area:");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
System.out.println("4. Quit");
System.out.print("Enter your choice (1/2/3/4): ");

int choice = scanner.nextInt();

switch (choice) {
case 1:
calculateCircleArea(scanner);
break;
case 2:
calculateRectangleArea(scanner);
break;
case 3:
calculateTriangleArea(scanner);
break;
case 4:
System.out.println("Exiting the program.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}

// Calculate the area of a circle
private static void calculateCircleArea(Scanner scanner) {
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * Math.pow(radius, 2);
System.out.println("The area of the circle is: " + area);
}

// Calculate the area of a rectangle
private static void calculateRectangleArea(Scanner scanner) {
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();
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
}

// Calculate the area of a triangle
private static void calculateTriangleArea(Scanner scanner) {
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();
double area = 0.5 * base * height;
System.out.println("The area of the triangle is: " + area);
}
}

0 comments on commit 9222290

Please sign in to comment.