In: Computer Science
LAUNGEG: C#
Objective:
For this assignment you will be creating two classes, an interface, and a driver program:
Class Calculator will implement the interface CalcOps
o As such it will implement hexToDec() - a method to convert
from
Hexadecimal to Decimal.
Class HexCalc will inherit from Calculator.
Interface CalcOps will have abstract methods for add( ), subtract( ),
multiply( ) and divide( ).
Both classes will implement the CalcOps interface.
o Class Calculator’s add, subtract, multiply and divide will take in 2 integers and return an integer. If you prefer it can take in 2 strings, and return a string.
o Class HexCalc’s add, subtract, multiply and divide will take in 2 strings (hexadecimal numbers) and return a string (hexadecimal number).
This Calculator class will work with whole numbers (int) and hexadecimal (String) values. The hexToDec ( ) method is a helper method included in interface CalcOps, that requires the Calculator class to implement the method which converts hexadecimal numbers to decimal. (Hint: Use language specific utility methods to convert hexadecimal to decimal.)
Submission Guidelines:
Program code – 1 interface program, 2 Classes and a driver program UML diagram - PDF format.
Assignment Requirements:
Create a parent class calculator that implements the arithmetic operations. You may use input values of integers or Strings.
Implement method public int hexToDec (String hexValue) which converts hexadecimal numbers to decimal numbers in the parent calculator class.
Create a class hexCalc which is a child class of the calculator class
o This class performs basic arithmetic operations such as addition,
subtraction, multiplication and division on hexadecimal numbers by
reusing the methods in parent class.
o To do hexadecimal arithmetic, convert the hexadecimal values
to
integers, perform the arithmetic operation using the parent class methods
and then convert the result back to hexadecimal string.
o For conversion between hexadecimal and integer values use the
following language specific methods.
C#
Convert.ToString(int,16) Convert integer to hexadecimal
Convert.ToInt32(hex, 16) Convert hexadecmial to integer
Run the driver program for all calculator operations using decimal and hexadecimal values.
The biggest challenge you will run into in this assignment will be calling methods with the correct data types as parameters to each method. If you are
getting errors, be sure to double check that what you passed to the method as arguments matches the methods parameter types. Where possible, use your language’s methods to convert to the appropriate type before calling.
• Be sure to convert from integers to strings and vice versa before and after converting from decimal to hex.
Skeleton:
//Design an interface public interface CalcOps
{
// ... includes basic arithmetic operations
public int hexToDec (String hexToDecimal) ;
}
//Parent class
public class Calculator implements CalcOps
{
}
//Hexadecimal arithmetic calculator that extends calculator
public class HexCalc extends Calculator{
@implement add, subtract, multiply, divide methods
//Reuse parent class methods.
}
public class TestCalculator {
//Provide choice between executing hexadecimal and decimal arithmetic //operations.
// Menu of choices
} // Driver class.
OUTPUT:
Would you like to do calculations with decimal or hexadecimal numbers (1 for Decimal, 2 for Hexadecimal
1
---MENU---
0 - Exit
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
Please Choose an Option: 1 Please enter the first number
45
Please enter the second number
67
112 ---MENU---
0 - Exit
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
Please Choose an Option: 2 Please enter the first number
34
Please enter the second number
1
33
---MENU---
0 - Exit
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
Please Choose an Option: 3 Please enter the first number
23
Please enter the second number
12
276
---MENU---
0 - Exit
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
Please Choose an Option: 4 Please enter the first number
10
Please enter the second number
3
3
---MENU---
0 - Exit
1 - Addition
2 - Subtraction
3 - Multiplication 4 - Division
Please Choose an Option: 0
You chose to Exit> THANK YOU !!! HAVE A GREAT DAY!!
Thank you!
Following console application allows user to perform basic mathematic operations in hexadecimal and decimal.
PFB source code :
Note : I have made variable names and console statement self explainatory. Should not be difficult to understand it.
TestCalculator.cs
---------------------------------------------------------------------
using System;
public class TestCalculator
{
static public void Main(String[] args)
{
Console.WriteLine("Would you like to do calculations with "
+
"decimal or hexadecimal numbers (1 for Decimal, 2 for
Hexadecimal)?");
int choice = Convert.ToInt32(Console.ReadLine());
while (true)
{
int operation = menu();
if (operation == 0)
{
Console.WriteLine("You chose to Exit> THANK YOU !!! HAVE A GREAT
DAY!!! ");
Environment.Exit(0);
}
Calculator parent = new Calculator();
if (choice == 1)
{
Console.WriteLine("Please enter the first number");
int first = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the second number");
int second = Convert.ToInt32(Console.ReadLine());
switch (operation)
{
case 1:
Console.WriteLine(parent.add(first, second));
break;
case 2:
Console.WriteLine(parent.subtract(first, second));
break;
case 3:
Console.WriteLine(parent.multiply(first, second));
break;
case 4:
Console.WriteLine(parent.divide(first, second));
break;
}
}
if (choice == 2)
{
Console.WriteLine("Please enter the first hexadecimal
number");
String f = Console.ReadLine();
Console.WriteLine("Please enter the second hexadecimal
number");
String s = Console.ReadLine();
HexCalc child = new HexCalc();
// convert from String to int
int first = parent.hexToDec(f);
int second = parent.hexToDec(s);
switch (operation)
{
case 1:
Console.WriteLine(child.decToHex(parent.add(first, second)));
break;
case 2:
Console.WriteLine(child.decToHex(parent.subtract(first,
second)));
break;
case 3:
Console.WriteLine(child.decToHex(parent.multiply(first,
second)));
break;
case 4:
Console.WriteLine(child.decToHex(parent.divide(first,
second)));
break;
}
}
}
}
public static int menu()
{
Console.WriteLine("---MENU---");
Console.WriteLine("0 - Exit");
Console.WriteLine("1 - Addition");
Console.WriteLine("2 - Subtraction");
Console.WriteLine("3 - Multiplication");
Console.WriteLine("4 - Division");
Console.WriteLine("Please Choose an Option: ");
return Convert.ToInt32(Console.ReadLine());
}
}
-------------------------------------------------------------------------------------------
Calcops interface
----------------
public interface Calcops
{
// basic arithmetic operations
public int hexToDec(string hexToDecimal);
public int add(int a1, int a2);
public int subtract(int s1, int s2);
public int multiply(int m1, int m2);
public int divide(int d1, int d2);
}
-----------------------------------------------------------------------------------------------------
Calculator.cs
-------------------------------------------
using System;
public class Calculator : Calcops
{
public int hexToDec(string hexToDecimal)
{
int dec = Int32.Parse(hexToDecimal,
System.Globalization.NumberStyles.HexNumber);
return dec;
}
public int add(int x, int y)
{
return x + y;
}
public int subtract(int x, int y)
{
return x - y;
}
public int multiply(int x, int y)
{
return x * y;
}
public int divide(int x, int y)
{
return x / y;
}
}
-------------------------------------------------------------------------------------------------------
HexCalc.cs
----------------
using System;
public class HexCalc : Calculator , Calcops {
public string decToHex(int Dec){
string hex = Convert.ToString(Dec, 16);
return hex;
}
}
====================================================================================
OUTPUT
===================================================
Hex decimal calculation :
-------------------------
-------------------------
decimal calculation :
-------------------------
-------------------------
====================================================
Let me know, if you face any issue.