



|
|
Byron's CSC212 Web Site
|
Programming Challenge Archive
Program Potpouri ( Lab Programming )
Refinement App
|
|
|
This program from Lab # 6 provides an opportunity to refine a command, operator and 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
// Segment #1
// readCylinderDimensions();
{
IO.print("Height? ");
height = IO.read_double();
IO.println();
IO.print("Diameter? ");
diameter = IO.read_double();
}
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 =
new Rectangle(height,(diameter*Math.PI));
// 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
// command
// Definition of the makeCylinderSide
// operator
}
// 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()
{
return ((bottom.area()*2.0)+side.area());
}
}
// Demo
// ---------------------------------------------
/*
Height? 12.8
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 213.30801507307245
*/
|
|
|