Difference between revisions of "Intro Week 6"
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
||
Line 3: | Line 3: | ||
Review Generic Lists | Review Generic Lists | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
List<int> scores = new List<int>(); //A list of ints | List<int> scores = new List<int>(); //A list of ints | ||
Line 32: | Line 32: | ||
are composed of several pieces of data that can be of different types. | are composed of several pieces of data that can be of different types. | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
struct student | struct student | ||
{ | { | ||
Line 40: | Line 40: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
struct player | struct player | ||
{ | { | ||
Line 78: | Line 78: | ||
Defining | Defining | ||
− | <csharp>public static string Hello () | + | <syntaxhighlight lang="csharp" line="1" >public static string Hello () |
{ | { | ||
return "Hello "; | return "Hello "; | ||
Line 90: | Line 90: | ||
Calling a finction in c# | Calling a finction in c# | ||
− | <csharp>string firstHello, jeffHello; //declare some strings | + | <syntaxhighlight lang="csharp" line="1" >string firstHello, jeffHello; //declare some strings |
firstHello = Hello(); //call the function Hello | firstHello = Hello(); //call the function Hello | ||
jeffHello = HelloToName("Jeff"); //call the function HelloToName | jeffHello = HelloToName("Jeff"); //call the function HelloToName |
Revision as of 18:28, 25 January 2016
Review Arrays
Review Generic Lists
1 List<int> scores = new List<int>(); //A list of ints
2
3 List<string> names = new List<string>(); //A list of strings
4
5 //add some ints to the scores list
6 scores.Add(100);
7 scores.Add(7);
8
9 //Add some names to the names list
10 names.Add("Jeff");
11 names.Add("Uncle Eddie");
12
13 foreach(string name in names)
14 {
15 Console.WriteLine(name);
16 }
Simple arrays and multidimensional arrays
Struct
Structures -struct are composed of several pieces of data that can be of different types.
1 struct student
2 {
3 public int oasis id;
4 public string name;
5 public double GPA;
6 }
1 struct player
2 {
3 public int playerID;
4 public string playerName;
5 public difficulty playerLevel;
6 }
In class demo coins Quarter class *Mint *Year
Coin class *Mint *Year *Value
Functions/Methods
Function help with code reuse. If your going to use it more than once make it into a funtions. syntax simple
[access-modifier] return type indentifier ( [parateters] ) { //some code }
access-modifiers
public : mean that this method can be called from anywhere else in the program
Static
- static means that
Defining
1 public static string Hello ()
2 {
3 return "Hello ";
4 }
5
6 public static string HelloToName (string Name)
7 {
8 return "Hello " + Name;
9 }
Calling a finction in c#
1 string firstHello, jeffHello; //declare some strings
2 firstHello = Hello(); //call the function Hello
3 jeffHello = HelloToName("Jeff"); //call the function HelloToName
Console Example /infod/jeff/classSource/class3/function.cs - source
Hello World! Hello Hello Jeff Hello Marge C:\User\csharp>
Home work
Sweet card program http://iam.colum.edu/introtoprogramming/Jeff/SP10/ConsoleApplicationStructs.zip have it for super freeeee as in free beer
Write a csharp program that uses a coin struct to make a coin collection
start with
12 quarters
22 dimes
5 nickels
and 174 pennies.
After you have made the coin collection (Array or List). Write a method that can calculate the value of the collection.
Bonus
write a csharp program that can make correct change given an amount of money.
ie type in 1.08 and it will give back a coin collection of
4 quarters
1 nickel
3 pennies