In: Computer Science
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions
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.)
SOURCE CODE TestCalculator.java --------------------------------------------------------------------- import java.util.*; public class TestCalculator { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("Would you like to do calculations with " + "decimal or hexadecimal numbers (1 for Decimal, 2 for Hexadecimal)?"); int choice = sc.nextInt(); while(true) { int operation = menu(); if (operation == 0) { System.out.println("You chose to Exit> THANK YOU !!! HAVE A GREAT DAY!!! "); System.exit(0); } Calculator parent = new Calculator(); if (choice == 1) { System.out.println("Please enter the first number"); int first = sc.nextInt(); System.out.println("Please enter the second number"); int second = sc.nextInt(); switch (operation) { case 1: System.out.println(parent.add(first, second)); break; case 2: System.out.println(parent.subtract(first, second)); break; case 3: System.out.println(parent.multiply(first, second)); break; case 4: System.out.println(parent.divide(first, second)); break; } } if (choice == 2) { System.out.println("Please enter the first hexadecimal number"); String f = sc.next(); System.out.println("Please enter the second hexadecimal number"); String s = sc.next(); HexCalc child = new HexCalc(); // convert from String to int int first = parent.hexToDec(f); int second = parent.hexToDec(s); switch (operation) { case 1: System.out.println(child.decToHex(parent.add(first, second))); break; case 2: System.out.println(child.decToHex(parent.subtract(first, second))); break; case 3: System.out.println(child.decToHex(parent.multiply(first, second))); break; case 4: System.out.println(child.decToHex(parent.divide(first, second))); break; } } } } public static int menu() { System.out.println("---MENU---"); System.out.println("0 - Exit"); System.out.println("1 - Addition"); System.out.println("2 - Subtraction"); System.out.println("3 - Multiplication"); System.out.println("4 - Division"); System.out.print("Please Choose an Option: "); return sc.nextInt(); } }
-------------------------------------------------------------------------------------------
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.java ------------------------------------------- public class Calculator implements CalcOps{ public int hexToDec(String hexToDecimal) { int dec = Integer.parseInt(hexToDecimal,16); 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.java ---------------- public class HexCalc extends Calculator implements CalcOps{ public String decToHex(int Dec) { String hex = Integer.toHexString(Dec); return hex; } }
================================================================================================
OUTPUT
===================================================
Following console application allows user to perform basic mathematic operations in hexadecimal and decimal.
Major changes were following :
1. console input output.
2. datatypes.
3. Way of inheritance.
4. File extension
Modified code is highlighted in green bold.
PFB Modified source code :
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.