Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Laboratory Challenges
Stepwise Refinement
 
The program below was featured in Laboratory # 07 on October 13,  2000..   As Friday the 13 th would have it,  I found this to be the most challenging lab to date by far.   This lab provided us an opportunity to refine a command,  refine an operator,  and refine an object.  

 
  Laboratory Challenges  -- Stepwise Refinement

   // General Information
   // ----------------------------------------------
  
   // File:  RefinementApp.java
   // Type:  java application file
   // Date:  Mon Mar  3, 1997
   // Name:  blue
   // Line:  Refining a command, operator, object
  
   // Application Description
   // ----------------------------------------------
  
   /*
  
      Read two double values representing the height 
      and diameter of a cylinder.  Display its 
      volume.  Display its surface area.
  
   */
  
   // Required Packages
   // ----------------------------------------------
  
      import blue.io.*;
      import blue.shapes.*;
  
   // Application Class
   // ----------------------------------------------
  
      class RefinementApp
      {
  
      // Data local to this Refinement class 
      // (and global to its methods)
         static private double height;
         static private double diameter;
  
         static public void main (String args[])
         {
         // read and echo the cylinder dimensions - Segment #+
   1   
     readCylinderDimensions();
     IO.println();
     IO.print("Cylinder height is "); 
     IO.println(height);
     IO.print("Cylinder diameter is ");
     IO.println(diameter);
          
         // Model the "top" of the cylinder, compute 
         // the volume -  Segment #2
            Circle bottom = 
              new Circle( diameter / 2.0 );
            Rectangle side = 
              makeCylinderSide(bottom, height);
            IO.println();
            bottom.describe();
            side.describe();
  
         // Create the cylinder and ask it to compute 
         // some values
         // Segment #3
            Cylinder cylinder = 
              new Cylinder(bottom, side);
            double sa = cylinder.surfaceArea();
            IO.println();
            IO.println("The surface area is " + sa );
         }
  
         // Definition of readCylinderDimensions 
          static public void readCylinderDimensions()
          {
      height = IO.read_double();
      diameter = IO.read_double();
          }
         
         // Definition of the makeCylinderSide 
          static private Rectangle makeCylinderSide(Circle bo+
   ttom, double h)
          {
     return  (new Rectangle((bottom.perimeter()), height));
          }
  
          
      }
  
   // Cylinder Class
   // ---------------------------------------------
  
    
      class Cylinder
      {
      // instance variables
         Circle bottom;
         Rectangle side;
  
      // Constructor
         public Cylinder(Circle b, Rectangle s)
         {
            bottom = b;
            side = s;
         }
  
      // An instance method (operator)
         public double surfaceArea()
         {
     return (bottom.perimeter() * side.width());
         }
      }
            
  
   // Demo
   // ---------------------------------------------
  
   /*
   $ javac RefinementApp.java
   $ java RefinementApp
   12.8
   4.51
  
   Cylinder height is 12.8
   Cylinder diameter is 4.51
  
   |> Circle with RADIUS=2.255 <|
   |> Rectangle with HEIGHT=14.168582867689967 WIDTH=12.8 <|
  
   The surface area is 181.35786070643158
   */