



|
|
CS1 Course Site
|
Programming Challenge Archive
Shapes World Problem Solving
The Tour of the Alien Chessman
|
|
The Problem
: The Tour of the Alien Chessman. An alien chessman descends upon a chess board which measures 19.6 inches per side. This alien chess creature makes a complete straight line tour from square A1 to square E4 to square G3 to square H8 to square B5 and back to square A1. How far, in inches, did the creature move in his tour?
JavaApplication --
The Tour of the Alien Chessman
// General Information
// ---------------------------------------------------
// File: AlienChessmanApp.java
// Type: java application file
// Date: Wed Sep 27, 2000
// Name: James W. Bremenour
// Line: Calculates how far an alien chessman travels
// Application Description
// ---------------------------------------------------
/*
This program calculates how far an Alien Chessman
travels on his tour of one of our chess boards. The
alien does not move according to the rules of chess
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
import blue.math.*;
// Application Class
// ---------------------------------------------------
class AlienChessmanApp
{
static public void main (String args[])
{
// Create the chess board
Square board = new Square(19.6);
// Find the side of one square of the board
// ss = small square
double ssSide =
Math.sqrt(board.area()/16.0);
// Find the distance of A1-E4
Rectangle a1e4Rect =
new Rectangle((4*ssSide),(3*ssSide));
double a1e4Dist = a1e4Rect.diagonal();
// Find the distance of E4-G3
Rectangle e4g3Rect =
new Rectangle((2*ssSide),ssSide);
double e4g3Dist = e4g3Rect.diagonal();
// Find the distance of G3-H8
Rectangle g3h8Rect =
new Rectangle(ssSide,(5*ssSide));
double g3h8Dist = g3h8Rect.diagonal();
// Find the distance of H8-B5
Rectangle h8b5Rect =
new Rectangle((6*ssSide),(3*ssSide));
double h8b5Dist = h8b5Rect.diagonal();
// Find the distance of B5-A1
Rectangle b5a1Rect =
new Rectangle(ssSide,(4*ssSide));
double b5a1Dist = b5a1Rect.diagonal();
// Calculate the total distance traveled
double totalDist =
(a1e4Dist + e4g3Dist + g3h8Dist +
h8b5Dist + b5a1Dist);
// Display the results
IO.print("The total distance traveled is:");
IO.print(" ");
IO.print(totalDist + " square inches");
IO.println();
}
}
// Demo
// ---------------------------------------------------
/*
$ java AlienChessmanApp
The total distance traveled is: 113.51534554112708 square+
inches
$
*/
|
|
|