Refactor Tutorial 2

This commit is contained in:
Marcus Vinicius de Carvalho 2024-09-26 22:05:05 +08:00
parent fe957201e3
commit 9d4f4352a3
16 changed files with 39 additions and 51 deletions

View File

@ -26,7 +26,6 @@ public class CircleApp
else if (option == 2) else if (option == 2)
{ {
cir.printArea(); cir.printArea();
} }
else if (option == 3) else if (option == 3)
{ {

View File

@ -26,7 +26,6 @@ public class CircleApp
else if (option == 2 && cir != null) else if (option == 2 && cir != null)
{ {
cir.printArea(); cir.printArea();
} }
else if (option == 3 && cir != null) else if (option == 3 && cir != null)
{ {

View File

@ -25,7 +25,8 @@ public class CircleApp
} }
else if (option == 2) else if (option == 2)
{ {
if (cir == null) { if (cir == null)
{
System.out.println("Create a circle first."); System.out.println("Create a circle first.");
} }
else else
@ -35,7 +36,8 @@ public class CircleApp
} }
else if (option == 3) else if (option == 3)
{ {
if (cir == null) { if (cir == null)
{
System.out.println("Create a circle first."); System.out.println("Create a circle first.");
} }
else else

View File

@ -19,7 +19,8 @@ public class CircleApp
} }
else if (option == 2) else if (option == 2)
{ {
if (cir == null) { if (cir == null)
{
System.out.println("Create a circle first."); System.out.println("Create a circle first.");
} }
else else
@ -29,7 +30,8 @@ public class CircleApp
} }
else if (option == 3) else if (option == 3)
{ {
if (cir == null) { if (cir == null)
{
System.out.println("Create a circle first."); System.out.println("Create a circle first.");
} }
else else

View File

@ -27,10 +27,9 @@ public class CircleApp
} }
private static Circle createCircle(Scanner scanner) { private static Circle createCircle(Scanner scanner) {
double radius = 0.0;
Circle circle; Circle circle;
System.out.println("Enter the radius to compute the area and circumference"); System.out.println("Enter the radius to compute the area and circumference");
radius = scanner.nextDouble(); double radius = scanner.nextDouble();
circle = new Circle(radius); circle = new Circle(radius);
System.out.println("A new circle is created"); System.out.println("A new circle is created");
return circle; return circle;

View File

@ -11,7 +11,7 @@ public class CircleApp
do { do {
printMenu(); printMenu();
option = scanner.nextInt(); option = scanner.nextInt();
if (option == 1) circle = createCircle(scanner); if (option == 1) createCircle(scanner);
else if (option == 2) printArea(); else if (option == 2) printArea();
else if (option == 3) printCircumference(); else if (option == 3) printCircumference();
} while (option < 4); } while (option < 4);
@ -28,14 +28,11 @@ public class CircleApp
else circle.printArea(); else circle.printArea();
} }
private static Circle createCircle(Scanner scanner) { private static void createCircle(Scanner scanner) {
double radius = 0.0;
Circle circle;
System.out.println("Enter the radius to compute the area and circumference"); System.out.println("Enter the radius to compute the area and circumference");
radius = scanner.nextDouble(); double radius = scanner.nextDouble();
circle = new Circle(radius); circle = new Circle(radius);
System.out.println("A new circle is created"); System.out.println("A new circle is created");
return circle;
} }
private static void printMenu() { private static void printMenu() {

View File

@ -13,8 +13,8 @@ public class CircularApp
do { do {
printMenu(); printMenu();
option = scanner.nextInt(); option = scanner.nextInt();
if (option == 1) circle = createCircle(scanner); if (option == 1) createCircle(scanner);
else if (option == 2) elipse = createElipse(scanner); else if (option == 2) createElipse(scanner);
else if (option == 3) printCircleArea(); else if (option == 3) printCircleArea();
else if (option == 4) printElipseArea(); else if (option == 4) printElipseArea();
else if (option == 5) printCircleCircumference(); else if (option == 5) printCircleCircumference();
@ -43,10 +43,9 @@ public class CircularApp
else circle.printArea(); else circle.printArea();
} }
private static Elipse createElipse(Scanner scanner) { private static void createElipse(Scanner scanner) {
double smallRadius = 0.0; double smallRadius = 0.0;
double largeRadius = 0.0; double largeRadius = 0.0;
Elipse elipse;
System.out.println("Enter the radius 1 to compute the area and circumference"); System.out.println("Enter the radius 1 to compute the area and circumference");
smallRadius = scanner.nextDouble(); smallRadius = scanner.nextDouble();
System.out.println("Enter the radius 2 to compute the area and circumference"); System.out.println("Enter the radius 2 to compute the area and circumference");
@ -58,17 +57,13 @@ public class CircularApp
} }
elipse = new Elipse(smallRadius, largeRadius); elipse = new Elipse(smallRadius, largeRadius);
System.out.println("A new elipse is created"); System.out.println("A new elipse is created");
return elipse;
} }
private static Circle createCircle(Scanner scanner) { private static void createCircle(Scanner scanner) {
double radius = 0.0;
Circle circle;
System.out.println("Enter the radius to compute the area and circumference"); System.out.println("Enter the radius to compute the area and circumference");
radius = scanner.nextDouble(); double radius = scanner.nextDouble();
circle = new Circle(radius); circle = new Circle(radius);
System.out.println("A new circle is created"); System.out.println("A new circle is created");
return circle;
} }
private static void printMenu() { private static void printMenu() {

View File

@ -1,6 +1,5 @@
public class Elipse public class Elipse
{ {
private double radius;
private double smallRadius; private double smallRadius;
private double largeRadius; private double largeRadius;
private static final double PI = 3.14159; private static final double PI = 3.14159;

View File

@ -15,11 +15,11 @@ public class Circle implements Circular
{ {
return radius; return radius;
} }
public double area() public double computeArea()
{ {
return PI * radius * radius; return PI * radius * radius;
} }
public double circumference() public double computeCircumference()
{ {
return 2 * PI * radius; return 2 * PI * radius;
} }
@ -27,12 +27,12 @@ public class Circle implements Circular
{ {
System.out.println("Area of circle"); System.out.println("Area of circle");
System.out.println("Radius: " + radius); System.out.println("Radius: " + radius);
System.out.println("Area: " + area()); System.out.println("Area: " + computeArea());
} }
public void printCircumference() public void printCircumference()
{ {
System.out.println("Circumference of circle"); System.out.println("Circumference of circle");
System.out.println("Radius: " + radius); System.out.println("Radius: " + radius);
System.out.println("Circumference: " + circumference()); System.out.println("Circumference: " + computeCircumference());
} }
} }

View File

@ -1,7 +1,7 @@
public interface Circular public interface Circular
{ {
public double area(); public double computeArea();
public double circumference(); public double computeCircumference();
public void printArea(); public void printArea();
public void printCircumference(); public void printCircumference();
} }

View File

@ -11,8 +11,8 @@ public class CircularApp
do { do {
printMenu(); printMenu();
option = scanner.nextInt(); option = scanner.nextInt();
if (option == 1) circular = createCircle(scanner); if (option == 1) createCircle(scanner);
else if (option == 2) circular = createElipse(scanner); else if (option == 2) createElipse(scanner);
else if (option == 3) printCircleArea(); else if (option == 3) printCircleArea();
else if (option == 4) printCircleCircumference(); else if (option == 4) printCircleCircumference();
} while (option < 5); } while (option < 5);
@ -29,10 +29,9 @@ public class CircularApp
else circular.printArea(); else circular.printArea();
} }
private static Elipse createElipse(Scanner scanner) { private static void createElipse(Scanner scanner) {
double smallRadius = 0.0; double smallRadius = 0.0;
double largeRadius = 0.0; double largeRadius = 0.0;
Elipse elipse;
System.out.println("Enter the radius 1 to compute the area and circumference"); System.out.println("Enter the radius 1 to compute the area and circumference");
smallRadius = scanner.nextDouble(); smallRadius = scanner.nextDouble();
System.out.println("Enter the radius 2 to compute the area and circumference"); System.out.println("Enter the radius 2 to compute the area and circumference");
@ -42,25 +41,21 @@ public class CircularApp
smallRadius = largeRadius; smallRadius = largeRadius;
largeRadius = tempRadius; largeRadius = tempRadius;
} }
elipse = new Elipse(smallRadius, largeRadius); circular = new Elipse(smallRadius, largeRadius);
System.out.println("A new elipse is created"); System.out.println("A new elipse is created");
return elipse;
} }
private static Circle createCircle(Scanner scanner) { private static void createCircle(Scanner scanner) {
double radius = 0.0;
Circle circle;
System.out.println("Enter the radius to compute the area and circumference"); System.out.println("Enter the radius to compute the area and circumference");
radius = scanner.nextDouble(); double radius = scanner.nextDouble();
circle = new Circle(radius); circular = new Circle(radius);
System.out.println("A new circle is created"); System.out.println("A new circle is created");
return circle;
} }
private static void printMenu() { private static void printMenu() {
System.out.println("==== Circle Computation ====="); System.out.println("==== Circle Computation =====");
System.out.println("|1. Create a new circle |"); System.out.println("|1. Create a new circle |");
System.out.println("|2. Create a new elise |"); System.out.println("|2. Create a new elipse |");
System.out.println("|3. Print Area |"); System.out.println("|3. Print Area |");
System.out.println("|4. Print circumference |"); System.out.println("|4. Print circumference |");
System.out.println("|5. Quit |"); System.out.println("|5. Quit |");

View File

@ -26,11 +26,11 @@ public class Elipse implements Circular
{ {
return largeRadius; return largeRadius;
} }
public double area() public double computeArea()
{ {
return PI * smallRadius * largeRadius; return PI * smallRadius * largeRadius;
} }
public double circumference() public double computeCircumference()
{ {
double a = smallRadius; double a = smallRadius;
double b = largeRadius; double b = largeRadius;
@ -45,13 +45,13 @@ public class Elipse implements Circular
System.out.println("Area of elipse"); System.out.println("Area of elipse");
System.out.println("Small Radius: " + smallRadius); System.out.println("Small Radius: " + smallRadius);
System.out.println("Large Radius: " + largeRadius); System.out.println("Large Radius: " + largeRadius);
System.out.println("Area: " + area()); System.out.println("Area: " + computeArea());
} }
public void printCircumference() public void printCircumference()
{ {
System.out.println("Circumference of elipse"); System.out.println("Circumference of elipse");
System.out.println("Small Radius: " + smallRadius); System.out.println("Small Radius: " + smallRadius);
System.out.println("Large Radius: " + largeRadius); System.out.println("Large Radius: " + largeRadius);
System.out.println("Circumference: " + circumference()); System.out.println("Circumference: " + computeCircumference());
} }
} }

View File

@ -1,6 +1,6 @@
public class Dice public class Dice
{ {
private int valueOfDice=1; private int valueOfDice = 1;
public Dice() public Dice()
{ {

View File

@ -10,5 +10,6 @@ public class Circle extends Point {
public void setRadius(double radius) { this.radius = radius; } public void setRadius(double radius) { this.radius = radius; }
public double area() { return Math.PI * radius * radius; } public double area() { return Math.PI * radius * radius; }
public String toString() { return "Circle of radius " + radius + " at point [" + x + "," + y + "]"; } public String toString() { return "Circle of radius " + radius + " at point [" + x + "," + y + "]"; }
} }

View File

@ -1,4 +1,4 @@
public class Point { public class Point extends Object {
protected int x, y; protected int x, y;
public Point() { x = 0; y = 0; } public Point() { x = 0; y = 0; }

View File

@ -11,4 +11,4 @@ public class Circle extends Point {
public double area() { return Math.PI * radius * radius; } public double area() { return Math.PI * radius * radius; }
public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); } public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); }
} }