California State University, Dominguez Hills

Department of Computer Science

Spring 2005

CSC121A Lab 05:  Fundamental Data Types

 

Objectives:

1.      To understand and use different primitive data types

2.      To be able to write math expressions in Java

3.      To enhance the understanding the test class and constants

4.      To be able to use input dialog

 

Exercises P3.9 on Page 128

 

      Implement a class SodaCan whose constructor receives the height and diameter of the soda can. Supply methods getVolume and getSurfaceArea. Supply a SodaCanTest class that tests your class.

 

Requirements:

 

1.      Create a project named CSC121A-05A in the BlueJ.

2.      Create a class SodaCan under the this project, and type the source code that implements the requirements of Exercises P3.9. Save this source code.

3.      Create a test class SodaCanTest, and type the source code that only contains the main method. Your main method should

a.       Pop out an input dialog window to ask the user to input two parameters for the height and the diameter of the soda can, respectively;

b.      Create an object aCan of SodaCan with the user-specified height and diameter of the soda can;

c.       Call aCan.getVolume() and aCan.getSurfaceArea();

d.      Print out the volume and surface area.

4.      Compile all source code and run the byte code with different sizes of soda cans.

5.      Create all documentation files with javadoc commands. Use web browser netscape to display your documentation.

 

Class Design:

1.      The class SodaCan:

§         Instance fields:

                                                               i.      Double height – the height of the soda can

                                                             ii.      Double radius – the radius of the soda can

§         Constructors

                                                               i.      a single constructor with two parameters of height and diameter

                                                             ii.      inilializes height and computes the radius using the diameter

§         Constants:

                                                               i.      PI = 3.14

§         Methods:

                                                               i.      public double getSurfaceArea()

                                                             ii.      public double getVolume()

 

2.      The class SodaCanTest:

§         Only a main method that satisfies the requirement

 

Implementation:

 

/**

  * Calculate the volume and surface area of soda cans

  *

  * @author ---

  * @date ---

  */

public class SodaCan

{

      /**

      * Define constants

      */

      public final double PI = 3.1415926;

     

      /**

       * Define instance fields

       */

      private double height;

      private double radius;

     

      /**

       * Constructor: receives the height and diameter

       * @param double h the height of the soda can

       * @param double d the diameter of the soda can

       */  

      public SodaCan(double h, double d) {

            // to be complete

      }

     

      /**

       * Calculates the volume of the soda can

       * @return double the valume of the soda can 

       */

      public double getVolume()

      {

            // to be complete

      }

     

      /**

       * calculates the surface of the soda can

       * @param return double the surface of the soda can

       */

      public double getSurfaceArea()

      {

            // to be complete

      }    

}

 

/**

  * Test class to the SodaCan class

  * @author ---

  * @date ---

  */

public class SodaCanTest

{

      public final static void main(String[] args)

      {

            // to be complete

      }    

}

 

 

Documentation Standard

 

Your source code documentation must follow the instructions of Chapter 2.8, pages 54-56.

Be advised to briefly comment all classes, constructors, and methods.