From a7b6bab2636afb9333f4696ee9f43f3517caa7ea Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Tue, 10 Sep 2024 17:31:30 +0800 Subject: [PATCH] Tutorial 2 --- .gitignore | 2 + NTU-OOP-Tuts.iml | 8 +++ T2Q1-1/T2Q1-1.iml | 11 ++++ T2Q1-1/src/Circle.java | 38 ++++++++++++++ T2Q1-1/src/CircleApp.java | 38 ++++++++++++++ T2Q1-2/T2Q1-2.iml | 11 ++++ T2Q1-2/src/Circle.java | 38 ++++++++++++++ T2Q1-2/src/CircleApp.java | 38 ++++++++++++++ T2Q1-3/T2Q1-3.iml | 11 ++++ T2Q1-3/src/Circle.java | 38 ++++++++++++++ T2Q1-3/src/CircleApp.java | 49 ++++++++++++++++++ T2Q1-4/T2Q1-4.iml | 11 ++++ T2Q1-4/src/Circle.java | 38 ++++++++++++++ T2Q1-4/src/CircleApp.java | 53 ++++++++++++++++++++ T2Q1-5/T2Q1-5.iml | 11 ++++ T2Q1-5/src/Circle.java | 38 ++++++++++++++ T2Q1-5/src/CircleApp.java | 48 ++++++++++++++++++ T2Q1-extra1/T2Q1-extra1.iml | 11 ++++ T2Q1-extra1/src/Circle.java | 38 ++++++++++++++ T2Q1-extra1/src/CircleApp.java | 50 +++++++++++++++++++ T2Q1-extra2/T2Q1-extra2.iml | 11 ++++ T2Q1-extra2/src/Circle.java | 38 ++++++++++++++ T2Q1-extra2/src/CircularApp.java | 86 ++++++++++++++++++++++++++++++++ T2Q1-extra2/src/Elipse.java | 57 +++++++++++++++++++++ T2Q1-extra3/T2Q1-extra3.iml | 11 ++++ T2Q1-extra3/src/Circle.java | 38 ++++++++++++++ T2Q1-extra3/src/Circular.java | 7 +++ T2Q1-extra3/src/CircularApp.java | 70 ++++++++++++++++++++++++++ T2Q1-extra3/src/Elipse.java | 57 +++++++++++++++++++++ T2Q2/T2Q2.iml | 11 ++++ T2Q2/src/Dice.java | 21 ++++++++ T2Q2/src/DiceApp.java | 23 +++++++++ 32 files changed, 1010 insertions(+) create mode 100644 .gitignore create mode 100644 NTU-OOP-Tuts.iml create mode 100644 T2Q1-1/T2Q1-1.iml create mode 100644 T2Q1-1/src/Circle.java create mode 100644 T2Q1-1/src/CircleApp.java create mode 100644 T2Q1-2/T2Q1-2.iml create mode 100644 T2Q1-2/src/Circle.java create mode 100644 T2Q1-2/src/CircleApp.java create mode 100644 T2Q1-3/T2Q1-3.iml create mode 100644 T2Q1-3/src/Circle.java create mode 100644 T2Q1-3/src/CircleApp.java create mode 100644 T2Q1-4/T2Q1-4.iml create mode 100644 T2Q1-4/src/Circle.java create mode 100644 T2Q1-4/src/CircleApp.java create mode 100644 T2Q1-5/T2Q1-5.iml create mode 100644 T2Q1-5/src/Circle.java create mode 100644 T2Q1-5/src/CircleApp.java create mode 100644 T2Q1-extra1/T2Q1-extra1.iml create mode 100644 T2Q1-extra1/src/Circle.java create mode 100644 T2Q1-extra1/src/CircleApp.java create mode 100644 T2Q1-extra2/T2Q1-extra2.iml create mode 100644 T2Q1-extra2/src/Circle.java create mode 100644 T2Q1-extra2/src/CircularApp.java create mode 100644 T2Q1-extra2/src/Elipse.java create mode 100644 T2Q1-extra3/T2Q1-extra3.iml create mode 100644 T2Q1-extra3/src/Circle.java create mode 100644 T2Q1-extra3/src/Circular.java create mode 100644 T2Q1-extra3/src/CircularApp.java create mode 100644 T2Q1-extra3/src/Elipse.java create mode 100644 T2Q2/T2Q2.iml create mode 100644 T2Q2/src/Dice.java create mode 100644 T2Q2/src/DiceApp.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..562b66d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +out/ \ No newline at end of file diff --git a/NTU-OOP-Tuts.iml b/NTU-OOP-Tuts.iml new file mode 100644 index 0000000..9a5cfce --- /dev/null +++ b/NTU-OOP-Tuts.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/T2Q1-1/T2Q1-1.iml b/T2Q1-1/T2Q1-1.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-1/T2Q1-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-1/src/Circle.java b/T2Q1-1/src/Circle.java new file mode 100644 index 0000000..46edd0b --- /dev/null +++ b/T2Q1-1/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double rad) + { + radius = rad; + } + public void setRadius(double rad) + { + radius = rad; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-1/src/CircleApp.java b/T2Q1-1/src/CircleApp.java new file mode 100644 index 0000000..bfd1443 --- /dev/null +++ b/T2Q1-1/src/CircleApp.java @@ -0,0 +1,38 @@ +import java.util.Scanner; +public class CircleApp +{ + public static void main(String[] args) + { + Circle cir = null; + int option; + double radius = 0.0; + Scanner sc = new Scanner(System.in); + do { + System.out.println("==== Circle Computation ====="); + System.out.println("|1. Create a new circle |"); + System.out.println("|2. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + option = sc.nextInt(); + if (option == 1) + { + System.out.println("Enter the radius to compute the area and circumference"); + radius = sc.nextDouble(); + cir = new Circle(radius); + System.out.println("A new circle is created"); + } + else if (option == 2) + { + cir.printArea(); + + } + else if (option == 3) + { + cir.printCircumference(); + } + } while (option < 4); + System.out.println("Thank you!!"); + } +} diff --git a/T2Q1-2/T2Q1-2.iml b/T2Q1-2/T2Q1-2.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-2/T2Q1-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-2/src/Circle.java b/T2Q1-2/src/Circle.java new file mode 100644 index 0000000..46edd0b --- /dev/null +++ b/T2Q1-2/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double rad) + { + radius = rad; + } + public void setRadius(double rad) + { + radius = rad; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-2/src/CircleApp.java b/T2Q1-2/src/CircleApp.java new file mode 100644 index 0000000..3ea97f6 --- /dev/null +++ b/T2Q1-2/src/CircleApp.java @@ -0,0 +1,38 @@ +import java.util.Scanner; +public class CircleApp +{ + public static void main(String[] args) + { + Circle cir = null; + int option; + double radius = 0.0; + Scanner sc = new Scanner(System.in); + do { + System.out.println("==== Circle Computation ====="); + System.out.println("|1. Create a new circle |"); + System.out.println("|2. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + option = sc.nextInt(); + if (option == 1) + { + System.out.println("Enter the radius to compute the area and circumference"); + radius = sc.nextDouble(); + cir = new Circle(radius); + System.out.println("A new circle is created"); + } + else if (option == 2 && cir != null) + { + cir.printArea(); + + } + else if (option == 3 && cir != null) + { + cir.printCircumference(); + } + } while (option < 4); + System.out.println("Thank you!!"); + } +} diff --git a/T2Q1-3/T2Q1-3.iml b/T2Q1-3/T2Q1-3.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-3/T2Q1-3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-3/src/Circle.java b/T2Q1-3/src/Circle.java new file mode 100644 index 0000000..46edd0b --- /dev/null +++ b/T2Q1-3/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double rad) + { + radius = rad; + } + public void setRadius(double rad) + { + radius = rad; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-3/src/CircleApp.java b/T2Q1-3/src/CircleApp.java new file mode 100644 index 0000000..d0b9448 --- /dev/null +++ b/T2Q1-3/src/CircleApp.java @@ -0,0 +1,49 @@ +import java.util.Scanner; +public class CircleApp +{ + public static void main(String[] args) + { + Circle cir = null; + int option; + double radius = 0.0; + Scanner sc = new Scanner(System.in); + do { + System.out.println("==== Circle Computation ====="); + System.out.println("|1. Create a new circle |"); + System.out.println("|2. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + option = sc.nextInt(); + if (option == 1) + { + System.out.println("Enter the radius to compute the area and circumference"); + radius = sc.nextDouble(); + cir = new Circle(radius); + System.out.println("A new circle is created"); + } + else if (option == 2) + { + if (cir == null) { + System.out.println("Create a circle first."); + } + else + { + cir.printArea(); + } + } + else if (option == 3) + { + if (cir == null) { + System.out.println("Create a circle first."); + } + else + { + cir.printCircumference(); + } + } + } while (option < 4); + System.out.println("Thank you!!"); + } +} diff --git a/T2Q1-4/T2Q1-4.iml b/T2Q1-4/T2Q1-4.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-4/T2Q1-4.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-4/src/Circle.java b/T2Q1-4/src/Circle.java new file mode 100644 index 0000000..46edd0b --- /dev/null +++ b/T2Q1-4/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double rad) + { + radius = rad; + } + public void setRadius(double rad) + { + radius = rad; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-4/src/CircleApp.java b/T2Q1-4/src/CircleApp.java new file mode 100644 index 0000000..90b4803 --- /dev/null +++ b/T2Q1-4/src/CircleApp.java @@ -0,0 +1,53 @@ +import java.util.Scanner; +public class CircleApp +{ + public static void main(String[] args) + { + Circle cir = null; + int option; + double radius = 0.0; + Scanner sc = new Scanner(System.in); + do { + printMenu(); + option = sc.nextInt(); + if (option == 1) + { + System.out.println("Enter the radius to compute the area and circumference"); + radius = sc.nextDouble(); + cir = new Circle(radius); + System.out.println("A new circle is created"); + } + else if (option == 2) + { + if (cir == null) { + System.out.println("Create a circle first."); + } + else + { + cir.printArea(); + } + } + else if (option == 3) + { + if (cir == null) { + System.out.println("Create a circle first."); + } + else + { + cir.printCircumference(); + } + } + } while (option < 4); + System.out.println("Thank you!!"); + } + + private static void printMenu() { + System.out.println("==== Circle Computation ====="); + System.out.println("|1. Create a new circle |"); + System.out.println("|2. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + } +} diff --git a/T2Q1-5/T2Q1-5.iml b/T2Q1-5/T2Q1-5.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-5/T2Q1-5.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-5/src/Circle.java b/T2Q1-5/src/Circle.java new file mode 100644 index 0000000..59036c7 --- /dev/null +++ b/T2Q1-5/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double radius) + { + this.radius = radius; + } + public void setRadius(double radius) + { + this.radius = radius; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-5/src/CircleApp.java b/T2Q1-5/src/CircleApp.java new file mode 100644 index 0000000..cbbc11a --- /dev/null +++ b/T2Q1-5/src/CircleApp.java @@ -0,0 +1,48 @@ +import java.util.Scanner; +public class CircleApp +{ + public static void main(String[] args) + { + Circle circle = null; + int option; + Scanner scanner = new Scanner(System.in); + do { + printMenu(); + option = scanner.nextInt(); + if (option == 1) circle = createCircle(scanner); + else if (option == 2) printArea(circle); + else if (option == 3) printCircumference(circle); + } while (option < 4); + System.out.println("Thank you!!"); + } + + private static void printCircumference(Circle circle) { + if (circle == null) System.out.println("Create a circle first."); + else circle.printCircumference(); + } + + private static void printArea(Circle circle) { + if (circle == null) System.out.println("Create a circle first."); + else circle.printArea(); + } + + 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. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + } +} diff --git a/T2Q1-extra1/T2Q1-extra1.iml b/T2Q1-extra1/T2Q1-extra1.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-extra1/T2Q1-extra1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-extra1/src/Circle.java b/T2Q1-extra1/src/Circle.java new file mode 100644 index 0000000..59036c7 --- /dev/null +++ b/T2Q1-extra1/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double radius) + { + this.radius = radius; + } + public void setRadius(double radius) + { + this.radius = radius; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-extra1/src/CircleApp.java b/T2Q1-extra1/src/CircleApp.java new file mode 100644 index 0000000..389c665 --- /dev/null +++ b/T2Q1-extra1/src/CircleApp.java @@ -0,0 +1,50 @@ +import java.util.Scanner; +public class CircleApp +{ + // This is a static variable. It will retain its value alongside class CircleApp + private static Circle circle = 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) printArea(); + else if (option == 3) printCircumference(); + } while (option < 4); + System.out.println("Thank you!!"); + } + + private static void printCircumference() { + if (circle == null) System.out.println("Create a circle first."); + else circle.printCircumference(); + } + + private static void printArea() { + if (circle == null) System.out.println("Create a circle first."); + else circle.printArea(); + } + + 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. Print Area |"); + System.out.println("|3. Print circumference |"); + System.out.println("|4. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-3):"); + } +} diff --git a/T2Q1-extra2/T2Q1-extra2.iml b/T2Q1-extra2/T2Q1-extra2.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-extra2/T2Q1-extra2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-extra2/src/Circle.java b/T2Q1-extra2/src/Circle.java new file mode 100644 index 0000000..59036c7 --- /dev/null +++ b/T2Q1-extra2/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double radius) + { + this.radius = radius; + } + public void setRadius(double radius) + { + this.radius = radius; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-extra2/src/CircularApp.java b/T2Q1-extra2/src/CircularApp.java new file mode 100644 index 0000000..371ed78 --- /dev/null +++ b/T2Q1-extra2/src/CircularApp.java @@ -0,0 +1,86 @@ +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):"); + } +} diff --git a/T2Q1-extra2/src/Elipse.java b/T2Q1-extra2/src/Elipse.java new file mode 100644 index 0000000..ae88c63 --- /dev/null +++ b/T2Q1-extra2/src/Elipse.java @@ -0,0 +1,57 @@ +public class Elipse +{ + private double radius; + private double smallRadius; + private double largeRadius; + private static final double PI = 3.14159; + + public Elipse(double smallRadius, double largeRadius) + { + this.smallRadius = smallRadius; + this.largeRadius = largeRadius; + } + public void setSmallRadius(double radius) + { + this.smallRadius = radius; + } + public double getSmallRadius() + { + return smallRadius; + } + public void setLargeRadius(double radius) + { + this.largeRadius = radius; + } + public double getLargeRadius() + { + return largeRadius; + } + public double area() + { + return PI * smallRadius * largeRadius; + } + public double circumference() + { + double a = smallRadius; + double b = largeRadius; + double h = (a - b) * (a - b) / ((a + b) * (a + b)); + double leftside = PI * (smallRadius + largeRadius); + double rightside = 1 + (3 * h)/(10 + Math.sqrt(4 - 3 * h)); + + return leftside * rightside; + } + public void printArea() + { + System.out.println("Area of elipse"); + System.out.println("Small Radius: " + smallRadius); + System.out.println("Large Radius: " + largeRadius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of elipse"); + System.out.println("Small Radius: " + smallRadius); + System.out.println("Large Radius: " + largeRadius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-extra3/T2Q1-extra3.iml b/T2Q1-extra3/T2Q1-extra3.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q1-extra3/T2Q1-extra3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q1-extra3/src/Circle.java b/T2Q1-extra3/src/Circle.java new file mode 100644 index 0000000..3e36543 --- /dev/null +++ b/T2Q1-extra3/src/Circle.java @@ -0,0 +1,38 @@ +public class Circle implements Circular +{ + private double radius; + private static final double PI = 3.14159; + + public Circle(double radius) + { + this.radius = radius; + } + public void setRadius(double radius) + { + this.radius = radius; + } + public double getRadius() + { + return radius; + } + public double area() + { + return PI * radius * radius; + } + public double circumference() + { + return 2 * PI * radius; + } + public void printArea() + { + System.out.println("Area of circle"); + System.out.println("Radius: " + radius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of circle"); + System.out.println("Radius: " + radius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q1-extra3/src/Circular.java b/T2Q1-extra3/src/Circular.java new file mode 100644 index 0000000..04ac82b --- /dev/null +++ b/T2Q1-extra3/src/Circular.java @@ -0,0 +1,7 @@ +public interface Circular +{ + public double area(); + public double circumference(); + public void printArea(); + public void printCircumference(); +} \ No newline at end of file diff --git a/T2Q1-extra3/src/CircularApp.java b/T2Q1-extra3/src/CircularApp.java new file mode 100644 index 0000000..aee7d38 --- /dev/null +++ b/T2Q1-extra3/src/CircularApp.java @@ -0,0 +1,70 @@ +import java.util.Scanner; +public class CircularApp +{ + // This is a static variable. It will retain its value alongside class CircleApp + private static Circular circular = null; + + public static void main(String[] args) + { + int option; + Scanner scanner = new Scanner(System.in); + do { + printMenu(); + option = scanner.nextInt(); + if (option == 1) circular = createCircle(scanner); + else if (option == 2) circular = createElipse(scanner); + else if (option == 3) printCircleArea(); + else if (option == 4) printCircleCircumference(); + } while (option < 5); + System.out.println("Thank you!!"); + } + + private static void printCircleCircumference() { + if (circular == null) System.out.println("Create a circle or elipse first."); + else circular.printCircumference(); + } + + private static void printCircleArea() { + if (circular == null) System.out.println("Create a circle or elipse first."); + else circular.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 elise |"); + System.out.println("|3. Print Area |"); + System.out.println("|4. Print circumference |"); + System.out.println("|5. Quit |"); + System.out.println("============================="); + System.out.println("Choose option (1-4):"); + } +} diff --git a/T2Q1-extra3/src/Elipse.java b/T2Q1-extra3/src/Elipse.java new file mode 100644 index 0000000..d880cb3 --- /dev/null +++ b/T2Q1-extra3/src/Elipse.java @@ -0,0 +1,57 @@ +public class Elipse implements Circular +{ + private double radius; + private double smallRadius; + private double largeRadius; + private static final double PI = 3.14159; + + public Elipse(double smallRadius, double largeRadius) + { + this.smallRadius = smallRadius; + this.largeRadius = largeRadius; + } + public void setSmallRadius(double radius) + { + this.smallRadius = radius; + } + public double getSmallRadius() + { + return smallRadius; + } + public void setLargeRadius(double radius) + { + this.largeRadius = radius; + } + public double getLargeRadius() + { + return largeRadius; + } + public double area() + { + return PI * smallRadius * largeRadius; + } + public double circumference() + { + double a = smallRadius; + double b = largeRadius; + double h = (a - b) * (a - b) / ((a + b) * (a + b)); + double leftside = PI * (smallRadius + largeRadius); + double rightside = 1 + (3 * h)/(10 + Math.sqrt(4 - 3 * h)); + + return leftside * rightside; + } + public void printArea() + { + System.out.println("Area of elipse"); + System.out.println("Small Radius: " + smallRadius); + System.out.println("Large Radius: " + largeRadius); + System.out.println("Area: " + area()); + } + public void printCircumference() + { + System.out.println("Circumference of elipse"); + System.out.println("Small Radius: " + smallRadius); + System.out.println("Large Radius: " + largeRadius); + System.out.println("Circumference: " + circumference()); + } +} \ No newline at end of file diff --git a/T2Q2/T2Q2.iml b/T2Q2/T2Q2.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T2Q2/T2Q2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T2Q2/src/Dice.java b/T2Q2/src/Dice.java new file mode 100644 index 0000000..1ef68f5 --- /dev/null +++ b/T2Q2/src/Dice.java @@ -0,0 +1,21 @@ +public class Dice +{ + private int valueOfDice=1; + + public Dice() + { + setDiceValue(); + } + public void setDiceValue() + { + valueOfDice = (int)( Math.random() * 6) + 1; + } + public int getDiceValue() + { + return valueOfDice; + } + public void printDiceValue() + { + System.out.println("Current Value is " + valueOfDice); + } +} diff --git a/T2Q2/src/DiceApp.java b/T2Q2/src/DiceApp.java new file mode 100644 index 0000000..a0f479f --- /dev/null +++ b/T2Q2/src/DiceApp.java @@ -0,0 +1,23 @@ +import java.util.Scanner; +public class DiceApp +{ + public static void main(String[] args) + { + int total, rollOne, rollTwo; + + Scanner sc = new Scanner(System.in); + System.out.println("Press to roll the first dice"); + sc.next(); + Dice d1 = new Dice(); + rollOne = d1.getDiceValue(); + d1.printDiceValue(); + + System.out.println("Press to roll second dice"); + sc.next(); + Dice d2 = new Dice(); + rollTwo = d2.getDiceValue(); + d2.printDiceValue(); + total = rollOne + rollTwo; + System.out.println("Your total number is: " + total); + } +}