Question

In: Computer Science

Please convert This java Code to C# (.cs) Please make sure the code can run and...

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

===================================================

Solutions

Expert Solution

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.


Related Solutions

please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT