CS 1063  Introduction to Computer Programming  Project 1

The Length Class

Objectives

Hand-in Requirements

The project will be submitted electronically through WebCT.  Zip up your entire project folder for submission.  (Right click on the project folder, follow the Send To link and select Compressed (zipped) Folder.)  Once the compression process is done, you will have a zipped file with the same name as your project folder and an extension of .zip.  This zipped file will be uploaded to the WebCT system during project submission.  The project folder should include the following files:

Details

You have been asked to write a program that performs some unit conversions from the metric system (the International System of Units) to U.S. units.  The first project will focus on converting from meters to inches, feet, yards, and miles.  One yard is defined to be equal to 0.9144 meters (NIST Guide to SI Units - Appendix B. Conversion Factors).  Converting to inches, feet, and miles can be performed based on knowing that 12 inches equals a foot, 3 feet equals a yard, and 1760 yards equals a mile.  For example, to convert 1000 meters (a kilometer) to a mile.

The first step divides by 0.9144 because 1 yard = 0.9144 meters implies that 1 meter = (1 / 0.9144) yards.  Similarly, 1 mile = 1760 yards implies that 1 yard = (1 / 1760) miles.

Writing the Length Class

Although the class has an instance field for the unit of the measurement, the methods in this project will assume that the unit is meters.

Instance fields:
myNumber - a double value representing the number of the measurement
myUnit - a String value representing the unit of the measurement
Constructors:
/* The constructor has two parameters: */
/* the number and the unit of the measurement. */
public Length(double number, String unit)
Methods:

/* getInches returns the number of inches */
public double getInches()

/* getFeet returns the number of feet */
public double getFeet()

/* getYards returns the number of yards */
public double getYards()

/* getMiles returns the number of miles */
public double getMiles()

/* setNumber sets the number of the measurement to a new value */
public void setNumber(double number)

/* setUnit sets the unit of the measurement to a new value */
public void setUnit(String unit)

/* toString returns a string describing the measurement. */
public String toString()

Testing your Length class:

  1. Initialize and construct a Length object named length1 with 5.02921 and "meters".

  2. Create a variable named inch1 that contains a double value.  In the variable, store the number of inches for length1 obtained using the getInches method.

  3. Print the number of feet of length1 using the getFeet method.  Be sure to use a descriptive label.

  4. Print the value of inch1.  Be sure to use a descriptive label.

  5. Print length1 using the toString method.

  6. Change the number of length1 to 1852 using the setNumber method.

  7. Change the unit of length1 to "yards" using the setUnit method.

  8. Print length1 using the toString method.

  9. Print the number of yards of length1 using the getYards method.  Be sure to use a descriptive label.

  10. Print the number of miles of length1 using the getMiles method.  Be sure to use a descriptive label.