For this task you are asked to create a number of conversion classes, all of whi
ID: 3888373 • Letter: F
Question
For this task you are asked to create a number of conversion classes, all of which inherit from the provided abstract class Converter. This allows our menu-based conversion program to be effortlessly extended to support different conversions with minimal extra code.
The public classes you have to write for this exercise are:
MetresToCentimetres (centimetres = 100 * metres)
CelsiusToFahrenheit (fahrenheit = celsius * 9 / 5 + 32)
MilesToKilometres (kilometres = miles * 1.609344)
DegreesToRadians (radians = degrees * Math.PI / 180)
Each of these classes must inherit from the provided Converter class and override its three methods. See the Week 8 lecture for more information on inheritance and overriding.
The three methods you will need to override are:
string From()
This method returns a string indicating the unit this converter converts from. For example, the MetresToCentimetres class will return "Metres".
string To()
This method returns a string indicating the unit this converter converts to. For example, the MetresToCentimetres class will return "Centimetres".
float Convert(float value)
This method converts 'value' to whatever this converter converts things to and returns the result. For example:
will print "1 metre in centimetres is 100".
A Main() method is provided to show an example of a menu-based converter. Note that part of the code is commented out:
When you write a new conversion class, you can them uncomment the line for that class and it will be added to the menu. When all 4 are uncommented, the program should look like this:
Conversion Menu
-------------------
1. Metres to Centimetres
2. Celsius to Fahrenheit
3. Miles to Kilometres
4. Degrees to Radians
Choose a converter (or type 0 to exit): 1
Converting from Metres to Centimetres
Enter input (Metres): 1
Result (Centimetres): 100
Conversion Menu
-------------------
1. Metres to Centimetres
2. Celsius to Fahrenheit
3. Miles to Kilometres
4. Degrees to Radians
Choose a converter (or type 0 to exit): 2
Converting from Celsius to Fahrenheit
Enter input (Celsius): 100
Result (Fahrenheit): 212
Conversion Menu
-------------------
1. Metres to Centimetres
2. Celsius to Fahrenheit
3. Miles to Kilometres
4. Degrees to Radians
Choose a converter (or type 0 to exit): 3
Converting from Miles to Kilometres
Enter input (Miles): 3000
Result (Kilometres): 4828.032
Conversion Menu
-------------------
1. Metres to Centimetres
2. Celsius to Fahrenheit
3. Miles to Kilometres
4. Degrees to Radians
Choose a converter (or type 0 to exit): 4
Converting from Degrees to Radians
Enter input (Degrees): 360
Result (Radians): 6.283185
Conversion Menu
-------------------
1. Metres to Centimetres
2. Celsius to Fahrenheit
3. Miles to Kilometres
4. Degrees to Radians
Choose a converter (or type 0 to exit):
Explanation / Answer
Java menu driven code for conversion:
Source code:
import java.util.*;
public class Conversion {
public static void main (String[] args) {
int ch;
boolean exitFlag = false;
while (!exitFlag) {
ch = openMenu();
if (ch == 1) {
convertMetersToCentimeters();
}
else if (ch == 2) {
convertCelsiusToFahrenheit();
}
else if (ch == 3) {
convertMilesToKM();
}
else if(ch == 4){
convertDegreesTORadians();
}
else {
exitFlag = true;
}
}
}
static int openMenu() {
int ch;
System.out.println();
System.out.println("Menu:");
System.out.println("-------------------------");
System.out.println("1) Meters -> Centimeters");
System.out.println("2) Celsius -> Fahrenheit");
System.out.println("3) Miles -> kilometers");
System.out.println("4) Degrees -> Radians");
System.out.println("5) Quit");
System.out.println("-------------------------");
System.out.print("Enter choice: ");
Scanner key = new Scanner (System.in);
ch = key.nextInt();
while (ch < 1 || ch > 5) {
System.out.print("Not a valid choice: ");
ch = key.nextInt();
}
System.out.println();
return ch;
}
static void convertMetersToCentimeters() {
System.out.print("Enter a number of meters: ");
Scanner key = new Scanner (System.in);
float met = key.nextFloat();
float centimet = met * 100;
System.out.println(met + " meters is " + centimet + " centimeters.");
}
static void convertCelsiusToFahrenheit() {
System.out.print("Enter a Celsius: ");
Scanner key = new Scanner (System.in);
float celsius = key.nextFloat();
float fahrenheit = (celsius*9/5) + 32;
System.out.println(celsius + " celsius value is" + fahrenheit + " fahrenheit.");
}
static void convertMilesToKM() {
System.out.print("Enter a number of miles: ");
Scanner key = new Scanner (System.in);
double miles = key.nextDouble();
double km = miles / 1.609344;
System.out.println(miles + " miles is " + km + " kilometers.");
}
static void convertDegreesTORadians() {
System.out.print("Enter Degrees: ");
Scanner key = new Scanner (System.in);
double degrees = key.nextDouble();
double radians = degrees * (3.14/180);
System.out.println(degrees + " degrees is " + radians + " radians.");
}
}
Output:
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 1
Enter a number of meters: 2
2.0 meters is 200.0 centimeters.
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 1
Enter a number of meters: 1
1.0 meters is 100.0 centimeters.
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 2
Enter a Celsius: 23
23.0 celsius value is73.4 fahrenheit.
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 3
Enter a number of miles: 3
3.0 miles is 1.8641135767120018 kilometers.
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 4
Enter Degrees: 30
30.0 degrees is 0.5233333333333334 radians.
Menu:
-------------------------
1) Meters -> Centimeters
2) Celsius -> Fahrenheit
3) Miles -> kilometers
4) Degrees -> Radians
5) Quit
-------------------------
Enter choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.