



|
|
CS1 Course Site
|
Programming Challenge Archive
Refinement Sequences
|
|
|
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.print("Height of figure? ");
height = IO.read_double();
IO.print("Diameter of figure? ");
diameter = IO.read_double();
}
// Definition of the makeCylinderSide
static private Rectangle makeCylinderSide(Circle c,+
double ht)
{
double length = c.perimeter();
Rectangle r = new Rectangle(height,length);
return r;
}
}
// 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.area() * 2 + side.area();
}
}
// Demo
// -------------------------------------------
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 213.30801507307245
$
|
|
|