Wednesday, May 13, 2009

LAB 14

Lab 14 Be sure to put each class in its own file. Add the “weight” property to the box. Accept the property values from the console and then display them. Put the whole thing in a loop. Enter ‘q’ to quit. Display a message if a number is not entered.

// Fig. 9.6: BoxTest.cs
// Indexers provide access to a Box object's members.
using System;

public class BoxTest
{
public static void Main( string[] args )
{
// create a box
Box box = new Box( 30, 30, 30, 30 );
string inputkey;
// show dimensions with numeric indexers
Console.WriteLine( "Created a box with the dimensions:" );
Console.WriteLine( "box[ 0 ] = {0}", box[ 0 ] );
Console.WriteLine( "box[ 1 ] = {0}", box[ 1 ] );
Console.WriteLine( "box[ 2 ] = {0}", box[ 2 ] );
Console.WriteLine("box[ 3]= {0}", box[3]);

inputkey = "n";
while (inputkey != "q" && inputkey != "Q")
{

// show dimensions with string indexers
Console.WriteLine("Welcome to UPS Center!");
Console.WriteLine("Please enter the Width");
box["width"] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the length");
box["length"] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the height");
box["height"] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the weight");
box["weight"] = Convert.ToDouble(Console.ReadLine());

Console.WriteLine( "Now the box has the dimensions:" );
Console.WriteLine( "box[ \"length\" ] = {0}", box[ "length" ] );
Console.WriteLine( "box[ \"width\" ] = {0}", box[ "width" ] );

Console.WriteLine( "box[ \"height\" ] = {0}", box[ "height" ] );
Console.WriteLine("box[ \"weight\" ] = {0}", box["weight"]);

Console.WriteLine("Press Enter to continue, Press q to quit!");
inputkey = Convert.ToString(Console.ReadLine());

Console.WriteLine("");

} //while loop
Console.WriteLine("Thank you!");
} // end method Main
} // end class BoxTest


// Fig. 9.5: Box.cs
// Box class definition represents a box with length,
// width and height dimensions with indexers.
public class Box
{
private string[] names = { "length", "width", "height", "weight"};
private double[] dimensions = new double[ 4];

// constructor
public Box( double length, double width, double height, double weight )
{
dimensions[ 0 ] = length;
dimensions[ 1 ] = width;
dimensions[ 2 ] = height;
dimensions[3] = weight;
}

// indexer to access dimensions by integer index number
public double this[ int index ]
{
get
{
// validate index to get
if ( ( index < 0 ) || ( index >= dimensions.Length ) )
return -1;
else
return dimensions[ index ];
} // end get
set
{
if ( index >= 0 && index < dimensions.Length )
dimensions[ index ] = value;
} // end set
} // end numeric indexer

// indexer to access dimensions by their string names
public double this[ string name ]
{
get
{
// locate element to get
int i = 0;
while ( ( i < names.Length ) &&
( name.ToLower() != names[ i ] ) )
i++;

return ( i == names.Length ) ? -1 : dimensions[ i ];
} // end get
set
{
// locate element to set
int i = 0;
while ( ( i < names.Length ) &&
( name.ToLower() != names[ i ] ) )
i++;

if ( i != names.Length )
dimensions[ i ] = value;
} // end set
} // end string indexer
} // end class Box

No comments:

Post a Comment