CS302 Practice Quiz #2



    Designate the following statements as either True or False:

  1. T Instance methods can access class data members as well as instance data members.
  2. T If no constructor is explicitly defined for a class, the Java compiler will supply a default constructor.
  3. F When calling a method from within another method of the same class, it is necessary to use dot notation.

    Circle the best answer to each question:

  4. Which of the following is required in order to designate a method as a class method? (e) static
  5. A method with a return value must contain which of the following? (b) return statement
  6. Evaluate the following variables given the following information:
    
              int a = 4;
              int b = 1;
              int c = 2;
              int d = 6;
              int e = 3;
              int f = 10;
              int v, w, x, y, z;
    
              v = a + b/c + d * e;             // v = 22
              
              w = f % c + a;                   // w = 4
    
              x = (e * e + c/e) * c;           // x = 18
    
              y = f + (int) 5.8;               // y = 15
    
              z = f % a  - e;                  // z = -1
    
  7. Write a calculation class to calculate and display the area and circumference of a circle. The provided class is the main class. You will define all the data members of your class to be private, instance variables. The start method is the only public method for your class. It will have a circumference for the radius of the circle and call all other methods. In addition to the start method, you will need 3 private instance methods: one to calculate the area, the second to calculate the circumference and a final one to output the results. You may use javabook classes as well as Math.PI constant. Remember the formulas for circles are:

    Area = Math.PI * (radius) * (radius)
    Circumference = 2 * Math.PI * radius

    class DriverClass{
         public static void main(String args[])
         {
              Calculation myCalculator = new Calculation();
              myCalculator.start(5.2);        // 5.2 is the radius of the circle
         }
    }
    ----------------------------------------------------------------------------
    import javabook.*;
    
    
    /**
     * Class with 4 methods: start, calcArea, calcCircumference, display.
     * I chose to use the default constructor and have no data members for 
     * this class.
     **/
    class Calculation{
    
      /**
       * start calls all other methods: calcArea,calcCircumference,display
       * @param r is the radius of the circle
       * @return none
       * Bug: Does not check parameter, lets other methods check this.
       **/
      public void start(double r){
         display(calcArea(r),calcCircumference(r));    
      }
    
      /**
       * calcArea uses the formula for the area of a circle to calculate the 
       *  area, returns -1 if radius is negative
       * @param radius is radius of the circle
       * @return area of circle
       **/
       private double calcArea(double radius){
        
          if(radius < 0)
             return -1;   //error condition
          else
             return Math.PI * radius * radius;
    
       }
    
       /**
        * calcCircumference uses the formula for the circumference of a circle to 
        * calculate the circumference, returns -1 if the radius is negative
        * @param radius is the radius of the circle
        * @return circumference of the circle
        **/
        private double calcCircumference(double radius){
    
          if(radius < 0)
            return -1;   //error condition
          else
            return Math.PI * 2 * radius;
    
        }
    
    
        /**
         * display takes an area and circumference and displays them. If either 
         * is negative, an error message is displayed.
         * @param a is the area of the circle
         * @param c is the circumference of the circle
         **/
         private void display(double a, double c){
    
            MainWindow window = new MainWindow();
    	window.show();
    	OutputBox output = new OutputBox(window);
            if(a < 0 || c < 0) //problem with negative numbers
              output.printLine("Negative values are illegal");
            else
              output.printLine("The area is " + a + " and the circumference is "+c);
            output.show();
         }
    }