



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Application Architectures
Procedural Application Architecture
|
|
Java Application --
Procedural App Arch
// General Information
// ---------------------------------------------------
// File: PercentWhite.java
// Type: java application file
// Date: Sun Oct 8, 2000
// Name: Patrick Dunn
// Line: A program to find the % of white on a die.
// Application Description
// ---------------------------------------------------
/*
This program will find the percentage of white contained+
on a standard die. This version is done using the
Global Procedural Application Architecture
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class PercentWhite
{
// define data dictionary
static private final int nrFaces = 6;
static private final int nrDots = 21;
static private double edge;
static private double diameter;
static private Square face;
static private Circle dot;
static private double totalArea;
static private double whiteArea;
static private double blackArea;
static private double percentBlack;
// the main method
static public void main (String args[])
{
establishDieDimensions();
createFaceAndDot();
computeAreas();
computeWhitePercent();
displayWhitePercent();
} // end of main method
// Name: establishDieDimensions
// Description: gets the edge length of the die and+
// also computes the diameter of the dot
// Parameters:none
// Returns:nothing
// Author:Patrick Dunn
// Date:10/21/00
// Change log:
static private void establishDieDimensions()
{
// read the side which will be used to calculate
// the dimensions of the die from the user
IO.print("Enter length of the edge of the die --> ");+
edge = IO.read_double();
// calculate the radius of the dot to create it
// done by taking 1/5 of the user input
diameter = (.2 * edge);
}
// Name: createFaceAndDot
// Description: creates one face (square) and one d+
ot
// based on the dimensions entered/calculated
// Parameters:none
// Returns:nothing
// Author:Patrick Dunn
// Date:10/21/00
// Change log:
static private void createFaceAndDot()
{
// create the side
face = new Square(edge);
// create the dot
dot = new Circle(diameter / 2.0);
}
// Name: computeAreas
// Description: computes the areas of the die and d+
ots
// including white area
// Parameters:none
// Returns:nothing
// Author:Patrick Dunn
// Date:10/21/00
// Change log:
static private void computeAreas()
{
// get the area and multiply by 6 to get the area
// for all 6 sides
totalArea = (face.area()) * 6;
blackArea = (dot.area()) * 21;
// find the area
whiteArea = totalArea - blackArea;
}
// Name: computeWhitePercent
// Description: computes the percentage of the die +
that
// is white
// Parameters:none
// Returns:nothing
// Author:Patrick Dunn
// Date:10/21/00
// Change log:
static private void computeWhitePercent()
{
// get the percentage
percentBlack = (blackArea / totalArea) * 100.0;
}
// Name: displayWhitePercent
// Description: Displays the amount of the die that+
is white
// Parameters:none
// Returns:nothing
// Author:Patrick Dunn
// Date:10/21/00
// Change log:
static private void displayWhitePercent()
{
// display
IO.println("The percentage of die that is white is " ++
(100 - percentBlack) + " percent");
}
} // end of class
// Demo
// ---------------------------------------------------
/*
[patdunn@k6 procedural]$ java PercentWhite
Enter length of the edge of the die --> 1.79
The percentage of die that is white is 89.00442571243572 p+
ercent
[patdunn@k6 procedural]$ java PercentWhite
Enter length of the edge of the die --> 5.43
The percentage of die that is white is 89.00442571243572 p+
ercent
[patdunn@k6 procedural]$
*/
|
|
|