Tutorial 2
This commit is contained in:
commit
a7b6bab263
|
@ -0,0 +1,2 @@
|
|||
.idea/
|
||||
out/
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="GENERAL_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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!!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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!!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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!!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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):");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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):");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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):");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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):");
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
public interface Circular
|
||||
{
|
||||
public double area();
|
||||
public double circumference();
|
||||
public void printArea();
|
||||
public void printCircumference();
|
||||
}
|
|
@ -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):");
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 <key> to roll the first dice");
|
||||
sc.next();
|
||||
Dice d1 = new Dice();
|
||||
rollOne = d1.getDiceValue();
|
||||
d1.printDiceValue();
|
||||
|
||||
System.out.println("Press <key> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue