Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a public class UnitConverter with static methods milesToKilometers, kilometersToMiles, poundsToKilograms, and kilogramsToPounds. Each method has a double parameter and returns a double value. Each

Create a public class UnitConverter with static methods milesToKilometers, kilometersToMiles, poundsToKilograms, and kilogramsToPounds.

Each method has a double parameter and returns a double value.

Each method performs a conversion using the parameter and a conversion formula, and returns the converted value. Your conversion factor MUST have 3-4 places past the decimal point. You can find the formulas here:

http://images.slideplayer.com/18/5674766/slides/slide_46.jpg (Links to an external site.)Links to an external site.

Be sure to test your methods with correct and incorrect values (including zero and negative numbers). No conversion should be done if the parameter is negative. Each method must return zero if the parameter is invalid.

NO INPUT OR OUTPUT SHOULD BE DONE IN THESE METHODS.

Create a progam UnitConverterTest that displays a menu allowing the user to perform different conversions, or quit the program. Do the following in the UnitConverterTest program:

Use a do-while loop for the program. If the user enters an incorrect menu choice, just display the menu again. The program must keep running until the user chooses quit.

Create a static method getInput to prompt for and read in the input. Be sure to validate the input before returning it. There should be NO negative values allowed. Use a do-while loop for validation. The method should look like this:

public static double getInput(String type) To get miles, for example, you would call the method with the argument miles. Use the type in the prompt. The method will return the validated user input.

Use a switch statement for the menu choice. In each case, call the getInput method to get the value, then call the converter method and display the value returned by the method with ONE place past the decimal point.

You must call the static UnitConverter methods using the class name. Do NOT create an instance (object) of the UnitConverter class.

Use the command prompt window (terminal) for ALL input and output.

Your full name must appear in the output or 1 point will be deducted. You must follow the Programming Guidelines handout. All code must be correctly indented and commented. Your program must compile in order to be graded. Do NOT use Eclipse, NetBeans, or IntelliJ for any labs or assignments. Do NOT create packages or projects; otherwise I will not be able to grade your assignment.

Sample program run:

Welcome to the Unit Converter. 1. Convert miles to kilometers. 2. Convert kilometers to miles. 3. Convert pounds to kilograms. 4. Convert kilograms to pounds. 5. Quit the program. Menu choice: 1 Enter miles: 45 45.0 miles is 72.4 kilometers Welcome to the Unit Converter. 1. Convert miles to kilometers. 2. Convert kilometers to miles. 3. Convert pounds to kilograms. 4. Convert kilograms to pounds. 5. Quit the program. Menu choice: 3 Enter pounds: 4 4.0 pounds is 1.8 kilograms Welcome to the Unit Converter. 1. Convert miles to kilometers. 2. Convert kilometers to miles. 3. Convert pounds to kilograms. 4. Convert kilograms to pounds. 5. Quit the program. Menu choice: 2 Enter kilometers: -10 Enter kilometers: 10 10.0 kilometers is 6.2 miles Welcome to the Unit Converter. 1. Convert miles to kilometers. 2. Convert kilometers to miles. 3. Convert pounds to kilograms. 4. Convert kilograms to pounds. 5. Quit the program. Menu choice: 7 Welcome to the Unit Converter. 1. Convert miles to kilometers. 2. Convert kilometers to miles. 3. Convert pounds to kilograms. 4. Convert kilograms to pounds. 5. Quit the program. Menu choice: 5 Programmed by Your Name 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions