



|
|
My Intro to Object-Oriented Programming
|
Programming Challenge Archive
Programming Assignment # 2
Alien Chessman
|
|
|
An alien chessman descends upon a chessboard measuring 19.6 inches square. The alien makes a complete straight-line tour from square A1 to E4 to G3 to H8 to B5 and back to A1. How far in inches did the alien travel?
JavaApplication - Programming Assignment # 2 --
Alien Chessman
// General Information
// ---------------------------------------------------
// File: AlienChessmanApp.java
// Type: java application file
// Date: Mon Sep 25, 2000
// Name: Jeffrey R Norkoli
// Line: Find the distance travelled by the alien
// chessman.
// Application Description
// ---------------------------------------------------
/*
An alien chessman descends upon a chessboard
measuring 19.6 inches square. The alien makes
a complete straight-line tour from square A1
to E4 to G3 to H8 to B5 and back to A1. How
far in inches did the alien travel?
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.shapes.*;
// Application Class
// ---------------------------------------------------
class AlienChessmanApp
{
static public void main (String args[])
{
// Each path between points can be considered
// the diagonal of a rectangle. It will be
// assumed that the alien travels from the
// center of one square to the center of
// another. Calculate the length of one square
// of the chessboard.
double lengthOfSide = 19.6;
double lengthOfSmallSquare = lengthOfSide/8.0;
double lOSS = lengthOfSmallSquare;
// Leg1: Model path from A1 to E4 and calculate
// diagonal. Use x and y coordinates.
double xFromA1ToE4 = lOSS * 4;
double yFromA1ToE4 = lOSS * 3;
Rectangle leg1 = new Rectangle(xFromA1ToE4,
yFromA1ToE4);
double lengthLeg1 = leg1.diagonal();
// Leg2: Model path from E4 to G3 and calculate
// diagonal. Use x and y coordinates.
double xFromE4ToG3 = lOSS * 2;
double yFromE4ToG3 = lOSS * 1;
Rectangle leg2 = new Rectangle(xFromE4ToG3,
yFromE4ToG3);
double lengthLeg2 = leg2.diagonal();
// Leg3: Model path from G3 to H8 and calculate
// diagonal. Use x and y coordinates.
double xFromG3ToH8 = lOSS * 1;
double yFromG3ToH8 = lOSS * 5;
Rectangle leg3 = new Rectangle(xFromG3ToH8,
yFromG3ToH8);
double lengthLeg3 = leg3.diagonal();
// Leg4: Model path from H8 to B5 and calculate
// diagonal. Use x and y coordinates.
double xFromH8ToB5 = lOSS * 6;
double yFromH8ToB5 = lOSS * 3;
Rectangle leg4 = new Rectangle(xFromH8ToB5,
yFromH8ToB5);
double lengthLeg4 = leg4.diagonal();
// Leg5: Model path from B5 to A1 and calculate
// diagonal. Use x and y coordinates.
double xFromB5ToA1 = lOSS * 1;
double yFromB5ToA1 = lOSS * 4;
Rectangle leg5 = new Rectangle(xFromB5ToA1,
yFromB5ToA1);
double lengthLeg5 = leg5.diagonal();
// Compute the total distance traveled by the
// alien.
double totalDistance = lengthLeg1 + lengthLeg2
+ lengthLeg3 + lengthLeg4 + lengthLeg5;
// Display result.
IO.println ("The distance travelled is " +
totalDistance + " inches.");
}
}
// Demo
// ---------------------------------------------------
/*
$ javac AlienChessmanApp.java
$ java AlienChessmanApp
The distance travelled is 56.75767277056354 inches.
$
*/
|
|
|