



|
|
CS1 Course Site
|
Programming Challenge Archive
Application Architectures
Functional Application Architecture
|
|
The Problem
: A ``standard'' die measures some number of inches on the edge. Its spots measure one /fifth the edge length on the diameter. What percent of the die is white?
Write a program to solve the problem in a manner consistent with the Functional application architecture. Run the program twice. Enter the values 1.79 and 5.43 as data on the successive runs.
JavaApplication --
Functional Application Architecture
// General Information
// ---------------------------------------------------
// File: PercentWhite.java
// Type: java application file
// Date: Tue Oct 24, 2000
// Name: James W. Bremenour
// Line: Calculates the percentage of white on a die
// Application Description
// ---------------------------------------------------
/*
Calculates the percentage of white on a die
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class PercentWhite
{
// Local class variables
// Number of faces
static private int nrFaces = 6;
// Number of dots
static private int nrDots = 21;
// Main Method
static public void main (String args[])
{
double edge = readEdge();
double diameter = calcDiameter(edge);
Square face = new Square(edge);
Circle dot = new Circle(diameter/2.0);
double totalArea = calcTotalArea(face);
double blackArea = calcBlackArea(dot);
double whiteArea = calcWhiteArea(totalArea, dot);
double whitePercent =
calcWhitePercent(totalArea, whiteArea);
IO.print("The die is: " + whitePercent);
IO.print("% white");
IO.println();
}
// Necessary Methods
static private double readEdge()
{
IO.print("What is the edge of the die? ");
double edge = IO.read_double();
return edge;
}
static private double calcDiameter(double e)
{
double diameter = e/5.0;
return diameter;
}
static private double calcTotalArea(Square f)
{
double totalArea = f.area() * nrFaces;
return totalArea;
}
static private double calcBlackArea(Circle d)
{
double blackArea = d.area() * nrDots;
return blackArea;
}
static private double calcWhiteArea(double t, Circl+
e d)
{
double whiteArea = t - d.area();
return whiteArea;
}
static private double calcWhitePercent(double tA, d+
ouble wA)
{
double whitePercent = (wA/tA)*100;
return whitePercent;
}
}
// Demo
// ---------------------------------------------------
/*
$ java PercentWhite
What is the edge of the die? 1.79
The die is: 99.4764012244017% white
$ java PercentWhite
What is the edge of the die? 5.43
The die is: 99.4764012244017% white
$
*/
|
|
|