CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Application Architecture
Objective Application Architecture
 
 

 
  JavaApplication  -- PercentWhite

   // General Information
   // ---------------------------------------------------
  
   // File:  percentWhite.java
   // Type:  java application file
   // Date:  Sun Oct 22, 2000
   // Name:  Kara Becker
   // Line:  Determine the percentage of white space on a die+
    using objective application architecture.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
     Determine the percentage of white space on a die using o+
   bjective application architecture.
   */
  
   // Required Packages
   // ---------------------------------------------------
  
   import blue.io.*;
   import blue.shapes.*;
  
   // Application Class
   // ---------------------------------------------------
  
   class percentWhite
   {
       static public void main (String args[])
       {
   IO.println("Edge length? ");
   Die d = new Die( IO.read_double() );
   IO.print("The die is ");
   IO.print(d.whitePercent());
   IO.println(" percent white.");
       }
   }    
   class Die {
       //Instance variables.
       private int nrFaces = 6;
       private int nrDots = 21;
       private double edgeLength;
       
       //Constructor.
       public Die(double sideLength)
       {
   edgeLength = sideLength;
       }
       
       //Other methods.
       public double whitePercent()
       {
   double diameter = (1.0 / 5.0) * edgeLength;
   double totalArea = (edgeLength * edgeLength) * nrFaces;
   Circle dot = new Circle(diameter / 2.0);
   double dotArea = dot.area() * nrDots;
   double whiteArea = totalArea - dotArea;
   double percentWhite = (whiteArea / totalArea) * 100.0;
   return percentWhite;
       } 
   }
  
  
   // Demo
   // ---------------------------------------------------
  
   /*
     $ java percentWhite
     Edge length?
     1.79
     The die is 89.00442571243572 percent white.
     $ javac percentWhite.java
     $ java percentWhite
     Edge length?
     5.43
     The die is 89.00442571243572 percent white.
     $
   */