Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design an OOP using C# (C_Sharp) console application that will read two numbers and an integer code. The value of the integer code should be

Design an OOP using C# (C_Sharp) console application that will read two numbers and an integer code. The value of the integer code should be 1,2,3 or 4. If the value of the code is one, use an ADDITION method to compute the sum of the two numbers. If the code is two, use a SUBTRACT method to compute the difference (first minus second). If the code is three, use a MULTIPLY method to compute the product of the two numbers. If the code is four and the second number is not zero, use a DIVISION method to compute the quotient (first divide by second). If the code is not equal to 1.2.3 or 4 display an error message. The program should then display the computed result on the screen. Use a MENU method to assist with the selection of the integer code. Be sure to include constructor and destructor methods. Also, Program should be able to be run in Visual Studio.

Sol120:

Here is an example implementation of the program in C# console application:

using System;

class Calculator

{

private double num1, num2;

public Calculator(double num1, double num2)

{

this.num1 = num1;

this.num2 = num2;

}

public double Addition()

{

return num1 + num2;

}

public double Subtract()

{

return num1 - num2;

}

public double Multiply()

{

return num1 * num2;

}

public double Divide()

{

if (num2 == 0)

{

Console.WriteLine(\"Error: Cannot divide by zero\");

return 0;

}

else

{

return num1 / num2;

}

}

}

class Program

{

static void Main(string[] args)

{

Console.WriteLine(\"Enter two numbers:\");

double num1 = Convert.ToDouble(Console.ReadLine());

double num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine(\"Select an operation:\");

Console.WriteLine(\"1. Addition\");

Console.WriteLine(\"2. Subtraction\");

Console.WriteLine(\"3. Multiplication\");

Console.WriteLine(\"4. Division\");

int choice = Convert.ToInt32(Console.ReadLine());

Calculator calculator = new Calculator(num1, num2);

switch (choice)

{

case 1:

Console.WriteLine(\"Result: \" + calculator.Addition());

break;

case 2:

Console.WriteLine(\"Result: \" + calculator.Subtract());

break;

case 3:

Console.WriteLine(\"Result: \" + calculator.Multiply());

break;

case 4:

Console.WriteLine(\"Result: \" + calculator.Divide());

break;

default:

Console.WriteLine(\"Error: Invalid operation code\");

break;

}

}

}

To run this program in Visual Studio, create a new console application project and copy and paste the code into the Program.cs file. Then build and run the project by pressing the \"Start\" button or pressing F5 on your keyboard.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

In what way are JSP documents the opposite of servlets?

Answered: 1 week ago