-
-
Save bytecodeman/5424889a1a14e173b7874ed95db7ce63 to your computer and use it in GitHub Desktop.
Circle Code V3 1/31/20
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
package chapter9; | |
//Define the circle class with two constructors | |
public class SimpleCircle { | |
// properties, attributes, instance variables | |
private double radius; | |
private double centerX; | |
private double centerY; | |
private static int count; | |
public static int getCount() { | |
return count; | |
} | |
/** Constructors */ | |
public SimpleCircle() { | |
this(1, 0, 0); | |
} | |
public SimpleCircle(double radius) { | |
this(radius, 0, 0); | |
} | |
public SimpleCircle(double radius, double centerX, double centerY) { | |
this.setRadius(radius); | |
this.setCenterX(centerX); | |
this.setCenterY(centerY); | |
SimpleCircle.count++; | |
} | |
public void finalize() { | |
System.out.println("Hi! I'm finalizing!"); | |
SimpleCircle.count--; | |
} | |
// public String toString() { | |
// return "(cx, cy) = " + this.centerX + " " + this.centerY + | |
// " Radius = " + this.radius; | |
// } | |
@Override | |
public String toString() { | |
return "Circle [radius=" + radius + ", centerX=" + centerX + ", centerY=" + centerY + "]"; | |
} | |
/** Return the area of this circle */ | |
// circle2.getArea() ===> this == circle2 | |
// tony.getArea() ====> this == tony | |
double getArea() { | |
return this.radius * this.radius * Math.PI; | |
} | |
// Not cool!!! | |
static double getArea(SimpleCircle x) { | |
return x.radius * x.radius * Math.PI; | |
} | |
/** Return the perimeter of this circle */ | |
double getPerimeter() { | |
return 2 * this.radius * Math.PI; | |
} | |
/** Set a new radius for this circle */ | |
void setRadius(double radius) { | |
if (radius >= 0) | |
this.radius = radius; | |
else | |
System.err.println("Negative Radius Detected: " + radius); | |
} | |
public double getCenterX() { | |
return this.centerX; | |
} | |
public void setCenterX(double centerX) { | |
if (centerX >= 0) | |
this.centerX = centerX; | |
else | |
System.err.println("Negative CenterX: " + centerX); | |
} | |
public double getCenterY() { | |
return centerY; | |
} | |
public void setCenterY(double centerY) { | |
if (centerY >= 0) | |
this.centerY = centerY; | |
else | |
System.err.println("Negative CenterY: " + centerY); | |
} | |
double getRadius() { | |
return radius; | |
} | |
} |
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
/* | |
* Name: Tony | |
* Date: | |
* Course Number: | |
* Course Name: | |
* Problem Number: | |
* Email: | |
* Short Description of the Problem | |
*/ | |
package chapter9; | |
import java.util.Scanner; | |
public class TestCircle2 { | |
private final static String TITLE = "Circle Tester V3.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
//********************************************** | |
// Put as many methods you need here | |
//********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter Radius: "); | |
double r = sc.nextDouble(); | |
System.out.print("Enter X: "); | |
double cx = sc.nextDouble(); | |
System.out.print("Enter Y: "); | |
double cy = sc.nextDouble(); | |
SimpleCircle circle = new SimpleCircle(r, cx, cy); | |
//circle.setCenterX(cx); | |
//circle.setCenterY(cy); | |
//double area = circle.getArea(); | |
//double peri = circle.getPerimeter(); | |
//System.out.println("Area = " + area); | |
//System.out.println("Peri = " + peri); | |
//System.out.println(circle); | |
SimpleCircle circle2 = new SimpleCircle(r, cx, cy); | |
//circle.setCenterX(cx); | |
//circle.setCenterY(cy); | |
//double area2 = circle2.getArea(); | |
//double peri2 = circle2.getPerimeter(); | |
//System.out.println("Area = " + area2); | |
//System.out.println("Peri = " + peri2); | |
//System.out.println(circle2); | |
System.out.println("No of Circles: " + SimpleCircle.getCount()); | |
circle = null; | |
System.gc(); | |
// Put current program to sleep for 10 msec | |
try { | |
Thread.sleep(10); | |
} | |
catch (Exception e) { | |
} | |
System.out.println("No of Circles: " + SimpleCircle.getCount()); | |
sc.nextLine(); // Clear Keyboard | |
} | |
//********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
// Do not change the main method | |
public static void main(String args[]) { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment