Java Programming Home Page: Archive: Message #85

Date: Sep 29 2000 13:15:10 EDT
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: hw2

Hi All,

Based on feedback from students, I have written

an untested sketch of a new HW 2.

> Here is a problem that I borrowed from the Aerospace

> Industry:)

> 

> Make up an Interface for a quantity that can be

> represented as either

> metric or English.

> 

> You need to set the metric conversion using:

> setMetric(boolean);

> 

> You can get the metric property too:

> boolean getMetric();

> Units are very important, and are READ ONLY;

> String getUnits();

> 

> Values are settable and gettable using:

> void setValue(double )

> double  getValue()

> 

>   Write two sample classes that implement your

> interface. One

> should be for units of length (i.e., meters vs

> yards) while the

> other should be for mass (i.e., kilograms vs lbs).

> 

> Repeated switching between English and metric units

> should not

> corrupt the original numbers with round off error!

Here is a start....

interface Convertable {

	void setMetric(boolean);

	boolean getMetric();

	double getValue();

	void setValue(double);

	String getUnits();

}

abstract class MetricHandler implements Convertable {

	private boolean metric = true;

	private String eUnits;

	private String mUnits;

MetricHandler(String _eUnits, String _mUnits) {

	eUnits = _eUnits;

	_mUnits = _mUnits;

}

	boolean getMetric() {

		return metric;

	}

	void setMetric(boolean b) {

		 metric = b;

	}

	String getUnits() {

		return metric?mUnits:eUnits;

	}

}