



|
|
CS1 Course Site
|
Programming Challenge Archive
Application Architectures
Global Procedural 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 Global Procedural application architecture. Run the program twice. Enter the values 1.79 and 5.43 as data on the successive runs.
JavaApplication --
Global Procedural 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 what on a die
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class PercentWhite
{
// Declare Variables
// The number of faces
static private int nrFaces = 6;
// The number of dots
static private int nrDots = 21;
// The edge of the die
static private double edge;
// One dot's diameter
static private double diameter;
// One face
static private Square face;
// One dot
static private Circle dot;
// The total area
static private double totalArea;
// The white area
static private double whiteArea;
// The black area
static private double blackArea;
// The white percent
static private double percentWhite;
// Main method
static public void main (String args[])
{
establishDieDimensions();
createFaceAndDot();
computeAreas();
computeWhitePercent();
displayWhitePercent();
}
// Necessary Methods
static private void establishDieDimensions()
{
IO.print("What is the edge length? ");
edge = IO.read_double();
diameter = edge/5.0;
}
static private void createFaceAndDot()
{
face = new Square(edge);
dot = new Circle(diameter/2.0);
}
static private void computeAreas()
{
totalArea = face.area() + dot.area();
whiteArea =
totalArea - (nrDots * dot.area());
blackArea = nrDots * dot.area();
}
static private void computeWhitePercent()
{
percentWhite = (whiteArea/totalArea)*100;
}
static private void displayWhitePercent()
{
IO.print("The die is: " + percentWhite);
IO.print("% white");
IO.println();
}
}
// Demo
// ---------------------------------------------------
/*
$ java PercentWhite
What is the edge length? 1.79
The die is: 36.036041302015434% white
$ java PercentWhite
What is the edge length? 5.43
The die is: 36.036041302015434% white
$
*/
|
|
|