Monday, September 30, 2013

Operators, constants, methods, scope (1)

Here is our first program with the number of passengers per van declared as a constant. We us the modulus to return the remainder to make sure we don't leave anyone behind

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

namespace VanPoolExample
{
    class Program
    {
        //declare a constant
        const int MAXPASSENGERS = 11;

        static void Main(string[] args)
        {
            //Input
            Console.WriteLine("How many people do you need to Transport?");
            int passengers = int.Parse(Console.ReadLine());

            //Calculation
            int vans = passengers / MAXPASSENGERS;

            if (passengers % MAXPASSENGERS > 0)
            {
                vans += 1;
            }

            //output
            Console.WriteLine("You will need {0} vans", vans);

            Console.ReadKey();

        }
    }
}

Here is the cube program with methods

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

namespace methodExamples
{
    class Program
    {
        int number; //class variable class scope
        static void Main(string[] args)
        {
            Program p = new Program();
            p.GetNumber();
            
           Console.ReadKey();
        }//end main

        void GetNumber()
        {
            //input
            Console.WriteLine("enter a integer");
             number = int.Parse(Console.ReadLine());
             CalculateCube();
        }

        void CalculateCube()
        { 
           // calculation
            int cube = number * number * number;

         

            DisplayCube(cube);
        } //end calculate cube

        void DisplayCube(int cubedNumber)
        {
            //output
            Console.WriteLine("The cube of {0} is {1}", number, cubedNumber);

          
        }

    }//end class
}//end namespace

No comments:

Post a Comment