import java.io.File; import java.io.IOException; import java.io.PrintStream; /** * This is the model for the Unit Converter application. */ public class UnitConvModel { /** Represents the number of kilograms in a pound. */ public final double KG_PER_LBS = 2.2; /** Represents the number of centimetres in an inch. */ public final double CM_PER_INCH = 2.54; private double input; private double result; private String conversion; /** * Creates a model with no user values and a calculated value * of zero. */ public UnitConvModel() { this.clear(); } /** * Re-initialises this model with a calculated value of zero. */ public void clear() { input = 0; result = 0; conversion = ""; } /** * Returns the input value of this model. * @return the input value as a double */ public double getInputValue() { return input; } /** * Returns the converted value calculated by this model. * @return the converted value as a double */ public double getResultValue() { return result; } /** * Saves the current conversion to a file in plain text. * @param file the file to create for the output */ public void save(File file) { if (file != null) { try { PrintStream out = new PrintStream(file); out.println(conversion); out.close(); } catch(IOException ex) {} } } /** Converts the passed value in centimetres to the corresponding * length in inches. * @param cm a length in centimetres */ public void cmToInches(double cm) { input = cm; result = cm / CM_PER_INCH; conversion = input + " cm to " + result + " inches"; } /** Converts the passed value in inches to the corresponding * length in centimetres. * @param in a length in inches */ public void inchesToCm(double in) { // TODO (3 lines of code): Perform conversion. // Use cmToInches as a guide. ; } /** Converts the passed value in pounds to the corresponding * weight in kilograms. * @param lbs a weight in pounds */ public void lbsToKg(double lbs) { input = lbs; result = lbs / KG_PER_LBS; conversion = input + " lbs to " + result + " kg"; } /** Converts the passed value in kilograms to the corresponding * weight in pounds. * @param kgs a weight in kilograms */ public void kgToLbs(double kgs) { // TODO (3 lines of code): Perform conversion. // Use lbsToKg as a guide. ; } /** Converts the passed value in Celcius to the corresponding * temperature in Fahrenheit * @param c a temperature in Celcius */ public void cToF(double c) { input = c; result = c * (9.0/5) + 32; conversion = input + "\u00B0C to " + result + "\u00B0F"; } /** Converts the passed value in Fahrenheit to the corresponding * temperature in Celcius * @param f a temperature in Fahrenheit */ public void fToC(double f) { // TODO (3 lines of code): Perform conversion. // Use cToF as a guide. ; } }