Wednesday, October 2, 2013

Math methods

So, here is the code we did in class. I commented it to try to make it clearer.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MathExamples
{
    class Program
    {
        /*******************
         * this program creates
         * separate methods for 
         * each of the the basic math
         * operations
         * Steve Conger 10/2/2013 
         * *******************/

        int number1, number2;

        static void Main(string[] args)
        {
            //initialize the Program class and make
            //an instance of it in memory
            Program p = new Program();
            //call the GetNumbers() method
            p.GetNumbers();
            //call the Display() method
            p.Display();
            //pause for a key stroke
            Console.ReadKey();
        }

        //Get input
        void GetNumbers()
        {
            Console.WriteLine("Please enter the first integer");
            number1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Please enter the second integer");
            number2 = int.Parse(Console.ReadLine());
        }

        int Addition()
        {
            //do addition and return the answer
            int answer=number1 + number2;
            return answer;
        }

        int Multiplication()
        {
            //do multiplication and return the answer
            int answer = number1 * number2;
            return answer;
        }

        int Subtraction()
        {
            //do subtraction and return the answer
            int answer = number1 - number2;
            return answer;
        }

        int Division()
        {
            //check to make sure the denominator
            //is not 0
            //the double equal signs == tests
            //for equality
            if (number2 == 0)
            {
                //if it is equal to 0 give a message
                //and just return zero for the answer
                Console.WriteLine("Can't Divide by zero");
                return 0;
            }
            //if the denominator is not 0 just do the 
            //division
            int answer = number1  / number2;
            return answer;
        }

        

        void Display()
        {
            //int sum = Addition();
            //display the answers. The method can be put directly
            //into the writeline because it returns a number
            //which can be displayed
            Console.WriteLine("the sum of {0}and {1} is {2}", 
                number1, number2, Addition());
            Console.WriteLine("the product of {0} and {1} is {2}", 
                number1, number2, Multiplication());
            Console.WriteLine("the difference of {0} and {1} is {2}",
                number1, number2, Subtraction());
            Console.WriteLine("the quotient of {0} and {1} is {2}",
               number1, number2, Division());
        }
    }
}

No comments:

Post a Comment