Lab 10 Pre-Lab Assignment     Name ____________________________________________

  1. A circle is defined in mathematics as the set of all points equidistant from a given point, called the center of the circle. The distance is called the radius. Hence a circle is characterized by its center and its radius. Fill in the following to implement a class called Circle that has three pieces of instance data - the x coordinate of the center, the y coordinate of the center, and the radius.
    1. Fill in the declarations of the instance variables (assume all three are type int).
    2. Fill in the code for a constructor that takes 3 parameters for the x and y coordinates of the center of the circle and the radius of the circle and uses those to initialize the instance variables.
    3. Write an accessor method getRadius that returns the radius.
    4. Write code for a method named area that computes and returns the area of the circle. Note that the area of a circle is pi (use Math.PI) times the square of the radius - that will be type double so the return type for the method should be double. The method takes no parameters.
    5. Write a method named resize that takes one parameter of type int and changes the radius by that amount (adds the amount to the radius). The method should not return anything (this means the return type in the header is the reserved word void). NOTE: The parameter can be negative (this would make the circle smaller) - no change to the radius should be made if adding the parameter would result in a negative radius.
    6. Write a method inCircle that takes two parameters x1 and y1 of type int representing the coordinates of a point (x1, y1). The method should return true if the point (x1,y1) is inside the circle (or on the boundary) and false otherwise. Remember that a point is in the circle if the distance from the point to the center of the circle is less than or equal to the radius. [You used the distance formula in lab3 and again in assignment 2.] Note that this method can be done with a single return statement - return the boolean expression that is true when the point is in the circle and false otherwise.
    7. Write a toString method that returns a string that includes the center and radius in a format similar to "Center at (5, 6); Radius 10"
      
      
      public class Circle
      {
      
          // Declare the instance data - be sure to use the appropriate visibility modifier
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
          // Constructor - takes three int parameters for the x and y coordinates
          // of the circle and the radius and uses them to initialize the circle. 
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
          // getRadius - returns the radius
      
      
      
      
      
      
      
      
      
      
      
      
          // area - computes and returns the area of the circle (type double)
      
      
      
      
      
      
      
      
      
      
      
      
          // resize - takes one parameter of type int representing the
          // amount to change the size of the radius; the radius is changed
          // by that amount
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
          // inCircle - takes two parameters x1, y1 of type int representing the
          // x and y coordinates of a point. The method returns true if the point 
          // specified point (x1,y1) is in the circle (or on the boundary) and 
          // false otherwise
      
      
      
      
      
      
      
      
      
      
      
          // toString - returns a string representation of the circle
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      } 
      
      

  2. Fill in code in the following client program as instructed by the comments.
    import java.util.Scanner;
    public class CircleTest
    {
       // Use some methods in the Circle class to test them
       public static void main (String[] args)
       {
          int x, y, r;
          Scanner scan = new Scanner (System.in);
          System.out.println ("Circle Tester");
          System.out.print ("Enter the x and y coordinates of the circle center: ");
          x = scan.nextInt();
          y = scan.nextInt();
    
          System.out.print ("Enter the radius of the circle: ");
          r = scan.nextInt();
    
          // Declare and instantiate a Circle object with center at
          // the point (x, y) and radius r. (Name your object testCircle)
    
    
    
    
    
    
    
          // Write a statement that prints the area of the circle (invoke
          // the area method in your print statement) appropriately labeled
    
    
    
    
    
    
    
          // Write a statement that increases the radius of testCircle by 5. 
    
    
    
    
    
    
          // Write a statement that prints the new radius of the circle
          // (invoke the getRadius method in your print statement).
    
    
    
    
    
    
    
    
    
          // Fill in the blank with a call to the inCircle method to
          // determine if the point (5, 13) is in testCircle
    
    
          if ( ___________________________________________________ )
    
            System.out.println ("(5, 13) is in the circle");
          else
            System.out.println ("(5, 13) is not in the circle");
    
    
          // Write a statement that prints the testCircle object
    
    
    
    
    
    
    
       }
    }