



|
|
CS1 Course Site
|
Programming Challenge Archive
Shapes World Problem Solving
Bluered Polygonal Worm
|
|
The Problem
: Bluered Polygonal Worm. A ``bluered polygonal worm'' develops in the fllowing fashion.
- After three years it consists of a blue equilateral triangle of a particular side length From this point on, the ``side length'' of the worm is fixed.
- After four years the worm consists of the blue triangle and a new blue and red square. The square has a blue background and contains a circle wich comes to within 3 units of the midpoint of each side. The two main shapes are coincident along one edge.
- After five years it consists of the blue triangle, the blue and red square and a new pentagon. The pentagon is blue with a red circle in its interior (which comes to within 3 units of he midpoint of each side. The new pentagon attaches itself coincidently (edge-to-edge) to the square.
- Growth continues in a similar fashion, year by year.
Interestingly, each red circle ends up so that its boundary comes to within three units of the midpoint of each side. If the side of a bluered polygonal worm is 14.5 units after three years, what will be its blue area after 7 years?
JavaApplication --
Bluered Polygonal Worm
// General Information
// ---------------------------------------------------
// File: BlueredWormApp.java
// Type: java application file
// Date: Thu Sep 28, 2000
// Name: James W. Bremenour
// Line: Calculates a specific area
// Application Description
// ---------------------------------------------------
/*
Calculates the specific area of the blue parts of a
worm that changes over time.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class BlueredWormApp
{
static public void main (String args[])
{
// Set the side of the bluered polygonal worm
double wormSide = 14.5;
// Determine how the worm changes after 3 years
Triangle blueTri = new Triangle(wormSide);
// Determine how the worm changes after 4 years
Square blueSqr = new Square(wormSide);
// Create the red inscribed circle
Circle helperCrc4 =
blueSqr.inscribingCircle();
double redCrc4Radius =
(helperCrc4.radius() - 3.0);
Circle redCrc4 = new Circle(redCrc4Radius);
// Find the blue area of the square - the
// red circle
double blueAreaYr4 =
(blueSqr.area()-redCrc4.area());
// Determine how the worm changes after 5 years
Polygon bluePnt = new Polygon(5,wormSide);
// Create the red inscribed circle
Circle helperCrc5 =
bluePnt.inscribingCircle();
double redCrc5Radius =
(helperCrc5.radius() - 3.0);
Circle redCrc5 = new Circle(redCrc5Radius);
// Find the blue area of the pentagon - the
// red circle
double blueAreaYr5 =
(bluePnt.area()-redCrc5.area());
// Determine how the worm changes after 6 years
Polygon blueHex = new Polygon(6,wormSide);
// Create the red inscribed circle
Circle helperCrc6 =
blueHex.inscribingCircle();
double redCrc6Radius =
(helperCrc6.radius() - 3.0);
Circle redCrc6 = new Circle(redCrc6Radius);
// Find the blue area of the hexagon - the
// red circle
double blueAreaYr6 =
(blueHex.area()-redCrc6.area());
// Determine how the worm changes after 7 years
Polygon blueHpt = new Polygon(7,wormSide);
// Create the red inscribed circle
Circle helperCrc7 =
blueHpt.inscribingCircle();
double redCrc7Radius =
(helperCrc7.radius() - 3.0);
Circle redCrc7 = new Circle(redCrc7Radius);
// Find the blue area of the heptagon - the
// red circle
double blueAreaYr7 =
(blueHpt.area()-redCrc7.area());
// Calculate total blue area
double totalBlueArea =
(blueAreaYr4 + blueAreaYr5 +
blueAreaYr6 + blueAreaYr7);
// Display the result
IO.print("The blue area of the worm is: ");
IO.print(totalBlueArea + " square units");
IO.println();
}
}
// Demo
// ---------------------------------------------------
/*
$ java BlueredWormApp
The blue area of the worm is: 929.0128042044735 square un+
its
$
*/
|
|
|