Here is the diagram that we did in class for the practices for chapter 5
Monday, October 29, 2012
Tuesday, October 23, 2012
Loops arrays, while
here is the code for the day. I may return to it to add comments later">
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shopping { class Program { //we need to create two arrays //one for the items //one for the prices //we will prompt the user how many items //they want to enter //Then we will enter items and prices //into the two arrays //we will sum the prices and add tax //we will display a summary information //then we will ask if they want to enter //another list. If they say yes we //will do it again, if no we will exit int number=0; static void Main(string[] args) { Program p = new Program(); p.Display(); } private void Display() { //loop the program as long as the user says yes string choice = "yes"; while (choice == "yes" || choice == "y") { //this changes the console background to blue Console.BackgroundColor = ConsoleColor.DarkBlue; Console.Clear(); //call methods GetNumberOfItems(); CreateShoppingArray(); CreatePricesArray(); FillArrays(); Console.WriteLine("do you want to contine with another list--yes or no"); choice = Console.ReadLine(); choice = choice.ToLower(); } //Console.ReadKey(); } //get the number of items private void GetNumberOfItems() { Console.WriteLine("How many items do you want to enter?"); number = int.Parse(Console.ReadLine()); } //create the two arrays private string[] CreateShoppingArray() { string[] items = new string[number]; return items; } private double[] CreatePricesArray() { double[] prices = new double[number]; return prices; } //this prompts the user and fills the arrays private void FillArrays() { string[] shoppingItems = CreateShoppingArray(); double[] itemPrices = CreatePricesArray(); for (int i = 0; i < shoppingItems.Length; i++) { Console.WriteLine("Enter the item"); shoppingItems[i] = Console.ReadLine(); Console.WriteLine("Enter the item price"); itemPrices[i] = double.Parse(Console.ReadLine()); }//end for CalculateResults(shoppingItems, itemPrices); }//end fill array private void CalculateResults(string[] shoppingList, double[] itemCost) { Console.WriteLine("*****************************\n"); double total = 0; for (int i = 0; i < itemCost.Length; i++) { //same as total=total + itemCost[]; total += itemCost[i]; } Console.WriteLine("Your Items"); for(int i=0;i< shoppingList.Length;i++) { Console.WriteLine(shoppingList[i] + "\t\t" + itemCost[i].ToString("c")); } Console.WriteLine("Total {0:C}", total); } } }
Here is the earlier code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WhileLoops { class Program { static void Main(string[] args) { Program p = new Program(); //uncomment the method you want to run //comment the ones you don't want to run p.ForLoopReview(); p.WhileLoopExample(); p.DoLoopExample(); Console.ReadKey(); } private void ForLoopReview() { string[] languages=new string[4]; languages[0] = "C++"; languages[1] = "C#"; languages[2] = "Java"; languages[3] = "php"; for (int i = 0; i < languages.Length; i++) { Console.WriteLine(languages[i]); }//end for }//end for loop review private void WhileLoopExample() { int x = 6; Console.WriteLine("While Loop OutPut"); while (x < 6) { Console.WriteLine(x); x++; } }//end while loop example private void DoLoopExample() { int x = 6; Console.WriteLine("Do loop output"); do { Console.WriteLine(x); x++; } while (x < 6); } }//end class }//end namespace
Monday, October 22, 2012
Thursday, October 18, 2012
Loops and arrays 1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ArraysandLoops { class Program { static void Main(string[] args) { Program p = new Program(); p.Run(); Console.ReadKey(); } private void Run() { DisplayArray(); InitializedArray(); } private string[] CreateArray() { //declare an array of strings string[] cheese = new string[5]; return cheese; // FillArray(cheese); } private string[] FillArray() { //create an array variable to store //the array returned by create array string[] queso = CreateArray(); //this loop starts at zero, and loops //until it is less than the length of the array (5) //i++ increments the counter by 1 for (int i = 0; i < queso.Length; i++) { Console.WriteLine("enter a cheese"); string cheeseName = Console.ReadLine(); //assigns it to the current index of the array queso[i] = cheeseName; }//end for loop return queso; }//end fill Array private void DisplayArray() { string[] fromage = FillArray(); Console.WriteLine("************************\n"); for (int i = 0; i < fromage.Length; i++) { Console.WriteLine(fromage[i]); }//end for Console.WriteLine(fromage[3]); }//end displayArray private void InitializedArray() { //create an array of integers and //assign them immediately int[] numbers = new int[] { 1, 3, 5, 6, 8, 2 }; //loop through the array to display them for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } }//end class }//end namespaceHere is the example of summing and averaging arrays
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SumandAverageArrays { class Program { static void Main(string[] args) { Program p = new Program(); p.CreateArray(); Console.ReadKey(); } private void CreateArray() { double[] numbers = new double[] { 2, 3.21, 4, 5.234, 1.2, 6 }; GetSum(numbers); } private void GetSum(double[] numberArray) { double sum=0; for(int i=0;i<numberArray.Length;i++) { //+= equivalent to sum = sum + numberarray[i] sum+=numberArray[i]; } double average = sum / numberArray.Length; Console.WriteLine("The sum is {0}", sum); Console.WriteLine("The average is {0}", average); //these built in methods do the same thing Console.WriteLine("**********************"); Console.WriteLine(numberArray.Sum()); Console.WriteLine(numberArray.Average()); } } }
Wednesday, October 17, 2012
Beginning Database Design
We began by looking at Visio 2010. We drug an Entity onto the grid and use the properties window to Name it DVD. Then we assigned a primary Key and some fields. We Determined that actor was a multi-valued attribute, meaning that every DVD probably has many Actors, so we broke it into its own entity.
There are Three Kinds of relationships:
*One to One, where each record in the primary key is related to no more than 1 record in the child table
*One to Many, where each record in the primary key table can be related to zero or several records in the child table
*Many To Many, where each record in the primary key table can be related to any number of records in the child table, and each record in the child table can be related to any number of records in the parent table. These must be resolved by adding a linking table
Next we determined that there is a many to many relationship between DVD and Actor.Here is the Diagram
Thursday, October 11, 2012
More if and else if
This is the code that is part of assignment 3
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Medicare { class Program { static void Main(string[] args) { //load the program Program p = new Program(); //call the display method p.Display(); //pause it Console.ReadKey(); } private void Display() { //get the inputs Console.WriteLine("Enter Your pay rate"); double rate = double.Parse(Console.ReadLine()); Console.WriteLine("Enter your hours"); double hours = double.Parse(Console.ReadLine()); //call the CalculateGross method and store the value //it returns in the local variable gross double gross = CalculateGrossPay(rate, hours); Console.WriteLine("Your gross pay is {0:C}", gross); //call the method to CalculateMedicare double med = CalculateMedicare(gross); Console.WriteLine("Your Medicare Deduction is {0:C}", med); } private double CalculateGrossPay(double rate, double hours) { //this method calcuates pay //it takes overtime into account double grossPay=0; if (hours > 40) { grossPay = rate * (40 + ((hours - 40) * 1.5)); } else { grossPay = rate * hours; } return grossPay; }//end calculateGrosspay private double CalculateMedicare(double gross) { double medicare = 0; //5000 > .2 //2000 to 4999 .1 //1999 or less .03 //uses and if, else if to calculate percentages if (gross >= 5000) { medicare = gross * .2; } else if (gross >= 2000) { medicare = gross * .1; } else { medicare = gross * .03; } return medicare; } } }
Here are the other if, else if examples
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Medicare { class Program { static void Main(string[] args) { //load the program Program p = new Program(); //call the display method p.Display(); //pause it Console.ReadKey(); } private void Display() { //get the inputs Console.WriteLine("Enter Your pay rate"); double rate = double.Parse(Console.ReadLine()); Console.WriteLine("Enter your hours"); double hours = double.Parse(Console.ReadLine()); //call the CalculateGross method and store the value //it returns in the local variable gross double gross = CalculateGrossPay(rate, hours); Console.WriteLine("Your gross pay is {0:C}", gross); //call the method to CalculateMedicare double med = CalculateMedicare(gross); Console.WriteLine("Your Medicare Deduction is {0:C}", med); } private double CalculateGrossPay(double rate, double hours) { //this method calcuates pay //it takes overtime into account double grossPay=0; if (hours > 40) { grossPay = rate * (40 + ((hours - 40) * 1.5)); } else { grossPay = rate * hours; } return grossPay; }//end calculateGrosspay private double CalculateMedicare(double gross) { double medicare = 0; //5000 > .2 //2000 to 4999 .1 //1999 or less .03 //uses and if, else if to calculate percentages if (gross >= 5000) { medicare = gross * .2; } else if (gross >= 2000) { medicare = gross * .1; } else { medicare = gross * .03; } return medicare; } } }
Tuesday, October 9, 2012
First Ifs (Selection) and a bit of assignment 2
Here is the piece of Assignment Two
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { //declare a constant const double MEDICARE = .03; static void Main(string[] args) { Program p = new Program(); //call show pay p.ShowNetPay(); } private double GrossPay(double hours, double rate) { //we pass hours and rate into the method and then //multiply them //not worried about overtime at this point return hours * rate; } private double CalculateMedicare(double gross) { //pass in gross and multiply it by constant return gross * MEDICARE; } private void ShowNetPay() { //enter the hours and rate Console.WriteLine("Enter hours worked"); double hours = double.Parse(Console.ReadLine()); Console.WriteLine("Enter the rate of Pay"); double rate = double.Parse(Console.ReadLine()); //get the gross pay by calling the method //and storing the value it returns double grosspay = GrossPay(hours, rate); //call the method for medicare double med = CalculateMedicare(grosspay); //do the calculation for net pay double netpay = grosspay - (med ); //Display the result Console.WriteLine("Your net pay is {0}", netpay); } } }
Here is the sample with if statements
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SelectionExamples { class Program { //first we will make a method to determine //if a number even //we also introduce tryParse static void Main(string[] args) { //initialize the rest of the class Program p = new Program(); //call the TestNumber method p.TestNumber(); //pause the program to wait for a key stroke Console.ReadKey(); } private void TestNumber() { //this gets an integer number from the user //if it is not a valid integer it propmpts the user //to start again with a valid integer number //declare a variable with a default of zero //the TryParse out parameter will assign //a new value to this variable int num = 0; //asl for a number from the user Console.WriteLine("Enter an Integer"); //the tryParse returns true or false (a boolean) //if the string on the console can be parsed as an integer //it returns true and assigns the value to num, //if not it returns false bool isInt = int.TryParse(Console.ReadLine(), out num); //if it is false prompt them to enter a valid number if (isInt == false) { Console.WriteLine("Make sure you enter an integer"); return; //end the method } //if it is true it will continue to execute the code //we call the TestForEven number, //and pass the num we got from the console //then we store the //result ("odd" or "even") in the variable numType string numType = TestForEven(num); //print out the value of numType Console.WriteLine(numType); } private string TestForEven(int number) { //create a variable with a default value //of odd string result = "odd"; //if then the condition in parenthesis //must resolve to true or false if (number % 2 == 0) //== means equal { result = "even"; } //return the result return result; } } }
Thursday, October 4, 2012
Methods and Operators
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { /****************** * + is addition * - is subtraction * * is multipication * / is division (if both sides are integer then any decimal part is dropped) * % modulus, returns the remainder from an integer division * follows same order of operations as algebra * all multiplications and divisions first left to right * all substractions and additons left to write * but what is in parenthesis is first * embedded parenthesis from inside out * */ class Program { private double total; private const double TAX = .095; static void Main(string[] args) { Program p = new Program(); //p.GetPrice(); //p.DisplayTotal(); int quotient = p.IntegerDivision(8, 3); Console.WriteLine("the quotient is {0}", quotient); int modulus = p.GetTheModulus(8, 3); Console.WriteLine("the remainder is {0}", modulus); Console.ReadKey(); } private void GetPrice() { Console.WriteLine("Enter the Price"); double price = double.Parse(Console.ReadLine()); total=GetTotal(price); } private double GetTotal(double pr) { return pr * (1 + TAX); } private void DisplayTotal() { Console.WriteLine("Your total is {0:c}",total); } private int IntegerDivision(int number1, int number2) { return number1 / number2; } private int GetTheModulus(int num1, int num2) { return num1 % num2; } } }
Tuesday, October 2, 2012
Gas Mileage with Methods
Here is the Gas Mileage calculator without methods that we did as the first assignment
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GasMileageCalculator { /********************* * This program will calculate miles per * gallon and price per mile * Steve Conger 9/27/2012 * *********************/ class Program { static void Main(string[] args) { //get inputs //prompt user Console.WriteLine("Enter the Total Mileage");//write prompt int miles = int.Parse (Console.ReadLine()); Console.WriteLine("Enter the total Gallons"); int gallons = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Price Per Gallon"); double price = double.Parse(Console.ReadLine()); //Calculate the outputs double milesPerGallon = (double) miles / gallons; double pricePerMile = price / milesPerGallon; // Display outputs Console.WriteLine(); Console.WriteLine("Your miles per gallon is {0:F2} \n", milesPerGallon); Console.WriteLine("The price per mile is {0:c}", pricePerMile); Console.ReadKey(); } } }
Methods
A few words about methods:
Methods are ways of braking up code into more manageable blocks.
Ideally each method does one thing. It can be one complicated thing involving several lines of code, but still one thing.
In order to execute a method must be called. You call a method by using its name and providing any required parameters. (See Example Two.)
Methods can only be called from other methods. At least one method must be called from the Main method to start the program
Methods can be private meaning they can only be seen by other methods in the current class, or they can be public meaning they can be seen by other classes and programs. (there are other options in between such as protected.)
Methods can be void (as in the first example) meaning that they return nothing to the calling method or they can return a value(see example two)
Here is the code from class. This version makes the variables have class scope, which means they can be accessed by any of the methods in the class.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GasMileageCalculatorWithMethods { class Program { /* * get inputs * calculate miles per Gallon * calculate price per mile * Display the outputs */ //declaring variables with class scope int miles; int gallons; double price; static void Main(string[] args) { //The Program class is not yet loaded //into memory, though the Main method is //because it is static, so we have to //load the class into memory with the new //keyword Program p = new Program(); p.CallMethods(); }//end main private void GetMiles() { Console.WriteLine("Please Enter the total Miles"); miles = int.Parse(Console.ReadLine()); }//end GetMiles private void GetGallons() { Console.WriteLine("Please Enter the total Gallons"); gallons = int.Parse(Console.ReadLine()); }//end getGallons private void GetPricePerGallon() { Console.WriteLine("Please Enter the price per Gallon"); price= double.Parse(Console.ReadLine()); } private double CalculateMilesPerGallon() { double mpg; mpg=miles / (double)gallons; return mpg; } private double CalculateCostPerMile() { return price / CalculateMilesPerGallon(); } private void DisplayResults() { Console.WriteLine("Your Miles per Gallon is {0:F2}", CalculateMilesPerGallon()); Console.WriteLine("The cost per mile is {0:C}", CalculateCostPerMile()); Console.ReadKey(); } private void CallMethods() { GetMiles(); GetGallons(); GetPricePerGallon(); DisplayResults(); } }//end class }//end namespace
Here is a second example of the same program. Instead of making variables with class level scope, the variables are passed as parameters to the appropriate methods
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GasMileageCalculatorMethods { class Program { //This program use methods with parameters // to calculate gas mileage double price; static void Main(string[] args) { //explain this Program p = new Program(); p.Display(); } private void Display() { //calling methods GetInputs(); Console.ReadKey(); } private void GetInputs() { //this method could be broken up as we did //in the example above Console.WriteLine("Enter the total Miles"); int miles = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Total gallons"); double gallons = double.Parse(Console.ReadLine()); //call and pass values double milesPerGallon=CalculateMileage(miles, gallons); Console.WriteLine("Enter the price per gallon"); price = double.Parse(Console.ReadLine()); double pricePerMile = CalculatePrice(price, milesPerGallon); DisplayOutputs(milesPerGallon, pricePerMile); } private double CalculateMileage(int m, double gals) { //explain return return m / gals; } private double CalculatePrice( double pr, double mpg) { return pr / mpg; } private void DisplayOutputs(double milesPerGallon, double pricePerMile) { //explain calling the method Console.WriteLine("You got {0:F2} miles per gallon", milesPerGallon); Console.WriteLine("The price per mile was {0:C}", pricePerMile); } } }