Wednesday, October 27, 2010

Classes and radiobuttons

Here is the form:



Here is the radiobutton code:

private void button1_Click(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked==true)
{
MessageBox.Show("the tip is 10%");
}
if (radioButton2.IsChecked==true)
{
MessageBox.Show("the tip is 15%");
}
if (radioButton3.IsChecked==true)
{
MessageBox.Show("the tip is 20%");
}
}

Tuesday, October 26, 2010

Object Oriented Programming

Object oriented programming arose out of the desire to create a more natural method for dealing with large coding projects. Rather than try to manage huge numbers of individual methods, code was broken up into objects. These objects reflected the acutal structure of the things the programmer was working with. For instance, a point of sale application might have objects for customer, item, sale, etc. Objects can also be used for systems and networks: things like connection objects. In the programming environment, forms are objects, as are buttons and text boxes, etc.

A class is the abstract description of an object. A class describing a customer, for instance, describes an ideal customer, not any particular customer.
Classes can contain fields (class level variables), properties, methods, constructors, and events.

There are four basic principles of Object oriented programming:

Abstraction
Objects should be abstractions of things. They should represent typical or generic descriptions of things.

Polymorphism
Polymorphism refers to the the ability of objects to behave differently in differnt contexts. For example the + sign between two numbers adds the numbers, between two strings it concatinates the strings. It behaves differently depending on context.

Polymorphism is primarily achieved through two techniques: Overloading and overwriting methods.

Inheritance
Inheritance allows you to derive a new object from an existing one and automatically get all the public variables, methods and properties of the parent. It promotes code reuse.

Encapsulation
Encapsulation is the principle that every object should be as self contained as possible. It should contain all that it needs to functions and be as independent as possible from other objects. The idea is that an object should be like a component or a lego, that you can plug in whereever you need and have it work.

Here is a picture of the form running:


Here is the Xaml for the form

<Window x:Class="MileageWithClass.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Blue">
<Grid>
<Label Content="Enter total Miles" Height="28" HorizontalAlignment="Left" Margin="47,40,0,0" Name="label1" VerticalAlignment="Top" Foreground="White" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="234,40,0,0" Name="txtMiles" VerticalAlignment="Top" Width="120" FontSize="16" />
<Label Content="Enter the total gallons" Height="28" HorizontalAlignment="Left" Margin="25,87,0,0" Name="label2" VerticalAlignment="Top" Foreground="White" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="234,93,0,0" Name="txtGallons" VerticalAlignment="Top" Width="120" FontSize="16" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="64,172,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>

Here is the Mileage Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MileageWithClass
{
public class Mileage
{

//empty constructor
public Mileage()
{
NumberOfMiles = 0;
NumberOfGallons = 0;
PricePerGallon = 0;
}

//overloaded constructor
public Mileage(double miles, double gallons)
{
NumberOfMiles = miles;
NumberOfGallons =gallons;
PricePerGallon = 0;
}
//private fields
private double numberOfMiles;

//public properties that expose private fields
public double NumberOfMiles
{
get
{
return numberOfMiles;
}
set
{
numberOfMiles = value;
}
}

private double numberOfGallons;

public double NumberOfGallons
{
get { return numberOfGallons; }
set { numberOfGallons = value; }
}

private double pricePerGallon;

public double PricePerGallon
{
get { return pricePerGallon; }
set { pricePerGallon = value; }
}

//public method
public double CalculateMileage()
{
return NumberOfMiles / NumberOfGallons;
}

//overloaded method
public double CalculateMileage(double miles, double gallons)
{
NumberOfMiles = miles;
NumberOfGallons = gallons;
return NumberOfMiles / NumberOfGallons;
}


}
}

Here is the code for the button in the form

private void button1_Click(object sender, RoutedEventArgs e)
{
//Mileage mileage = new Mileage();
//mileage.NumberOfMiles = double.Parse(txtMiles.Text);
//mileage.NumberOfGallons = double.Parse(txtGallons.Text);


Mileage m = new Mileage(double.Parse(txtMiles.Text),
double.Parse(txtGallons.Text));

double milesPerGallon = m.CalculateMileage();

MessageBox.Show("the miles per gallon is "
+ milesPerGallon.ToString());
}

Monday, October 25, 2010

Entity Relation Diagram

Here is the diagram we did for the DVD

Thursday, October 21, 2010

Error Trapping

Here is the code that shows error trapping (AM Class)

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

namespace errortrapping
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.BadDivision();
Console.ReadKey();
}

void BadDivision()
{
double number;
double number2;
bool b1, b2;

try
{
Console.WriteLine("Enter a number");
b1 = double.TryParse(Console.ReadLine(), out number);

if (b1 == false)
{
Console.WriteLine("You must enter a real number");
return;
}

Console.WriteLine("Enter a second number");
b2 = double.TryParse(Console.ReadLine(), out number2);

if (b2 == false)
{
Console.WriteLine("You must enter a real number");
return;
}

if (number2 == 0)
{
throw new DivideByZeroException();
}

double quotient = number / number2;

Console.WriteLine("the quotient is {0}", quotient);
}//end try
catch (DivideByZeroException divError)
{
Console.WriteLine(divError.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
}
}

Tuesday, October 19, 2010

Random and Loops

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

namespace RandomLoops
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.GetRandom();
Console.ReadKey();
}

void GetRandom()
{
Random rand = new Random();

for (int counter = 1; counter <= 10; counter++)
{
int number = rand.Next(1, 1000);
Console.WriteLine(number.ToString());
}

int number2 = 1;
int counter2 = 0;
while (counter2 <= 10)
{
Console.WriteLine("Enter a number, 0 to quit");
number2 = int.Parse(Console.ReadLine());
counter2++;
}
}
}
}

Thursday, October 14, 2010

What do we know so far?

What do we know about Namespaces?

  • 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"));

}

}

}

Wednesday, October 13, 2010

If then examples (morning)

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

namespace ifmethod
{
class Program
{
/*Steve Conger
* 10/12/2010
* Assignment x
* */

static void Main(string[] args)
{
Program p = new Program();
// p.Display();
p.GetDegrees();
Console.ReadKey();
}//end main

int Remainder(int num1, int num2)
{
int modulus;
modulus= num1 % num2;
return modulus;
}//end remainder

void Display()
{

int numberOne, numberTwo;
bool good, isInt;

Console.WriteLine("enter a number");
good = int.TryParse(Console.ReadLine(), out numberOne);

//if (good == false)
//{
// Console.WriteLine("You must Enter an integer");
// return; //end method display
//}
//else
// {
//do something else
//}


Console.WriteLine("enter another number");
isInt = int.TryParse(Console.ReadLine(), out numberTwo);

//this is a way to do it in one statement
//the || means or
if (good == false || isInt == false)
{
Console.WriteLine("You must Enter an integer");
return; //end method display
}

int remain = Remainder(numberOne, numberTwo);

Console.WriteLine("the remainder of {0} divided by {1} = {2}", numberOne.ToString(),
numberTwo.ToString(), remain.ToString());

// SumOfRemainder(remain);

}

void Weather(int degrees)
{
if (degrees >= 100)
{
Console.WriteLine("way too hot");
}
else
if (degrees >= 80)
{
Console.WriteLine("hot");
}
else
if (degrees >= 60)
{
Console.WriteLine("Just right");
}
else
if (degrees >= 40)
{
Console.WriteLine("autumnal");
}
else
{
Console.WriteLine("cold");
}


}

void GetDegrees()
{
Console.WriteLine("Enter a temperature");
int temp = int.Parse(Console.ReadLine());

Weather(temp);
}

//void SumOfRemainder(int r)
//{
// Console.WriteLine(r.ToString());
//}

}//end program class
}//end namespace

Monday, October 11, 2010

IF ELSE IF ELSE

Here is starter code for the assignment;

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment3Help
{
class Program
{
double grossPay;

static void Main(string[] args)
{
Program p = new Program();
p.Display();
Console.ReadKey();
}

double CalculateGrossPay(double rate, double hours)
{
return rate * hours;
}

void Display()
{
Console.WriteLine("Enter the Rate");
double rate = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the hours");
double hours = double.Parse(Console.ReadLine());
grossPay = CalculateGrossPay(rate, hours);
Console.WriteLine("The Gross pay is {0}", grossPay.ToString("c"));
}//end display

double SocialSecurity()
{
//at or above 5000 30%
//at or above 3000 25%
//at or above 2000 20%
//at or above 1000 15%
//at or above 800 10%
//below 800 0%
double socialSecurity;
if (grossPay >= 5000)
{
socialSecurity = grossPay * .3;
}
else if ( grossPay >=3000)
{
socialSecurity = grossPay * .25;
}
else if (grossPay >= 2000)
{
socialSecurity = grossPay * .2;
}
else if (grossPay >= 1000)
{
socialSecurity = grossPay * .1;
}
else
{
socialSecurity = 0;
}
return socialSecurity;

}

}//class
}

The other code we did
using System;
using System.Collections.Generic;
using System.Text;

namespace OperationsPractice
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
//p.Display();
Console.WriteLine("Enter a temperature");
int temp = int.Parse(Console.ReadLine());
p.IfElseExample(temp);
Console.ReadKey();
}//end main

private int Addition(int num1, int num2)
{
return num1 + num2;
}//end add

private void Display()
{
int number1=0;
int number2;
bool isInt1;
bool isInt2;

Console.WriteLine("Enter the first number");
isInt1 = int.TryParse(Console.ReadLine(), out number1);
if (isInt1 == false)
{
Console.WriteLine("You must enter an integer");
//Console.ReadKey();
return;
}//end if

Console.WriteLine("Enter the Second number");
isInt2 = int.TryParse(Console.ReadLine(), out number2);

if (isInt2 == false)
{
Console.WriteLine("You must enter an integer");
//Console.ReadKey();
return;
}//end if
int sum=Addition(number1, number2);
Console.WriteLine("the sum is {0}", sum );
}//end display

private void IfElseExample(int number)
{
if (number >= 100)
{
Console.WriteLine("It is too hot");
}
else if (number >= 80)
{
Console.WriteLine("Hot");
}
else if (number >= 60)
{
Console.WriteLine("Just right");
}
else if (number >= 40)
{
Console.WriteLine("cool");
}
else
{
Console.WriteLine("Cold");
}

//if(number != 3)
//{
// if (number > 5)
//{
//return;
//}
//else
//{
//}
// }
//else
//{
//do something else
//}
}
}//end class
}//end namespace

Thursday, October 7, 2010

Code for Assignment 2

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

namespace Assignment2
{
class Program
{

double grossPay;

static void Main(string[] args)
{
Program prog = new Program();
prog.GetNumbers();
Console.ReadKey();
}

void GetNumbers()
{
Console.WriteLine("Enter the Rate");
double rate = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the hours");
double hours = double.Parse(Console.ReadLine());
CalculateGross(rate, hours);
CalculateTotalDeductions();
}

void CalculateGross(double rt, double hrs)
{
grossPay = rt * hrs;
Console.WriteLine("The Product is {0}", grossPay.ToString("c"));
}

double CalculateSocialSecurity()
{
return grossPay * .2;
}

double CalculateMedicare()
{
return grossPay * .05;
}

void CalculateTotalDeductions()
{
double ss = CalculateSocialSecurity();
double md = CalculateMedicare();
double totalDeductions = ss + md;
Console.WriteLine("the total deductions are {0}", totalDeductions.ToString("c"));
}

}//class
}

Tuesday, October 5, 2010

MethodExamples Morning

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

namespace MethodExamples
{
class Program
{

static void Main(string[] args)
{
Program prog = new Program();
//prog.BasicOperators();
prog.UseGetRemainder();
}//end main

private void BasicOperators()
{
double number;
int num1=7, num2=34, num3=12;
string myName = "Steve Conger";

//The basic operators are * / + - %
// other operators ++ -- += -= *= /= %= < > != ==

number = 756.77;
double sum = number + num1;
int total = (int)number + num1;

Console.WriteLine("the double sum is {0}", sum.ToString());
Console.WriteLine("The integer total is {0}", total.ToString());

Console.ReadKey();


}//end basic operators

private int GetRemainder(int numberOne, int numberTwo)
{
int remainder;
remainder = numberOne % numberTwo;
return remainder;
}

private void UseGetRemainder()
{
Console.WriteLine("Enter your first number");
int number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your second number");
int number2 = int.Parse(Console.ReadLine());

int myRemainder = GetRemainder(number1, number2);

Console.WriteLine("the remainder of {0} and {1} is {2}", number1.ToString(),
number2.ToString(), myRemainder.ToString());

Console.ReadKey();

}

}//end class program
}//end namespace

Monday, October 4, 2010

operators and methods: afternoon.

Here is the code we did in class

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

namespace methodExamples
{
class Program
{
double number = 0;
static void Main(string[] args)
{
//* / + - %
Program p = new Program();
// p.BasicOperators();
p.UseCube();
Console.ReadKey();

}

private void BasicOperators()
{
int x=6, y=8;
double a, b;
a = 12.34;
b = 3.75;


double sum = a + b;
double difference = a - b;
double product = a * b;

int quotient = y / x;
int remainder = y % x;

Console.WriteLine("The sum is {0}", sum.ToString());
Console.WriteLine("The difference {0}", difference.ToString());
Console.WriteLine("The product is {0}", product.ToString());
Console.WriteLine("The quotient of integer {0}, and {1} is {2}", y.ToString(), x.ToString(), quotient.ToString());
Console.WriteLine("The remainder is {0}", remainder.ToString());


}//end method BasicOperators

private double Cube(double number)
{
double cubeNumber=number * number * number;
return cubeNumber;
}

private void UseCube()
{

Console.WriteLine("Enter a number");
double myNumber = double.Parse(Console.ReadLine());
double myCube = Cube(myNumber);
Console.WriteLine("The cube of {0} is {1}", myNumber.ToString(), myCube.ToString());
}
}//end class
}//end namespace

Class Activities

Today in class we broke into scenario groups. Each group should send me their names and what topic they are working on. Additionally groups should begin working on the thing to do associated with their scenario, specifically deciding what the big topics are and composing a statement of work (or at least a statment of scope).

There was also time to begin the individual practices. We will go over the practices on Wednesday and start chapter 2.

Things are due as you finish them. I am not very strict about due dates. But we will continue moving on and it can be easy to get behind. Ideally things should be turned in about a week after we cover them.