Computer Science 2310
Fall 2009
Exercises for Quiz 9

This will be a closed book quiz. You may bring one 8.5x11 page of prepared notes, written on both sides.

  1. Suppose that you have an object called cal, of class Calendar. Object cal contains a variable called day, of type int, and also has a public method called setDay that takes a single integer parameter, stores the value of that parameter into variable day, and then produces a void result. What would you write to ask cal to run its setDay method with parameter 5?

    Answer

  2. What is the difference between an object and a class?

    Answer

  3. What is (typically) the responsibility of a constructor for a class? What do you use it for?

    Answer

  4. A circle in the plane can be described by its center point and its radius. Class Circle describes objects that are circles in the plane, and its definition is partially given below. Class Circle provides some operations on circles. For example, function area returns the area of a circle. Write the definition of function area. Use 3.14159 as an approximation of pi.

      class Circle 
      {
        public Circle(Point center, double radius)
        {
          myCenter = center;
          myRadius = radius;
        }
    
        public double area()
        {
          ...
        }
    
        private Point myCenter;    // center of this circle
        private double myRadius;   // radius of this circle
      }
    

    Answer

  5. An object of class Point represents a point in the plane. Class Point is defined as follows, with some parts left out.

      class Point
      {
        // x and y coordinates of the point.
    
        private double x; 
        private double y;
    
        public Point(double xval, double yval)
        {
          ...
        }
    
        public double distanceFromOrigin()
        {
          ...
        }
      }
    
    1. Write the definition of the constructor for class Point. It should make the x-coordinate be xval and the y-coordinate be yval.

      Answer

    2. Write the definition of distanceFromOrigin. The distance of point (x,y) from the origin is sqrt(x2 + y2).

      Answer

  6. Using classes Point and Circle from the preceding exercises, write a sequence of statements that creates a new circle with center point (0,0) and with a radius of 10.0 and then prints the area of that circle on the standard output, preceded by "The area is ". You must use the area method to get the area of the circle.

    Answer