//  Scott DeRuiter             6/28/2002
//  Box.java
//  We will write a class that can be used by client classes
//  to create objects that model a box

public class Box   
{		
	//    data members
	private double length, width, height;
	private String dimension, name;

	//  Constructor
	public Box ( double l, double w, double h, String d, String n )  
	{
		length = l;
		width = w;
		height = h;
		dimension = d;
		name = n;
	}

	//  A method to print out info about the box.
	public void PrintInfo ( )   
	{		
		System.out.println ( "\n\nHere is the info for " + name );
		System.out.println ( "\n\nlength =    " + length );
		System.out.println ( "width =     " + width );
		System.out.println ( "height =    " + height );
		System.out.println ( "dimension = " + dimension );
	}

	//   A method to calculate and return the volume of the box.
	public double Volume ( )   
	{
		double vol = length * width * height;
		return vol;
	}

	//  To calculate and return the surface area.
	public double SurfaceA ( )   
	{
		return ( 2.0 * length * width + 2.0 * length * height + 2.0 * width * height );
	}
}

//  Scott DeRuiter       6/28/2002
//  BoxClient.java
//  Uses the Box class to create some boxes
//  7/15/2011 - Greenstein: Changed TextReader to Scanner

import java.util.Scanner;

public class BoxClient   
{
	public static void main ( String [] args )   
	{
		Scanner console = new Scanner ( System.in );
		System.out.print ( "\n\nEnter the length ->  " );
		double len = console.nextDouble ( );
		Box present = new Box ( len, 15.345, 5.5, "inches", "present" );
		present.PrintInfo ( );
		System.out.println ( "\n\nThe present has a volume of " +
			present.Volume ( ) + "\n\n" );		
		System.out.println ( "\n\nThe present has a surface area of " +
			present.SurfaceA ( ) + "\n\n" );

		Box melodybox = new Box ( 13.3, 5.75, 8.32, "feet", "Melody's Box" ); 		
		melodybox.PrintInfo ( );
	}
}

Back to Lesson 4 Examples

Back to Java Main Page