Question
C# Converter Classes For this task you are asked to create a number of conversion classes, all of which inherit from the provided abstract class
C#
Converter Classes
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:
Converter converter = new MetresToCentimeters(); Console.WriteLine("1 metre in centimetres is {0}", converter.Convert(1));
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:
// Uncomment these to test out new classes as you implement them //converters.Add(new MetresToCentimetres()); //converters.Add(new CelsiusToFahrenheit()); //converters.Add(new MilesToKilometres()); //converters.Add(new DegreesToRadians());
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):
Note that the Main() method will not be run as part of testing your submission- instead, your MetresToCentimetres, CelsiusToFahrenheit, MilesToKilometres and DegreesToRadians classes will be instantiated and tested directly.
Sample code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Converters { public abstract class Converter { public abstract string From();
public abstract string To();
public abstract float Convert(float value); }
// Write your converter classes here // ... class Program { static void Main(string[] args) { bool running = true; bool inputValid; int menuOption; float fromValue;
List converters = new List();
// Uncomment these to test out new classes as you implement them //converters.Add(new MetresToCentimetres()); //converters.Add(new CelsiusToFahrenheit()); //converters.Add(new MilesToKilometres()); //converters.Add(new DegreesToRadians());
while (running) { Console.WriteLine(" Conversion Menu"); Console.WriteLine("-------------------"); Console.WriteLine(); for (int i = 0; i < converters.Count; i++) { Console.WriteLine("{0}. {1} to {2}", i + 1, converters[i].From(), converters[i].To()); }
Console.WriteLine();
do { Console.Write("Choose a converter (or type 0 to exit): "); inputValid = int.TryParse(Console.ReadLine(), out menuOption); if (!inputValid || menuOption < 0 || menuOption > converters.Count) { inputValid = false; Console.WriteLine("Invalid option."); } } while (!inputValid);
if (menuOption == 0) { running = false; } else { Converter currentConverter = converters[menuOption - 1]; Console.WriteLine("Converting from {0} to {1} ", currentConverter.From(), currentConverter.To()); do { Console.Write("Enter input ({0}): ", currentConverter.From());
inputValid = float.TryParse(Console.ReadLine(), out fromValue); if (!inputValid) { Console.WriteLine("Invalid value."); } } while (!inputValid);
float result = currentConverter.Convert(fromValue); Console.WriteLine("Result ({0}): {1}", currentConverter.To(), result); }
} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started