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;
}
}
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.
Get Answers For Free
Most questions answered within 1 hours.