Tuesday, November 1, 2016

Methods

The first examples

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

namespace MethodsExample
{
    class Program
    {
        //int number; this would make available everwhere 
        //in the class
        static void Main(string[] args)
        {
            Program p = new Program();
            p.GetNumber();
            p.Pause();
        }

        private void GetNumber()
        {
            Console.WriteLine("Enter a number");
            int number = int.Parse(Console.ReadLine());
            Display(number);
        }

        private void Display(int numb)
        {
            int result = Cube(numb);
            Console.WriteLine("The cube of {0} is {1}", numb, result);
        }
           

        private int Cube(int num)
        {
            int cube = num * num * num;
            return cube;
        }

        private void Pause()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

the tip program


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

namespace TipMethodExample
{
    class Program
    {
        //Determine the amount of the meal
        //Determine the percent of tip
        //Calculate tax amount
        //Calcualte tip amount
        //Display results (amount, tax, tip, total);
        //assign constant values
        private const double TAXPERCENT = .092;
        private const double PERCENTDIVISOR = 100;

        static void Main(string[] args)
        {
            //instantiate the class Program
            Program p = new Program();

            //call the calculate method
            //which calls GetMealAmount and GetTipPercent
            p.Calculate();
            //call the pause method
            p.Pause();
        }

        private double GetMealAmount()
        {
            //this gets the meal amount and returns it to 
            //the calling method
            Console.WriteLine("Enter the Meal Amount");
            double amount = double.Parse(Console.ReadLine());
            return amount;//the return must return the type 
            //given in the method signature (double)
            //and must be the last statement in the method
        }

        private double GetTipPercent()
        {
            //this gets the tip percent and returns
            //it to the calling method
            Console.WriteLine("Enter the Tip Percent as a Whole number");
            double tipPercent = double.Parse(Console.ReadLine());
            return tipPercent;
        }

        private void Calculate()
        {
            //call get meal amount and assign returned value
            //to mealAmount
            //same for tipPerce
            double mealAmount = GetMealAmount();
            double tipPerc = GetTipPercent();
            double taxAmount = mealAmount * TAXPERCENT;
            double tipAmount = mealAmount * (tipPerc/ PERCENTDIVISOR);
            double total = mealAmount + taxAmount + tipAmount;
            //pass calculate values to Display method
            Display(mealAmount, taxAmount, tipAmount, total);

        }

        private void Display(double meal, double tax, double tip, double total)
        {
            Console.WriteLine("Meal:\t\t{0:C}", meal);
            Console.WriteLine("Tax:\t\t{0:C}", tax);
            Console.WriteLine("Tip:\t\t{0:C}", tip);
            Console.WriteLine("Total:\t\t{0:C}", total);
        }

        private void Pause()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

here is the peer excercise--though without comments for now

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

namespace PeerExcerciseMethods1
{
    class Program
    {
        //get a number
        //test the number
        //do the calculation
        //display the results
        static void Main(string[] args)
        {
            Program p = new Program();
            int number=p.TestNumber();
            p.Display(number);
            p.Pause();
        }

        private int GetNumber()
        {
            Console.WriteLine("Enter an integer between 1 and 40");
            int number=0;
            bool good = false;
            //this checks to make sure it is a valid
            while (!good)
            {
                good = int.TryParse(Console.ReadLine(), out number);
                if (!good)
                    Console.WriteLine("Enter a valid integer");
            }

            return number;
        }

        private int TestNumber()
        {
            int num = GetNumber();
            //the while instead of an if
            //loops until they get it right
            while (num < 0 || num > 40)
            {
                num = GetNumber();
            }
            return num;
        }

        private int GetPrime(int number)
        {
            return number * number - number + 41;
        }

        private void Display(int number)
        {
            Console.WriteLine("Your prime number is {0}", GetPrime(number));
        }

        private void Pause()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

        }


    }
}

No comments:

Post a Comment