P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
Programming Challenge Archive

General Problems Solving
Columns of Cubes
 
 
  Java Application  -- Cubes

   // General Information
   // ---------------------------------------------------
  
   // File:  Cubes.java
   // Type:  java application file
   // Date:  Fri Sep 15, 2000
   // Name:  Patrick Dunn
   // Line:  A program to find the volume of a structure base+
   d on the area
  
   // Application Description
   // ---------------------------------------------------
  
   /*
     Calculates the volume of a structure of 5 cubes in a col+
   umn.
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.math.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class Cubes
      {
         static public void main (String args[])
         {
     // define variables and constants
     double areaOfStructure = 587.785;   // area of structu+
   re
     double areaOfOneCube;               // area of one cub+
   e       
     double sideLengthOfOneCube;         // side length of +
   one cube
     double totalHeight;                 // total height of+
    the structure
     double widthOfOneCube;              // wide of the str+
   ucture
     double volumeOfStructure;           // volume of the s+
   tructure
     int numOfCubes = 5;                 // number of cubes+
  
  
     // intro
     IO.println();
     IO.println("Volume of a Column of Cubes");
     IO.println("---------------------------");
     IO.println("Area of the Structure - " + areaOfStructur+
   e + " square feet.");
     
     // calculate area of one cube
     areaOfOneCube = areaOfStructure / numOfCubes;
  
     // find the side length of one cube
         sideLengthOfOneCube = Math.sqrt(areaOfOneCube);
               
             // assign the width of one cube = side length of+
    one cube
     widthOfOneCube = sideLengthOfOneCube;
  
     // find the total height of the structure
             totalHeight = sideLengthOfOneCube * numOfCubes;
  
     // calcuate the volume
             volumeOfStructure = sideLengthOfOneCube * widthO+
   fOneCube * totalHeight;
  
     // display
     IO.println("Volume of the 5 cube structure " + volumeO+
   fStructure);
     
  
         }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   [patdunn@k6 cubes]$ javac Cubes.java
   [patdunn@k6 cubes]$ java Cubes
  
   Volume of a Column of Cubes
   ---------------------------
   Area of the Structure - 587.78499999999997 square feet.
   Volume of the 5 cube structure 6372.9827969477774
   [patdunn@k6 cubes]$ 
   */