



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Application Architectures
Functional Application Architecture
|
|
Java Application --
Functional App Arch
// General Information
// ---------------------------------------------------
// File: PercentWhite.java
// Type: java application file
// Date: Sat Oct 21, 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 contain+
ed
on a standard die. This version is done using the
Functional Application Architecture
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class PercentWhite
{
// declare variables
static private final int nrFaces = 6;
static private final int nrDots = 21;
static public void main (String args[])
{
double edge = readEdge();
double diameter = determineDiameter(edge);
Square face = new Square(edge);
Circle dot = new Circle(diameter/2.0);
double totalArea = computeTotalArea(face);
double blackArea = computeBlackArea(dot);
double whiteArea = computeWhiteArea(totalArea,blackAre+
a);
double whitePercent = computeWhitePercent(totalArea,wh+
iteArea);
IO.print("The die is ");
IO.print(whitePercent);
IO.println(" percent white.");
} // end of main method
// Name: readEdge
// Description: reads the edge length of the die
// Parameters: none
// Returns: double - edge length
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double readEdge()
{
IO.print("Enter length of the edge of the die --> ");+
return IO.read_double();
}
// Name: determineDiameter
// Description: determines the diameter of the dot +
based on
// 1/5 (.2) of the edge length
// Parameters: double sideEdgeLength - edge length +
of the die
// Returns: diameter of a dot on the die
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double determineDiameter(double sideLen+
gthEdge)
{
// calculate and return the diameter of a dot
return (sideLengthEdge * .2);
}
// Name: computeTotalArea
// Description: computes the total area of the die
// Parameters: Square theDieFace - the face of one +
side of the square
// Returns: double - total area of the square
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double computeTotalArea(Square theDieFa+
ce)
{
// get the area of one side of the square, the face
double areaOfOneSide = theDieFace.area();
// multiply * 6 and this will give us the total area
return (nrFaces * areaOfOneSide);
}
// Name: computeBlackArea
// Description: returns the total black area on the+
die
// Parameters: Circle theDot - circle representing +
one dot on the die
// Returns: double - total dot area
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double computeBlackArea(Circle dot)
{
double areaOfOneDot = dot.area();
return (nrDots * areaOfOneDot);
}
// Name: computeWhiteArea
// Description: Computes the total white area of th+
e die
// after the black area is subtracted
// Parameters:
// double dieTotalArea - total area of the die
// double dotTotalArea - total area of the dots
// Returns: double - the white area remaining
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double computeWhiteArea(double dieTotal+
Area,
double dotTotalArea)
{
return dieTotalArea - dotTotalArea;
}
// Name: computeWhitePercent
// Description: computes the total percentage of th+
e die that is
// white
// Parameters:
// double dieTotalArea - total area of the die
// double totalWhiteArea - total are of the die that +
is white
// Returns: double - percentage of the area that is+
white
// Author: Patrick Dunn
// Date: 10/21/2000
// Change log:
static private double computeWhitePercent(double dieTo+
talArea,
double totalWhiteArea)
{
double percentWhite = (totalWhiteArea / dieTotalArea)+
* 100;
return percentWhite;
}
} // end PercentWhite class
// Demo
// ---------------------------------------------------
/*
[patdunn@k6 functional]$ javac PercentWhite.java
[patdunn@k6 functional]$ java PercentWhite
Enter length of the edge of the die --> 1.79
The die is 89.00442571243572 percent white.
[patdunn@k6 functional]$ java PercentWhite
Enter length of the edge of the die --> 5.43
The die is 89.00442571243572 percent white.
[patdunn@k6 functional]$
*/
|
|
|