CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Program Potpouri
Refinement
 
This program was featured in the lab devoted to our experiences with refining a command,  operator and an object.
 
  JavaApplication  -- 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
            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
            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
            Cylinder cylinder = 
              new Cylinder(bottom, side);
            double sa = cylinder.surfaceArea();
            IO.println();
            IO.println("The surface area is " + sa );
         }
  
         // Definition of readCylinderDimensions 
         static private void readCylinderDimensions()
         {
     IO.println("Enter the height of the Cylinder: ");
     height = IO.read_double();
     IO.println("Enter the diameter of the Cylinder: ");
     diameter = IO.read_double();
         }
         // Definition of the makeCylinderSide 
         static private Rectangle makeCylinderSide(Circle b, +
   double h)
         {
     double width = b.diameter();
     Rectangle r = new Rectangle(h, width);
     return r;
         }
      }
  
   // Cyliner 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()
         {
            double sa = (bottom.area() * 2) + side.area();
    return sa;
         }
      }         
  
   // Demo
   // ---------------------------------------------
  
   /*
   $ java RefinementApp
   Enter the height of the Cylinder: 
   12.8
   Enter the diameter of the Cylinder: 
   4.51
  
   Cylinder height is 12.8
   Cylinder diameter is 4.51
  
   |> Circle with RADIUS=2.255 <|
   |> Rectangle with HEIGHT=12.8 WIDTH=4.51 <|
  
   The surface area is 89.67815436664088
   $ 
   */