P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
Programming Challenge Archive

Shapes World Programming Challenge
Alien Chessman
 
 
  Java Application  -- AlienChessmanApp

   // General Information
   // ---------------------------------------------------
  
   // File:  AlienChessmanApp.java
   // Type:  java application file
   // Date:  Wed Sep 27, 2000
   // Name:  Patrick Dunn
   // Line:  The alien chessman visits us
  
   // Application Description
   // ---------------------------------------------------
  
   /*
     This application is to find the total distance traveled
     by an Alien chessman who visits us from the planet
     fooBarShoo - a remote planet owned by Marriott corporati+
   on
     in the Wal-Mart galaxy far far away.  Can you tell I've
     been up too long today? ;-)
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.shapes.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class AlienChessmanApp
      {
         static public void main (String args[])
         {
     // declare some variables
     double sideOfChessBoard = 19.6;  // side of the chessbo+
   ard
  
     // create the chess board
     Square chessBoard = new Square(sideOfChessBoard);
  
     // obtain the total area of the chessboard
     double areaOfBoard = chessBoard.area();
  
     // take the total area and divide by 64 to obtain the a+
   rea for each
     // square
     double eachSquare = areaOfBoard / 64;
  
     // now that we have the info we need lets begin the fun+
   !
     
     // The first move from A1 to E4
     // construct a rectangle of 5 squares length and 4 squa+
   res width
     Rectangle move1 = new Rectangle((eachSquare * 5),(eachS+
   quare * 4));
     
     // obtain the diagonal of this move and we'll have the +
   distance
     // traveled by our friendly alien
     double move1Traveled = move1.diagonal();
  
     // add to our grand total
     double tourLength = move1Traveled;
  
     // The second move from E4 to G3
     // construct a rectangle of 3 squares length and 1 squa+
   res width
     Rectangle move2 = new Rectangle((eachSquare * 3),eachSq+
   uare);
     
     // obtain the diagonal of this move and we'll have the +
   distance
     // traveled by our friendly alien
     double move2Traveled = move2.diagonal();
  
     // add to our grand total
     tourLength = tourLength + move2Traveled;
  
     // The third move from G3 to H8
     // construct a rectangle of 1 squares length and 5 squa+
   res width
     Rectangle move3 = new Rectangle(eachSquare,(eachSquare +
   *5));
     
     // obtain the diagonal of this move and we'll have the +
   distance
     // traveled by our friendly alien
     double move3Traveled = move3.diagonal();
  
     // add to our grand total
     tourLength = tourLength + move3Traveled;
  
     // The fourth move from H8 to B5
     // construct a rectangle of 7 squares length and 4 squa+
   res width
     Rectangle move4 = new Rectangle((eachSquare * 7),(eachS+
   quare * 4));
     
     // obtain the diagonal of this move and we'll have the +
   distance
     // traveled by our friendly alien
     double move4Traveled = move4.diagonal();
  
     // add to our grand total
     tourLength = tourLength + move4Traveled;
  
     // The fifth move from B5 to A1
     // construct a rectangle of 1 squares length and 4 squa+
   res width
     Rectangle move5 = new Rectangle(eachSquare,(eachSquare +
   * 4));
     
     // obtain the diagonal of this move and we'll have the +
   distance
     // traveled by our friendly alien
     double move5Traveled = move5.diagonal();
  
     // add to our grand total
     tourLength = tourLength + move5Traveled;
  
     // now display the moves and how far the alien moved
     // on each along with the total
    
     IO.println("The alien chessman visits our chessboard.")+
   ;
     IO.println("He decides to tour it and here are his move+
   s and how far he traveled.");
  
     IO.println("Move 1: A1 to E4 - distance traveled: " + m+
   ove1Traveled + " inches");
     IO.println("Move 2: E4 to G3 - distance traveled: " + m+
   ove2Traveled + " inches");
     IO.println("Move 3: G3 to H8 - distance traveled: " + m+
   ove3Traveled + " inches");
     IO.println("Move 4: H8 to B5 - distance traveled: " + m+
   ove4Traveled + " inches");
     IO.println("Move 5: B5 to A1 - distance traveled: " + m+
   ove5Traveled + " inches");
     IO.println("-------------------------------------------+
   -------");
     IO.println("Total distance traveled is " + tourLength ++
    " inches");
         }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   $ java AlienChessmanApp
   The alien chessman visits our chessboard.
   He decides to tour it and here are his moves and how far h+
   e traveled.
   Move 1: A1 to E4 - distance traveled: 38.43475323519068 in+
   ches
   Move 2: E4 to G3 - distance traveled: 18.981571655160703 i+
   nches
   Move 3: G3 to H8 - distance traveled: 30.6068646303407 inc+
   hes
   Move 4: H8 to B5 - distance traveled: 48.393702134162055 i+
   nches
   Move 5: B5 to A1 - distance traveled: 24.748941517770014 i+
   nches
   --------------------------------------------------
   Total distance traveled is 161.16583317262416 inches
   $ 
   */