Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Programming Assignment # 2
Bluered Polygonal Worm
 
A ``bluered polygonal worm'' grows by adding different geometric shapes to its base triangle.   After the base triangle (at year 3),  a new shape is added each year;  first a square is added,  then a pentagon,  and then the process repeats.   The triangles are colored all blue,  but the squares and pentagons have red circles on top of a blue background.   Given that the circles come to within 3 units of the midpoint of their circumscribing shapes,  what is the total blue area of the worm after seven years?  

 
  JavaApplication - Programming Assignment # 2  -- Bluered Polygonal Worm

   // General Information
   // ---------------------------------------------------
  
   // File:  PolygonalWormApp.java
   // Type:  java application file
   // Date:  Fri Sep 29, 2000
   // Name:  Jeffrey R Norkoli
   // Line:  Find the blue area of a worm produced 
   //        with polygons.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
      A "bluered polygonal worm" grows by adding 
      different geometric shapes to its base triangle.  
      After the base triangle (at year 3), a new shape 
      is added each year; first a square is added, then 
      a pentagon, and then the process repeats.  The 
      triangles are colored all blue, but the squares 
      and pentagons have red circles on top of a blue 
      background.  Given that the circles come to 
      within 3 units of the midpoint of their 
      circumscribing shapes, what is the total blue 
      area of the worm after seven years?
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.shapes.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class PolygonalWormApp
      {
         static public void main (String args[])
         {
         
         // Record known data - the side length of the 
         // equillateral triangle.  Since the side 
         // length of the triangle is common to all 
         // shapes, I will rename it accordingly.
         double triangleSide = 14.5;
         double commonSide = triangleSide;
  
         // Model the triangle at year three and 
         // calculate its blue area.
         Triangle yr3Triangle = new Triangle(commonSide);
         double yr3BlueArea = yr3Triangle.area();
  
         // Model the square at year four.
         Square yr4Square = new Square(commonSide);
         
         // Model the circle within the year four square.
         double yr4CircleRadius = commonSide/2.0 - 3.0;
         Circle yr4Circle = new Circle(yr4CircleRadius);
  
         // Calculate the blue area for the year 
         // four square.
         double yr4BlueArea = yr4Square.area() 
          - yr4Circle.area();
  
         // Model the pentagon at year five.
         Polygon yr5Pentagon = new Polygon(5,commonSide);
  
         // Model the circle within the year 
         // five pentagon.
         Circle yr5HelperCircle = 
           yr5Pentagon.inscribingCircle();
         double radiusHelper = yr5HelperCircle.radius();
         double yr5CircleRadius = radiusHelper - 3.0;
         Circle yr5Circle = new Circle(yr5CircleRadius);
  
         // Calculate the blue area of the year 
         // five pentagon.
         double yr5BlueArea = yr5Pentagon.area() 
           - yr5Circle.area();
  
         // Since the bluered polygon worm's year 
         // six shape is a repeat of the cycle - a 
         // triangle - the year six blue area is 
         // equal to the blue area of year three.
         double yr6BlueArea = yr3BlueArea;
  
         // Following the cycle given to us, the 
         // year seven shape will be a repeat of 
         // the year four shape - a square.
         double yr7BlueArea = yr4BlueArea;
  
         // Total the blue area.
         double blueAreaTotal = yr3BlueArea + yr4BlueArea 
          + yr5BlueArea + yr6BlueArea + yr7BlueArea;
  
         // Display the result.
         IO.println ("The blue area of the worm is " 
     + blueAreaTotal + " square units.");
  
         }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   $ javac PolygonalWormApp.java
   $ java PolygonalWormApp
   The blue area of the worm is 697.8165154018677 
   square units.
   $ 
   */