



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Shapes World Programming Challenge
Polygonal Worm
|
|
Java Application --
Polygonal Worm
// General Information
// ---------------------------------------------------
// File: TheWorm.java
// Type: java application file
// Date: Sat Sep 30, 2000
// Name: Patrick Dunn
// Line: Finds the area of a blue-red worm after 7
// years.
// Application Description
// ---------------------------------------------------
/*
This program utilizes the shapes class to find the area
of a growing blue-red polygonal worm after 7 years.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class TheWorm
{
static public void main (String args[])
{
// init the side length of the shapes 14.5 units
double sideLength = 14.5;
// create the first triangle (it's blue)
Triangle blueTriangle = new Triangle(sideLength);
// get the area of the triangle
double totalBlueArea = blueTriangle.area();
// construct the square of 14.5 units
Square theBlueSquare = new Square(sideLength);
// get the inscribing circle
Circle theInscribingCircle1 = theBlueSquare.inscribingC+
ircle();
// subtract the area of the inscribing circle from the
// area of the square
totalBlueArea = totalBlueArea + (theBlueSquare.area() -+
theInscribingCircle1.area());
// create a 5 sided polygon of side length 14.5 units
Polygon polygonOfFive = new Polygon(5,sideLength);
// get the inscribing circle of the polygon
Circle theInscribingCircle2 = polygonOfFive.inscribingC+
ircle();
// take the area of the polygon and subtract from the a+
rea of
// the inscribing circle and add to the total blue area+
.
totalBlueArea = totalBlueArea + (polygonOfFive.area() -+
theInscribingCircle2.area());
// create a 6 sided polygon of side length 14.5 units
Polygon polygonOfSix = new Polygon(5,sideLength);
// get the inscribing circle of the polygon
Circle theInscribingCircle3 = polygonOfSix.inscribingCi+
rcle();
// take the area of the polygon and subtract from the a+
rea of
// the inscribing circle and add to the total blue area+
.
totalBlueArea = totalBlueArea + (polygonOfSix.area() - +
theInscribingCircle3.area());
// create a 7 sided polygon of side length 14.5 units
Polygon polygonOfSeven = new Polygon(7,sideLength);
// get the inscribing circle of the polygon
Circle theInscribingCircle4 = polygonOfSeven.inscribing+
Circle();
// take the area of the polygon and subtract from the a+
rea of
// the inscribing circle and add to the total blue area+
.
totalBlueArea = totalBlueArea + (polygonOfSeven.area() +
-
theInscribingCircle4.area());
// display the total area
IO.println("The blue area of the worm is " + totalBlueA+
rea +
" square units");
}
}
// Demo
// ---------------------------------------------------
/*
<?DEMO>
*/
|
|
|