-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from sricharan200/sricharan200-patch-2
Implemented Area Calculator using Java
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |