P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
Programming Challenge Archive

Lab Challenge Archive
Refinement Application
 
 
  Java Application  -- RefinementApp

   // 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 );
  
         }
  
          // Name:readCylinderDimensions
          // Description:Definition of readCylinderDimensions+
    
          //   command
          // Parameters: none (global vars defined)
          // Author:patrick dunn
          // Date:10/11/2000
          // Change log:
          static private void readCylinderDimensions()
          {
      // read cylinder height and diameter
      IO.print("Enter the cylinder's height --> ");
      height = IO.read_double();
  
      IO.print("Enter the cylinder's diameter --> ");
      diameter = IO.read_double();
          }
  
          // Name:makeCylinderSide
          // Description:Definition of the makeCylinderSide
          //  operator
          // Parameters:Circle bot - bottom of cylinder
          //            double ht - height of cylinder
          // Returns: a rectangle object
          // Author:Patrick Dunn
          // Date:10/11/2000
          // Change log:
          static public Rectangle makeCylinderSide(Circle bot+
   ,double ht)
          {
      // create the rectangle based on the cylinders diamete+
   r
      double sideLength = bot.perimeter();
      Rectangle cylinderSide = new Rectangle(ht,sideLength);+
  
      
      // return rectangle
      return cylinderSide;
          }
      }
  
   // Cyliner Class
   // ---------------------------------------------
  
     // Name: class Cylinder
     // Description: a class to create a cylinder
     // Parameters: Circle object / Rectangle object
     // Returns: a cylinder
     // Author:Craig Graci
     // Date: 10/11/200
     // Change log: 
     //   10/11/2000 - Patrick Dunn - refined for returning
     //       the surface area
  
      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.area() + side.area());
         }
      }
  
  
   // Demo
   // ---------------------------------------------
  
   /*
   $ java RefinementApp
   Enter the cylinder's height --> 12.8
   Enter the cylinder's diameter --> 4.51
  
   Cylinder height is 12.8
   Cylinder diameter is 4.51
  
   |> Circle with RADIUS=2.255 <|
   |> Rectangle with HEIGHT=12.8 WIDTH=14.168582867689967 <|
  
   The surface area is 197.33293788975203
   $ 
   */