P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
Programming Challenge Archive

Application Architectures
Objective Application Architecture
 
 
  Java Application  -- Objective App Arch

   // General Information
   // ---------------------------------------------------
  
   // File:  PercentWhite.java
   // Type:  java application file
   // Date:  Sat Oct 21, 2000
   // Name:  Patrick Dunn
   // Line:  A program to find the % of white on a die.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
       This program will find the percentage of white contain+
   ed
       on a standard die.  This version is done 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.print("Edge length? ");
     Die d = new Die(IO.read_double());
     IO.print("The die is ");
     IO.print(d.whitePercent());
     IO.println(" percent white.");
  
     } // end of main method
      }
      
      // Name:Die - class
      // Description:A class that is a die with some properti+
   es
      // Parameters:edgeLength - the length of the edge of th+
   e die
      // Returns:a 6 sided die
      // Author:Patrick Dunn
      // Date:10/21/00
      // Change log:
      class Die
      {
      // instance vars
      private int nrFaces = 6;
      private int nrDots = 21;
      private double whiteArea;
      private double dieTotalArea;
      private double dotTotalArea;
  
      // constructor of the class
      public Die(double lengthEdge)
      {
      
      // create the face of a die and dot
      Square face = new Square(lengthEdge);
  
      // calculate the dot diameter based on 1/5 of the
      // edge length
      double dotDiameter = lengthEdge * .2;
  
      // create the dot
      Circle dot = new Circle(dotDiameter / 2.0);
  
      // calculate the area of the die
      dieTotalArea = (face.area()) * nrFaces;
  
      // calculate total area of dots
      dotTotalArea = (dot.area()) * nrDots;
      }
      
      
          // Name: areaOfWhite - method of class Die
      // Description: A method that calculates the amount of+
    area of
      //   the die that is white
      // Parameters: none
      // Returns: double - area of die that is white
      // Author:Patrick Dunn
      // Date:10/21/00
      // Change log:
      public double areaOfWhite()
      {
      // calculate total area of the white portion
      return dieTotalArea - dotTotalArea;
      }
  
      // Name: whitePercent - a method of class die
      // Description: A method that calculates the amount of+
    area
      //  of the die that is white in terms of percentage
      // Parameters: double - percent of die that is white
      // Returns:a 6 sided die
      // Author:Patrick Dunn
      // Date:10/21/00
      // Change log:
      public double whitePercent()
      {
      // calculate white area percentage
      whiteArea = dieTotalArea - dotTotalArea;
      return (whiteArea / dieTotalArea) * 100;
      }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   [patdunn@k6 objective]$ javac PercentWhite.java
   [patdunn@k6 objective]$ java PercentWhite
   Edge length? 1.79
   The die is 89.00442571243572 percent white.
   [patdunn@k6 objective]$ java PercentWhite
   Edge length? 5.43
   The die is 89.00442571243572 percent white.
   [patdunn@k6 objective]$ 
   */