package length;
/**
* A Length is an object that has a length and a unit, can be converted to
* meters, can be added to other Lengths, and can be compared to other Lengths.
*
* @author Tom Bylander
*/
publicabstractclass Length implements Comparable {
/**
* The length in the units of this object.
*/
privatedouble length;
/**
* Store the length in this Length.
*
* @param length
*/
public Length(double length) {
this.length = length;
}
/**
* This should add the other Length to this Length object.
*
* @param other
*/
publicabstractvoid add(Length other);
/**
* This should return a different String if the length is exactly 1.0.
*
* @return the correct name of the unit of this Length object.
*/
publicabstract String getUnit();
/**
* @return the length in meters
*/
publicabstractdouble toMeters();
/**
* @return the length of this Length object.
*/
publicdouble getLength() {
return length;
}
/**
* Set the length of this Length object.
*
* @param length
* length in the units of this object
*/
publicvoid setLength(double length) {
this.length = length;
}
/**
* Compare this Length object to the other one.
*/
publicint compareTo(Length other) {
if((this.toMeters() - other.toMeters()) < 0)
return -1;
elseif((this.toMeters() - other.toMeters()) > 0)
return 1;
return 0;
}
/**
* @return a String that includes the class name, the length, and the unit.
*/
public String toString() {
returnthis.getClass() + \": \" + getLength() + \" \" + getUnit();
}
}
package length;
publicclass Meter extends Length{
/**
* Parameterized constructor
* @param length
*/
public Meter(double length) {
super(length);
}
@Override
publicvoid add(Length other) {
setLength(other.toMeters() + getLength());
}
@Override
public String getUnit() {
if(getLength() == 1.0)
return \"meter\";
else
return \"meters\";
}
@Override
publicdouble toMeters() {
return getLength();
}
}
package length;
publicclass Inch extends Length {
/**
* 1 inch = 0.0254 meters
*/
publicstaticfinaldoubleMETERS_PER_INCH = 0.0254;
/**
* Parameterized constructor
* @param length
*/
public Inch(double length) {
super(length);
}
@Override
publicvoid add(Length other) {
double otherToInch = other.toMeters() / METERS_PER_INCH;
setLength(otherToInch + getLength());
}
@Override
public String getUnit() {
if(getLength() == 1.0)
return \"inch\";
else
return \"inches\";
}
@Override
publicdouble toMeters() {
return (getLength() * METERS_PER_INCH);
}
}
package length;
publicclass Foot extends Length {
/**
* 1 foot = 0.3048 meters
*/
publicstaticfinaldoubleMETERS_PER_FOOT = 0.3048;
/**
* Parameterized constructor
* @param length
*/
public Foot(double length) {
super(length);
}
@Override
publicvoid add(Length other) {
double otherToFoot = other.toMeters() / METERS_PER_FOOT;
setLength(otherToFoot + getLength());
}
@Override
public String getUnit() {
if(getLength() == 1.0)
return \"foot\";
else
return \"feet\";
}
@Override
publicdouble toMeters() {
return (getLength() * METERS_PER_FOOT);
}
}
package length;
publicclass Yard extends Length.
package length; A Length is an object that has a length and .pdf
1. package length;
/**
* A Length is an object that has a length and a unit, can be converted to
* meters, can be added to other Lengths, and can be compared to other Lengths.
*
* @author Tom Bylander
*/
publicabstractclass Length implements Comparable {
/**
* The length in the units of this object.
*/
privatedouble length;
/**
* Store the length in this Length.
*
* @param length
*/
public Length(double length) {
this.length = length;
}
/**
* This should add the other Length to this Length object.
*
* @param other
*/
publicabstractvoid add(Length other);
/**
* This should return a different String if the length is exactly 1.0.
*
* @return the correct name of the unit of this Length object.
*/
publicabstract String getUnit();
/**
* @return the length in meters
*/
2. publicabstractdouble toMeters();
/**
* @return the length of this Length object.
*/
publicdouble getLength() {
return length;
}
/**
* Set the length of this Length object.
*
* @param length
* length in the units of this object
*/
publicvoid setLength(double length) {
this.length = length;
}
/**
* Compare this Length object to the other one.
*/
publicint compareTo(Length other) {
if((this.toMeters() - other.toMeters()) < 0)
return -1;
elseif((this.toMeters() - other.toMeters()) > 0)
return 1;
return 0;
}
/**
* @return a String that includes the class name, the length, and the unit.
*/
public String toString() {
returnthis.getClass() + ": " + getLength() + " " + getUnit();
}
}
package length;
publicclass Meter extends Length{
/**
6. publicdouble toMeters() {
return (getLength() * METERS_PER_YARD);
}
}
package length;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
publicclass LengthTest {
publicstaticvoid main(String[] args) {
Scanner in = null;
try {
in = new Scanner(new File("data.txt"));
} catch (FileNotFoundException exception) {
thrownew RuntimeException("failed to open data.txt");
}
//ArrayList to hold Length objects
List lengthList = new ArrayList();
while (in.hasNextDouble()) {
double value = in.nextDouble();
String unit = in.next();
Length length = null;
//Create the right type of Length object and store it in length
if(unit.equalsIgnoreCase("meter") || unit.equalsIgnoreCase("meters"))
length = new Meter(value);
elseif(unit.equalsIgnoreCase("inch") || unit.equalsIgnoreCase("inches"))
length = new Inch(value);
elseif(unit.equalsIgnoreCase("foot") || unit.equalsIgnoreCase("feet"))
length = new Foot(value);
elseif(unit.equalsIgnoreCase("yard") || unit.equalsIgnoreCase("yards"))
length = new Yard(value);
System.out.println(length);
//Add length to arraylist
7. lengthList.add(length);
}
// Close scanner object
in.close();
//Display minimum and maximum length
Collections.sort(lengthList);
System.out.println(" Minimum is " + lengthList.get(0));
System.out.println("Maximum is " + lengthList.get(lengthList.size() - 1));
//Sum of Lengths Adding from First to Last
Meter meter = new Meter(0);
Inch inch = new Inch(0);
Foot foot = new Foot(0);
Yard yard = new Yard(0);
for (Length length : lengthList) {
meter.add(length);
inch.add(length);
foot.add(length);
yard.add(length);
}
System.out.println(" Sum of Lengths Adding from First to Last");
System.out.println(meter);
System.out.println(inch);
System.out.println(foot);
System.out.println(yard);
//Sum of Lengths Adding from Last to First
System.out.println(" Sum of Lengths Adding from Last to First");
meter = new Meter(0);
inch = new Inch(0);
foot = new Foot(0);
yard = new Yard(0);
//Reverse list to calculate sum from last to first
Collections.reverse(lengthList);
for (Length length : lengthList) {
meter.add(length);
inch.add(length);
foot.add(length);
8. yard.add(length);
}
System.out.println(meter);
System.out.println(inch);
System.out.println(foot);
System.out.println(yard);
}
}
SAMPLE OUTPUT:
class length.Meter: 1.0 meter
class length.Inch: 1.0 inch
class length.Foot: 1.0 foot
class length.Yard: 1.0 yard
class length.Meter: 401.336 meters
class length.Inch: 15839.0 inches
class length.Foot: 1319.0 feet
class length.Yard: 439.0 yards
Minimum is class length.Inch: 1.0 inch
Maximum is class length.Inch: 15839.0 inches
Sum of Lengths Adding from First to Last
class length.Meter: 1609.344 meters
class length.Inch: 63360.00000000001 inches
class length.Foot: 5280.0 feet
class length.Yard: 1760.0 yards
Sum of Lengths Adding from Last to First
class length.Meter: 1609.3439999999996 meters
class length.Inch: 63360.0 inches
class length.Foot: 5279.999999999999 feet
class length.Yard: 1760.0 yards
Solution
package length;
/**
* A Length is an object that has a length and a unit, can be converted to
* meters, can be added to other Lengths, and can be compared to other Lengths.
9. *
* @author Tom Bylander
*/
publicabstractclass Length implements Comparable {
/**
* The length in the units of this object.
*/
privatedouble length;
/**
* Store the length in this Length.
*
* @param length
*/
public Length(double length) {
this.length = length;
}
/**
* This should add the other Length to this Length object.
*
* @param other
*/
publicabstractvoid add(Length other);
/**
* This should return a different String if the length is exactly 1.0.
*
* @return the correct name of the unit of this Length object.
*/
publicabstract String getUnit();
/**
* @return the length in meters
*/
publicabstractdouble toMeters();
/**
* @return the length of this Length object.
*/
publicdouble getLength() {
10. return length;
}
/**
* Set the length of this Length object.
*
* @param length
* length in the units of this object
*/
publicvoid setLength(double length) {
this.length = length;
}
/**
* Compare this Length object to the other one.
*/
publicint compareTo(Length other) {
if((this.toMeters() - other.toMeters()) < 0)
return -1;
elseif((this.toMeters() - other.toMeters()) > 0)
return 1;
return 0;
}
/**
* @return a String that includes the class name, the length, and the unit.
*/
public String toString() {
returnthis.getClass() + ": " + getLength() + " " + getUnit();
}
}
package length;
publicclass Meter extends Length{
/**
* Parameterized constructor
* @param length
*/
public Meter(double length) {
super(length);
14. import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
publicclass LengthTest {
publicstaticvoid main(String[] args) {
Scanner in = null;
try {
in = new Scanner(new File("data.txt"));
} catch (FileNotFoundException exception) {
thrownew RuntimeException("failed to open data.txt");
}
//ArrayList to hold Length objects
List lengthList = new ArrayList();
while (in.hasNextDouble()) {
double value = in.nextDouble();
String unit = in.next();
Length length = null;
//Create the right type of Length object and store it in length
if(unit.equalsIgnoreCase("meter") || unit.equalsIgnoreCase("meters"))
length = new Meter(value);
elseif(unit.equalsIgnoreCase("inch") || unit.equalsIgnoreCase("inches"))
length = new Inch(value);
elseif(unit.equalsIgnoreCase("foot") || unit.equalsIgnoreCase("feet"))
length = new Foot(value);
elseif(unit.equalsIgnoreCase("yard") || unit.equalsIgnoreCase("yards"))
length = new Yard(value);
System.out.println(length);
//Add length to arraylist
lengthList.add(length);
}
// Close scanner object
in.close();
//Display minimum and maximum length
15. Collections.sort(lengthList);
System.out.println(" Minimum is " + lengthList.get(0));
System.out.println("Maximum is " + lengthList.get(lengthList.size() - 1));
//Sum of Lengths Adding from First to Last
Meter meter = new Meter(0);
Inch inch = new Inch(0);
Foot foot = new Foot(0);
Yard yard = new Yard(0);
for (Length length : lengthList) {
meter.add(length);
inch.add(length);
foot.add(length);
yard.add(length);
}
System.out.println(" Sum of Lengths Adding from First to Last");
System.out.println(meter);
System.out.println(inch);
System.out.println(foot);
System.out.println(yard);
//Sum of Lengths Adding from Last to First
System.out.println(" Sum of Lengths Adding from Last to First");
meter = new Meter(0);
inch = new Inch(0);
foot = new Foot(0);
yard = new Yard(0);
//Reverse list to calculate sum from last to first
Collections.reverse(lengthList);
for (Length length : lengthList) {
meter.add(length);
inch.add(length);
foot.add(length);
yard.add(length);
}
System.out.println(meter);
System.out.println(inch);
System.out.println(foot);
16. System.out.println(yard);
}
}
SAMPLE OUTPUT:
class length.Meter: 1.0 meter
class length.Inch: 1.0 inch
class length.Foot: 1.0 foot
class length.Yard: 1.0 yard
class length.Meter: 401.336 meters
class length.Inch: 15839.0 inches
class length.Foot: 1319.0 feet
class length.Yard: 439.0 yards
Minimum is class length.Inch: 1.0 inch
Maximum is class length.Inch: 15839.0 inches
Sum of Lengths Adding from First to Last
class length.Meter: 1609.344 meters
class length.Inch: 63360.00000000001 inches
class length.Foot: 5280.0 feet
class length.Yard: 1760.0 yards
Sum of Lengths Adding from Last to First
class length.Meter: 1609.3439999999996 meters
class length.Inch: 63360.0 inches
class length.Foot: 5279.999999999999 feet
class length.Yard: 1760.0 yards