Circle

In this program you'll follow up the XYPoint with a Circle class, that uses an XYPoint as its center.

  1. Go into the CS labs (see this map for details) and log onto a Windows machine.
  2. Start up a new project, called Circle, with a main class called Main.java. Refer to Lesson 2 if you are unsure how to do this. You do not need to include javabook2.jar in your class files. But be sure to delete TrivialApplication.java and set Main.java as the main file.
  3. In the blank file Main.java, write the following code:
  4. public class Main {
    
    	public static void main(String[] args) {
    
    	}
    
    }
    
  5. Add XYPoint.java to the project, from the last exercise.
  6. Create another new blank text file called Circle.java, and add it to the project.
  7. In Circle.java, type in the following code:
  8. public class Circle {
    
    	private XYPoint center;
    	private int radius;
    
    	public Circle(XYPoint center, int radius) {
    		setCenter(center);
    		setRadius(radius);
    	}
    
    	public Circle(int x, int y, int radius) {
    		this(new XYPoint(x,y),radius);
    	}
    
    	public Circle(int radius) {
    		this(0,0,radius);
    	}
    
    	public Circle() {
    		this(0);
    	}
    
    	public XYPoint getCenter() {
    		return center;
    	}
    
    	public int getCenterX() {
    		return center.getX();
    	}
    
    	public int getCenterY() {
    		return center.getY();
    	}
    
    	public int getRadius() {
    		return radius;
    	}
    
    	public int getDiameter() {
    		return 2 * getRadius();
    	}
    
    	public double getCircumference() {
    		return Math.PI * getDiameter();
    	}
    
    	public double getArea() {
    		return Math.PI * Math.pow(getRadius(),2);
    	}
    
    	public boolean overlap(Circle otherCircle) {
    		return (getRadius() + otherCircle.getRadius()) >= 
    			XYPoint.distance(getCenter(),otherCircle.getCenter());
    	}
    
    	public void setCenter(XYPoint center) {
    		this.center = center;
    	}
    
    	public void setCenterX(int x) {
    		center.setX(x);
    	}
    
    	public void setCenterY(int y) {
    		center.setY(y);
    	}
    
    	public void setCenter(int x, int y) {
    		setCenterX(x);
    		setCenterY(y);
    	}
    
    	public void setRadius(int radius) {
    		this.radius = radius;
    	}
    
    	public String toString() {
    		return "a circle with center at " + center + " and radius " + radius;
    	}
    
    }
    
  9. Now go back to Main.java, and add the following code:
  10. public class Main {
    
    	public static void main(String[] args) {
    
                    Circle c1 = new Circle();
                    System.out.println(c1);
    
                    XYPoint center = c1.getCenter();
                    center.setX(8);
                    center.setY(16);
                    System.out.println(c1);
    
                    c1.setCenter(-2,4);
                    System.out.println(c1);
    
                    center = new XYPoint(-3,6);
                    c1.setCenter(center);
                    c1.setRadius(2);
                    System.out.println(c1);
    
                    System.out.println("c1 has diameter " + c1.getDiameter());
                    System.out.println("c1 has circumference " + c1.getCircumference());
                    System.out.println("c1 has area " + c1.getArea());
    
                    Circle c2 = new Circle(-8,3,3);
                    System.out.println(c2);
    
                    double dist = XYPoint.distance(c1.getCenter(), c2.getCenter());
                    System.out.println("c1 and c2 are separated by a distance " + dist);
                    System.out.print("do c1 and c2 overlap? ");
    
                    boolean overlap;
                    overlap = c1.overlap(c2);
                    System.out.println(overlap);
    
                    c2.setRadius( 2 * c1.getRadius() );
                    System.out.println("c1 and c2 are separated by a distance " + dist);
                    System.out.print("do c1 and c2 overlap? ");
    
                    overlap = c1.overlap(c2);
                    System.out.println(overlap);
    
    	}
    
    }
    
  11. Compile and run your program. You should get the following output:
  12. a circle with center at (0,0) and radius 0
    a circle with center at (8,16) and radius 0
    a circle with center at (-2,4) and radius 0
    a circle with center at (-3,6) and radius 2
    c1 has diameter 4
    c1 has circumference 12.566370614359172
    c1 has area 12.566370614359172
    a circle with center at (-8,3) and radius 3
    c1 and c2 are separated by a distance 5.830951894845301
    do c1 and c2 overlap? false
    c1 and c2 are separated by a distance 5.830951894845301
    do c1 and c2 overlap? true
    
  13. Press enter, as the console window says, and the black console window will disappear.