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

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.

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) {
    
       
       
    
       }
    
       // center defaults to (0, 0)
       public Circle(double radius) {
    
       
       
    
       }
    
       //accessors
       public double getRadius( ) {
    
       }
    
       public double getX( ) {
    
       }
    
       public double getY( ) {
    
       }
    
       // other methods
       public double getArea( ) {
    
       }
    
       public double getCircumference( ) {
    
       }
    }
      
  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.

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.)

     

     

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.

     

  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.