Question

In: Computer Science

LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a...

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!

Solutions

Expert Solution

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.


Related Solutions

C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
OBJECTIVE: In the second assignment, you started coding the NineGaps game by creating the skeleton of...
OBJECTIVE: In the second assignment, you started coding the NineGaps game by creating the skeleton of the program including the loops, inputs/outputs, and some conditions without going to the details of the game. In this third assignment, we are going to complete the game by coding the details, using what we learned so far, especially working with arrays. YAY! At the end of this assignment, we can actually play with the very first game that we created by ourselves (at...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
Using C++. For this assignment you will design a set of classes that work together to...
Using C++. For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. The classes you should design are : The ParkedCar class: This class should simulate a parked car. The class's responsibilities are: -to know the car's make, model,color ,license number,and the number of minutes that the car has been parked The ParkingMeter Class: This class should simulate a parking meter. the class's only responsibility is: -To know...
Assignment Examine the Main and Address classes. You are going to add two classes derived from...
Assignment Examine the Main and Address classes. You are going to add two classes derived from Address: BusinessAddress and PersonAddress. Create BusinessAddress class Select package home and create a new Java class called BusinessAddress Make the class extend the Address class Add two private String fields businessName and address2 Generate constructor and all getters and setters Add a printLabel() method The printLabel method should print (using System.out.println()) First line – the businessName field Second line – the address2 field if...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design and implement what is known as the “Game of Life”, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.   The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cell’s life cycle...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding...
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding of the lexical analysis phase of a compiler and the process of constructing a symbol table. Problem: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a program (in C, C++, C#, Java, or Python) that implements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT