NTU-OOP-Tuts-2024/T2Q1-extra2/src/CircularApp.java

87 lines
3.3 KiB
Java
Raw Normal View History

2024-09-10 17:31:30 +08:00
import java.util.Scanner;
public class CircularApp
{
// This is a static variable. It will retain its value alongside class CircleApp
private static Circle circle = null;
// This is a static variable. It will retain its value alongside class CircleApp
private static Elipse elipse = null;
public static void main(String[] args)
{
int option;
Scanner scanner = new Scanner(System.in);
do {
printMenu();
option = scanner.nextInt();
if (option == 1) circle = createCircle(scanner);
else if (option == 2) elipse = createElipse(scanner);
else if (option == 3) printCircleArea();
else if (option == 4) printElipseArea();
else if (option == 5) printCircleCircumference();
else if (option == 6) printElipseCircumference();
} while (option < 7);
System.out.println("Thank you!!");
}
private static void printElipseCircumference() {
if (elipse == null) System.out.println("Create a elipse first.");
else elipse.printCircumference();
}
private static void printCircleCircumference() {
if (circle == null) System.out.println("Create a circle first.");
else circle.printCircumference();
}
private static void printElipseArea() {
if (elipse == null) System.out.println("Create a elipse first.");
else elipse.printArea();
}
private static void printCircleArea() {
if (circle == null) System.out.println("Create a circle first.");
else circle.printArea();
}
private static Elipse createElipse(Scanner scanner) {
double smallRadius = 0.0;
double largeRadius = 0.0;
Elipse elipse;
System.out.println("Enter the radius 1 to compute the area and circumference");
smallRadius = scanner.nextDouble();
System.out.println("Enter the radius 2 to compute the area and circumference");
largeRadius = scanner.nextDouble();
if (smallRadius > largeRadius) {
double tempRadius = smallRadius;
smallRadius = largeRadius;
largeRadius = tempRadius;
}
elipse = new Elipse(smallRadius, largeRadius);
System.out.println("A new elipse is created");
return elipse;
}
private static Circle createCircle(Scanner scanner) {
double radius = 0.0;
Circle circle;
System.out.println("Enter the radius to compute the area and circumference");
radius = scanner.nextDouble();
circle = new Circle(radius);
System.out.println("A new circle is created");
return circle;
}
private static void printMenu() {
System.out.println("====== Circle Computation =======");
System.out.println("|1. Create a new circle |");
System.out.println("|2. Create a new elipse |");
System.out.println("|3. Print Circle Area |");
System.out.println("|4. Print Elipse Area |");
System.out.println("|5. Print Circle circumference |");
System.out.println("|6. Print Elipse circumference |");
System.out.println("|7. Quit |");
System.out.println("=================================");
System.out.println("Choose option (1-6):");
}
}