-1

Hi I need a little help creating a simple program that will Generate multiplication table (using for-loop) by using Class Library from VS C#.

I have here below the incomplete code for the for-loop but I got lost coz it's a bit different than application form and console application. If you're using class library you cannot use debug, you can run or check the codes by using Test Explorer/ Test.

(Edited Update 1) For this 2 things are needed.

  • Class Library (Main program)
  • Same solution but another class name, now it comes with NUnit that is referenced to the Main Program.

(Edited Update 2)

I'll be back to check for some info

Update 3. Here's the new code

namespace FunctionTest
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void Multiplication()

        {
            int i;
            int n = 0;
            n = Convert.ToInt32(Console.ReadLine());

            for (i = 1; i < 13; i++)
            {
                Console.WriteLine(i + "x" + n + " = " + i * n);
            }
        }
    }

Here's the idea or what it should be look like. (Below is the program)

using System;
namespace ExerciseFunction
{
    public class Equations
    {
        public int addition(int x, int y)
        {
            int z = x + y;
            return z;
        }
  
        public int subtraction(int x, int y)
        {
            int z = x - y;
            return z;
        }

        public int multiplication(int x, int y)
        {
            int z = x * y;
            return z;
        }

        public int division(int x, int y)
        {
            int z = x / y;
            return z;
        }


        static void Main(string[] args)
        {

        }
    }
}

Now this one is the NUnit to check if the input or answer is correct or not of the Program.

using NUnit.Framework;
using ExerciseFunction;

namespace ExerciseNunit
{
    [TestFixture]
    public class Operations
    {


        [Test]
        public static void addition()
        {
            Equations result = new Equations ();
            float r = result.addition(4, 5);
            Assert.AreEqual(9, r);
            Assert.AreNotEqual(13, r);
        }

        [Test]
        public static void subraction()
        {
            Equations result = new Equations();
            int t = result.subtraction(5, 3);
            Assert.AreEqual(2, t);
            Assert.AreNotEqual(5, t);
        }

        [Test]
        public static void multiplication()
        {
            Equations result = new Equations();
            int y = result.multiplication(6, 3);
            Assert.AreEqual(18, y);
            Assert.AreNotEqual(15, y);
        }

        [Test]
        public static void division()
        {
            Equations result = new Equations();
            int u = result.division(4, 2);
            Assert.AreEqual(2, u);
        }

    }
}

Thanks and looking forwards hearing your response. Your help is appreciated!

Josh
  • 29
  • 1
  • 8
  • What is your question again? ... I don't follow. What should it do? ... Class Library as main program? – Matyas Sep 14 '15 at 07:12
  • So why the tests? ... Why the class library? You want an application which will write the table, or is your homework to figure out how the tests work? :-) – Matyas Sep 14 '15 at 07:36
  • Hi. Thanks for the reply. I want to generate a multiplication table like this 5x1 = 5, 5x2 = 10, 5x3= 15, 5x4= 20 and so on.. Yes Class Library as main program in the sense I will create another class in the same solution and use the references of the main program. Like linking each other. I have posted a sample to make it more clear. – Josh Sep 14 '15 at 07:41

1 Answers1

0

If you want to write a program, you probably want to execute it too. So you need and executable, not a library.

So first create a new project "Console Application" or "Windows Forms Application", or maybe a "WPF application" but not a Class Library. Also writing some unit test is useful, but I don't thing that in this case.

Secondly: do not declare loop variable i before the cycle. Do it in the cycle like this

for (int i = 0; i < 15; ++i) //or i++ there is a difference but not relevant right now.

Then... You probably want to get some input from a user to get your n. In console application you can do that like this

int n;
string input = Console.ReadLine();
if (int.TryParse(input, out n))
{
    //do your math here.
}
else
{
    Console.WriteLine("That was not a number.");
}

Your for-cycle would work but the formatting of the output will be poor and most importantly, you are not printing or giving the output anywhere. So let's fix it like this (put that to place "//do your math here."):

for (int i = 1; i < 15; ++i)
{
    Console.WriteLine(string.Format("{0} x {1} = {2}", i, n, i * n));
}

In the end you might want the application not to exit immediately. If you add Console.ReadLine(); in the end. It will wait for pressing any key before it exits.

If you so much want to have the algebra part in another project (which doesn't really make sense, but OK), you can create another project (Class Library) with this class in it (or put just the class in the existing project):

public static class Algebra
{
    public static int Multiply(int a, int b)
    {
        return a * b;
    }

    //.... other methods
}

and then call it in the for loop like this:

int product = Algebra.Multiply(i, n);
Console.WriteLine(string.Format("{0} x {1} = {2}", i, n, product));

And of course you can then unit-test the Multiply method as much as you want.

Matyas
  • 1,122
  • 5
  • 23
  • 29