- Namespaces are a bloc that begin and end with curly braces
- Namespaces contain classes
- Namespaces are used to group similar things and keep things that should be separate, separate
What do we know about classes?
- A classes are blocks that begin and end with curly braces
- classes can contain methods(including special methods such as properties and events) and variable declarations
- All methods and variable declarations must be inside of a class
- A class is an abstract definition of an object
What do we know about Main()?
- Main is a special method. It is the starting point of any C# program
- As a method it is a block statement that begins and ends with curly braces
- because Main is always static it is loaded immediately into memory and executes first
- Also because it is static, we must instantiate (declare) the class it is in to load the other methods in the class into memory
- Best practice is to only put the minimal amount of code to start the program into Main
What do we know about Methods?
- Methods are blocks used to separate the work that a program does.
- As blocks they begin and end with curly braces
- A method always has a return type. Void is a return type that means the
method does not return a value. - A method that is not void must have a return statement
- The return statement is always the last statement in a method, any code after a return statement will never execute
- Methods can take arguments(parameters) that pass values from one method to another (but they don't have to.)
- A method must be called from somewhere if it is to do its work
- You call a method by naming it and providing any arguments it requires
- once a method has executed all its statements the program flow returns to the place where the method was called
- If the method returns a value other than void, you can assign that value to a variable to store and use in the calling method
What do we know about variables?
- Variables are used to store temporary values that the program needs to do its work
- A variable must be declared, to do that you state its type and then its name
double number; - A variable must be assigned a value before you can use it.
number=10; - Variables have "Scope", that means there are limits to where their values can be accessed and how long they are in memory
- The general rule is that a variable has the scope of the block in which it was declared. (between the curly braces of the block)
- So a variable declared at the class level can be accessed by any method in the class.
- A variable declared in a method has method scope and can only be accessed by statements in that method
- A variable declared in an if block has a value that can only be accessed within the if statement
What do we know about statements?
- Statements are actual lines of commands that do the work of the program.
- All statements end with a semi colon
- Statements can only exist in methods.
What do we know about objects?
You can access the methods and properties of an object by giving
the object name or object instance name, typing a dot . and choosing the
method from the intellisense list: Console.WriteLine(); p.Display()
A Sample Program:
Let's create a program to calculate tips for a meal. We won't worry for
now about calculating taxes or doing tips for services or taxies. We will
just focus on tips for meals.
First Let's walk through the Program Planner.
What are the input's that we will need? We will need the cost of the meal
and the percent tip we wish to offer.
Secondly we need to know what the output is. We will want to
see the cost of the meal, the amount of the tip, and the total of
the meal and the tip together.
To get from inputs to outputs we need to do two calculations.
First we get the amount of the tip by multiplying it times the percentage.
Next we add the amount of the tip to the meal amount to get the grand total.
This is the place to think about methods. Think about the tasks you
need to do. There should be a method for each task. In the case of the tips
program we need to:
- get the meal amount and tip percent >
- calculate the tip
- calculate the total
- Display the results
So we need four methods in addition to the Main(). If it helps
it might be a good idea to lay out the method signatures first
like this:
class Program()
{
static void Main()
{
}
void GetInfo()
{
}
double CalculateTip(double total, double percent)
{
}
double CalculateTotal(double mTotal, double tipAmount)
{
}
void Display(double mt, double mtip, double grandTotal)
{
}
}
}
When you set up the methods think about what parameters you have to pass
or what variables you have to declare. It doesn't have to be perfect. You can adjust
it as you go. The trick is to keep the logic forefront in you mind: First we need to get
the information, next we calculate the tip, then we calculate the total with the tip,
then we display the results. If you hold tight to the logic the syntax issues shouldn't
be so difficult to deal with and overcome.
Below is the whole code with comments:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TipCalculator
{
//this class calculates the amount of a tip
//give the total cost of the meal
//and the percent of the tip the user
//wishes to give
//Steve Conger
//Sample program
//10/14/2010
class Program
{
static void Main(string[] args)
{
Program p = new Program(); //load the Program class
p.GetInformation(); //call the starting method
Console.ReadKey(); //pause the program
}
void GetInformation()
{
//this method gathers all the information
//program needs both from the Console
//for meal and percent
//and from other methods
Console.WriteLine("Enter the total meal cost");
double mealTotal = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the percent tip");
double tipPercent = double.Parse(Console.ReadLine());
//call the Calculatetip method and send it the mealTota
//and the tip percent
//it returns a value stored in the variable tip
double tip = CalculateTip(mealTotal, tipPercent);
//call the CalculateTotal method passing it the mealTotal
//and tip amount that we got back from the previous method
double totalWithTip = CalculateTotal(mealTotal, tip);
//send it all to Display()
DisplayResults(mealTotal, tip, totalWithTip);
}
double CalculateTip(double total, double percent)
{
double tipAmount;
//here we test to see if they sent the percent as a decimal
//or a whole number
//if it is a whole number we divide it by 100
//to turn it into a decimal
if (percent > 1)
{
tipAmount=total * (percent /100);
}
else
{
//otherwise we just multiply the total by the percentage
tipAmount= total * percent;
}
return tipAmount;
}
double CalculateTotal(double mTotal, double tipAmt)
{
//in this method we just add the meal amount and the tip
return mTotal + tipAmt;
}
void DisplayResults(double mt, double mtip, double grandTotal)
{
//the console clear does what it says it clears the console
Console.Clear();
//below we just display the values we passed from GetInfo
Console.WriteLine("*******************************");
Console.WriteLine("the meal total is {0}", mt.ToString("c"));
Console.WriteLine("the tip amount will be {0}", mtip.ToString("c"));
Console.WriteLine("the total due is {0}", grandTotal.ToString("c"));
}
}
}
No comments:
Post a Comment