CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Application Architectures
Objective 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 Objective application architecture.   Run the program twice.   Enter the values 1.79 and 5.43 as data on the successive runs.
 
  JavaApplication  -- Objective 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
      {
         static public void main (String args[])
         {
     IO.println("Edge length?  ");
     Die d = new Die( IO.read_double() );
     IO.print("The die is: " + d.whitePercent());
             IO.print(" percent white.");
             IO.println();
         }
      }
         class Die
         {
         // Instance variables
             private double diameter;
             private double totalArea;
             private double blackArea;
             private double whiteArea;
         // Constructor
             public Die(double edge)
             {
               int nrFaces = 6;
               int nrDots = 21;
               diameter = edge/5.0;
               Square face = new Square(edge);
               Circle dot = new Circle(diameter/2.0);
               totalArea = face.area() * nrFaces;
               blackArea = dot.area() * nrDots;
               whiteArea = face.area()-blackArea;
             }
         // Necessary other methods
         // The use of a variable is incase I need to 
         // debug it
             public double whitePercent()
             {
               double wP = (whiteArea/totalArea)*100;
               return wP;
             }
         }     
  
   // Demo
   // ---------------------------------------------------
  
   /*
   $ java PercentWhite
   Edge length?  
   1.79
   The die is: 5.671092379102391 percent white.
   $ java PercentWhite
   Edge length?  
   5.43
   The die is: 5.671092379102391 percent white.
   $ 
   */