CSE 1030 A, Summer 2013

UnitConverter

 

Your Task

Use the following code to create a class named "UnitConverter":

/**	This utility class facilitates conversion from metric units to
 *	imperial units.
 */
public class UnitConverter
{
	/** Represents the number of centimetres in an inch. */
	public static final double CM_PER_INCH = 2.54;
	
	// Include additional constants here as needed.
	// Use the one above as a guide.
	;
	
	
	// This line prevents clients from instantiating the class.
	private UnitConverter() {}
	
	
	/**	Converts the passed value in centimetres to the corresponding
	 *	length in inches.
	 *	
	 *	@param cm A length in centimetres.
	 *	@return The passed length in inches.
	 */
	public static double cmToInches(double cm)
	{
		double result = cm / CM_PER_INCH;
		return result;
	}
	
	
	/**	Write a description here.
	 *	Use the method above as a guide.	
	 */
	public static double kgToPounds(double kg)
	{
		// Your code goes here.
		;
		return 0;
	}
	
	
	/**	Write a description here.
	 *	Use the method above as a guide.	
	 */
	public static double cToFahrenheit(double c)
	{
		// Your code goes here.
		;
		return 0;
	}
}
The method to convert from centimetres to inches is already completed. Using this as an example, complete the methods to convert kilograms and degrees Celsius. To convert from kilograms to pounds, multiply by 2.2. To convert from Celsius to Fahrenheit, multiply by 9/5 (remember to avoid integer division), then add 32. Create constants as needed.

Use the following code to create a class named "UnitConverterUI":

import java.io.PrintStream;
import java.util.Scanner;

/**	This class serves as the user interface for the UnitConverter
 *	class.
 */
public class UnitConverterUI
{
	public static void main(String[] args)
	{
		PrintStream out = System.out;
		Scanner in = new Scanner(System.in);
		
		out.println("Conversion codes:");
		out.println(" 1: cm -> inches");
		out.println(" 2: kg -> pounds");
		out.println(" 3: \u00B0C -> \u00B0F");
		out.print("Enter the value to convert: ");
		double value = in.nextDouble();
		out.print("Enter the conversion code:  ");
		int code = in.nextInt();
		
		double result;
		// Your code to convert the values goes here.
		;
		
		out.printf("The converted value is %.3f.\n", result);
	}
}
Complete the code so that the user's input is converted using the static methods in the UnitConverter utility class.

In the UnitConverter class, write documentation for each constant and method. Use the existing comments as examples. Comments that start with /** are Javadoc comments and will appear in the class's API. To create the API documentation for UnitConverter, use the terminal and navigate to the directory containing UnitConverter.java. Enter the command javadoc UnitConverter.java. In an internet browser, navigate to the same directory as your code and open the generated UnitConverter.html file.

 

Sample Runs

Run:

	Conversion codes:
	 1: cm -> inches
	 2: kg -> pounds
	 3: °C -> °F
	Enter the value to convert: 10
	Enter the conversion code:  2
	The converted value is 22.000.

 

 

 

--- End of Exercise ---