Question

Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the...

Subject- ( App Development for Web) ( language C#, software -visual studio)
Exact question is-Firstly the console calculator is created which perform multiply,divide,sub and add, operation and it accept all type of data (divide by 0 case as well ).Now the main motive is to create the library project from the console calculator project .Than we have to create a unit test project which test the functionality of added library.Make test like Test 1. multiply two positive number,Test 2. Add 1 positive and 1 negative number and so on ........? I try to add a new project in the console calculator and named it calculatorlibrary but there were certain errors

correct the errors in the programs

Program.cs

using System;
}
}

class Program
{
static void Main(string[] args)
{
bool endApp = false;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
calculator calculator = new calculator();

while (!endApp)
{
// Declare variables and set to empty.
string numInput1 = "";
string numInput2 = "";
double result = 0;

// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}

// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();

double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}

// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");

string op = Console.ReadLine();

try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}

Console.WriteLine("------------------------\n");

// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;

Console.WriteLine("\n"); // Friendly linespacing.
}
return;
}
}
}

Calculatorlibrary.cs

using CalculatorLibrary;
using System;
using System.Diagnostics;

namespace calculatorprogram
{
public Calculator()
{
StreamWriter logFile = File.CreateText("calculator.log");
Trace.Listeners.Add(new TextWriterTraceListener(logFile));
Trace.AutoFlush = true;
Trace.WriteLine("Starting Calculator Log");
Trace.WriteLine(String.Format("Started {0}", System.DateTime.Now.ToString()));
}

public double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.

// Use a switch statement to do the math.
switch (op)
{
case "a":
result = num1 + num2;
Trace.WriteLine(String.Format("{0} + {1} = {2}", num1, num2, result));
break;
case "s":
result = num1 - num2;
Trace.WriteLine(String.Format("{0} - {1} = {2}", num1, num2, result));
break;
case "m":
result = num1 * num2;
Trace.WriteLine(String.Format("{0} * {1} = {2}", num1, num2, result));
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
Trace.WriteLine(String.Format("{0} / {1} = {2}", num1, num2, result));
}
break;
// Return text for an incorrect option entry.
default:
break;
}
return result;
}
}

Homework Answers

Answer #1

Create New -

Project Name - calculatorprogram

Class Name - Calculator

Project - Class Library

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


using System;
using System.Diagnostics;
using System.IO;

namespace calculatorprogram
{
public class Calculator
{
public Calculator()
{
StreamWriter logFile = File.CreateText("calculator.log");
Trace.Listeners.Add(new TextWriterTraceListener(logFile));
Trace.AutoFlush = true;
Trace.WriteLine("Starting Calculator Log");
Trace.WriteLine(String.Format("Started {0}", System.DateTime.Now.ToString()));
}

public double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.

// Use a switch statement to do the math.
switch (op)
{
case "a":
result = num1 + num2;
Trace.WriteLine(String.Format("{0} + {1} = {2}", num1, num2, result));
break;
case "s":
result = num1 - num2;
Trace.WriteLine(String.Format("{0} - {1} = {2}", num1, num2, result));
break;
case "m":
result = num1 * num2;
Trace.WriteLine(String.Format("{0} * {1} = {2}", num1, num2, result));
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
Trace.WriteLine(String.Format("{0} / {1} = {2}", num1, num2, result));
}
break;
// Return text for an incorrect option entry.
default:
break;
}
return result;
}
}
}

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

Once you build this Programm, you can find the - "calculatorprogram.dll" in the bin folder

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

Create new -

Project Name - calculatorlibrary

Class Name - Program

Project - Console Class Library

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

Add Project Reference by Clickin on the Right Click of the calculatorlibrary console application

and then Click on OK and Build the solution.

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

I have modified code and added my comments wherever code has been changed. Once you build this, you can see that build is run successfully and when you run the programm, observe the below output;

Then Created new Unit Test Case Project - UnitTestProjectCalculator

Please see the below code in the for the Class - UnitTest1

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using calculatorlibrary;
namespace UnitTestProjectCalculator
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethodAddPositiveNumbers()
        {
            int iFIrstNumber = 1;
            int iSecondNumber = 2;

            Calculator objCalculator = new Calculator();
           Assert.AreEqual( objCalculator.DoOperation(iFIrstNumber, iSecondNumber,"a"),3);
        }

        [TestMethod]
        public void TestMethodAddNegativeNumbers()
        {
            int iFIrstNumber = -1;
            int iSecondNumber = -5;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "a"), -6);
        }

        [TestMethod]
        public void TestMethodAddPositiveNegativeNumbers()
        {
            int iFIrstNumber = 1;
            int iSecondNumber = -5;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "a"), -4);
        }

        [TestMethod]
        public void TestMethodSubPositiveNumbers()
        {
            int iFIrstNumber = 3;
            int iSecondNumber = 2;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "s"), 1);
        }

        [TestMethod]
        public void TestMethodSubNegativeNumbers()
        {
            int iFIrstNumber = -3;
            int iSecondNumber = -25;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "s"), 22);
        }

        [TestMethod]
        public void TestMethodSubPositiveNegativeNumbers()
        {
            int iFIrstNumber = -3;
            int iSecondNumber = 25;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "s"), -28);
        }

        [TestMethod]
        public void TestMethodMulPositiveNumbers()
        {
            int iFIrstNumber = 3;
            int iSecondNumber = 2;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "m"), 6);
        }

        [TestMethod]
        public void TestMethodMulNegativeNumbers()
        {
            int iFIrstNumber = -3;
            int iSecondNumber = -2;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "m"), 6);
        }

        [TestMethod]
        public void TestMethodDivPositiveNumbers()
        {
            int iFIrstNumber = 3;
            int iSecondNumber = 3;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "d"), 1);
        }

        [TestMethod]
        public void TestMethodDivNegativeNumbers()
        {
            int iFIrstNumber = -3;
            int iSecondNumber = -3;

            Calculator objCalculator = new Calculator();
            Assert.AreEqual(objCalculator.DoOperation(iFIrstNumber, iSecondNumber, "d"), 1);
        }
    }
}

:

You need add the Project Reference here as well to call the DoOperation() Method from the Calculator Class (You can see that I have imported the same using - using calculatorlibrary;)

Here you can see that I have created 10 different Test Cases and one by one I have tested that they are running.

Here we have used Streamwriter Class in the Constructor and so that we have to run the Test case one by one. If you wanted to run all the Test cases at once then you need comment the code in the constructor, need to build this Project, then need to add the Reference of that newly build dll in to the - CalculatorProgramm and UnitTestCaseProjectCalculatorProject and then it will run all the 10 Test cases once at a time.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT