CS 1713 -- Introduction to Computer Science
Fall 2003 -- Exam 1 Answers

Answers are in boldface.

Questions about classes.

  1. Complete the code for a simple Circle class, similar to the Rectangle class that we studied.

    The Circle class should have data fields giving the circle's radius, x-coordinate of center, and y-coordinate of center.

    There should be methods to return each of these fields, as well the methods getArea() and getCircumference(). (Recall for a circle of radius r, the area is pi * r 2, and the circumference is 2 * pi * r. You should also use Math.PI as the source of the constant pi.)

    There should be two constructors, the second one letting the coordinates of the center default to zeros. In both constructors, if the parameter for the radius is negative, set it equal to zero.

    Fill in the blanks below to complete the Java implementation of this class:

    public class Circle {
       // data fields for the object
       private double myRadius;
       private double myX; // x coordinate of center
       private double myY; // y coordinate of center
    
       // constructors
       public Circle(double radius, double x, double y) {
          if (radius < 0) myRadius = 0;
          else
             myRadius = radius;
          myX = x;
          myY = y;
       }
    
       // center defaults to (0, 0)
       public Circle(double radius) {
          if (radius < 0) myRadius = 0;
          else
             myRadius = radius;
          myX = 0;
          myY = 0;
       }
    
       // alternative code for above constructor ...
       // (Can't have both of these definitions, though)
       // (Answer by Jared Rodriguis.)
       // Note: if a constructor invokes a constructor, the
       //   constructor invokation must be the first thing
       public Circle(double radius) {
          this(radius, 0, 0);
       }
    
       //accessors
       public double getRadius( ) {
          return myRadius;
       }
    
       public double getX( ) {
          return myX;
       }
    
       public double getY( ) {
          return myY;
       }
    
       // other methods
       public double getArea( ) {
          return Math.PI*myRadius*myRadius;
       }
    
       public double getCircumference( ) {
          return 2*Math.PI*myRadius;
       }
    }
    
  2. Fill in code in the test program below to do the following:
    1. Instantiate a circle object with radius 10, and with x and y coordinates of the center (2, 3), using circle1 as its variable name.
    2. Give code to print the area of circle1.
    3. Similarly, use the second constructor to instantiate another object with radius 5 and center (0, 0), using circle2 as its variable name.
    4. Give code to print the circumference of circle2.

  3. Write a toString method for the Circle class so that when you try to print circle1 in the main method, it will print the characters below exactly as shown.

  4. Write a compareTo method for the Circle class that will return 0 when the radii of the class instance and the parameter are the same, will return -1 when the radius of the class instance is less than that of the parameter, and will return 1 when the radius of the class instance is greater than the radius of the parameter.

    This method compareTo involves two objects that are instances of the Circle class. The first is the circle object that the method is inside, and the second is the parameter c. The radius of the first is just myRadius, while the radius of the second is given by c.getRadius(). (In this case this.myRadius also works for the first, and c.myRadius also works for the second.) Here is a solution:

    Here is a second version:

    Finally, here is extra code for the main function to test some of the above. (This was not asked for on the actual exam.)

    Notice that when one writes circle1.compareTo(circle2), the variable circle1 refers to an instance of the Circle class, and circle1.compareTo ... is a method inside that instance. Then circle2 is fed in as a parameter, becoming c inside the method.

    Here is the output:

Question about loops.
  1. Starting with a positive integer value stored in n, write a loop that will add up the positive odd integers less than n, and print the sum. (Hint: an integer i is odd in case i%2 is 1, where the latter means the remainder when i is divided by 2.)

    Here are two solutions:

    and here is the output:

Questions about Strings.
  1. Give an if statement that will check if two strings s and t are equal (have the same characters) and will print "They are equal" in case they are.

    Give an if statement that will check if string s comes before string t in lexicographic order and will print "s precedes t" if it is true.

    Output:

  2. Write a loop that will start with a string s such as

    and will print each character of s on a separate line, so that in this case it will print:

    You must use a loop and it should work no matter what s is.


Points for each problem: 1-20, 2-20, 3-10, 4-10, 5-10, 6-15, 7-15.