



|
|
My Intro to Object-Oriented Programming
|
Programming Challenge Archive
Programming Assignment # 3
Objective Application Architecture
|
|
|
Read an edge length for a die. Determine the white percentage of the die if the black dots have a diameter equal to 1 / 5 the edge length of the die.
This version of Programming Assignment # 03 is written using the Objective Application Architecture.
JavaApplication - Programming Assignment # 3 --
Objective
// General Information
// ---------------------------------------------------
// File: PercentWhite.java
// Type: java application file
// Date: Fri Oct 20, 2000
// Name: Jeffrey R. Norkoli
// Line: Objective Application Architecture
// Application Description
// ---------------------------------------------------
/*
Read an edge length for a die. Determine the
white percentage of the die if the black dots
have a diameter equal to 1/5 the edge length
of the die.
This version of Programming Assignment #03 is
written using the Objective Application
Architecture.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class PercentWhite
{
// The Main Method
static public void main (String args[])
{
IO.println("Edge length? ");
Die d = new Die();
IO.print("The die is ");
IO.print(d.whitePercent());
IO.println(" percent white.");
}
}
class Die
{
// Instance Variables
static private final int
nrFaces = 6;
static private final int
nrDots = 21;
private double edge;
private double totalArea;
private double whiteArea;
// Constructor
public Die()
{
// Read edge length of die.
edge = IO.read_double();
// Compute total surface
// area of die.
Square face = new
Square(edge);
totalArea = face.area()
* nrFaces;
// Compute dot area (black area).
double diameter =
edge / 5.0;
Circle dot = new
Circle(diameter / 2.0);
// Calculate white area of die.
whiteArea = totalArea
- (dot.area() * nrDots);
}
public double whitePercent()
{
// Compute white area percentage.
return (whiteArea
/ totalArea * 100);
}
}
// Demo
// ---------------------------------------------------
/*
$ javac PercentWhite.java
$ java PercentWhite
Edge length?
1.79
The die is 89.00442571243572 percent white.
$ java PercentWhite
Edge length?
5.43
The die is 89.00442571243572 percent white.
$
*/
|
|
|