Difference between revisions of "Intro Week 6"

esse quam videri
Jump to: navigation, search
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Review Arrays==
 
==Review Arrays==
  
Simple arrays and multidimesional arrays
+
Review Generic Lists
 +
 
 +
<syntaxhighlight lang="csharp">
 +
List<int> scores = new List<int>();  //A list of ints
 +
 
 +
List<string> names = new List<string>(); //A list of strings
 +
 
 +
//add some ints to the scores list
 +
scores.Add(100);
 +
scores.Add(7);
 +
 
 +
//Add some names to the names list
 +
names.Add("Jeff");
 +
names.Add("Uncle Eddie");
 +
 
 +
foreach(string name in names)
 +
{
 +
  Console.WriteLine(name);
 +
}
 +
 
 +
</syntaxhighlight>
 +
 
 +
Simple arrays and multidimensional arrays
  
 
[[OOP Arrays]]
 
[[OOP Arrays]]
Line 10: 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">
 
struct student
 
struct student
 
{
 
{
Line 17: Line 39:
 
public double GPA;
 
public double GPA;
 
}
 
}
</csharp>
+
</syntaxhighlight>
<csharp>
+
<syntaxhighlight lang="csharp">
 
struct player
 
struct player
 
{
 
{
Line 25: Line 47:
 
public difficulty playerLevel;
 
public difficulty playerLevel;
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
  In class demo coins
 
  In class demo coins
Line 42: Line 64:
 
syntax simple
 
syntax simple
  
 +
<pre>
 
[access-modifier] return type indentifier ( [parateters] )
 
[access-modifier] return type indentifier ( [parateters] )
 
{
 
{
 
     //some code
 
     //some code
 
}
 
}
 +
</pre>
  
 
access-modifiers<br>
 
access-modifiers<br>
public<br>
+
public : mean that this method can be called from anywhere else in the program<br>
private<br>
+
Static
protected<br>
+
:static means that
internal<br>
 
protected internal<br>
 
more on these next week.
 
  
 
Defining
 
Defining
  
<csharp>string Hello ()
+
<syntaxhighlight lang="csharp">public static string Hello ()
 
{
 
{
 
     return "Hello ";
 
     return "Hello ";
 
}
 
}
  
string HelloToName (string Name)
+
public static string HelloToName (string Name)
 
{
 
{
 
     return "Hello " + Name;
 
     return "Hello " + Name;
}</csharp>
+
}</syntaxhighlight>
  
 
Calling a finction in c#
 
Calling a finction in c#
  
<csharp>string firstHello, jeffHello;  //declare some strings
+
<syntaxhighlight lang="csharp">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
</csharp>
+
</syntaxhighlight>
  
 
Console Example
 
Console Example
 
/infod/jeff/classSource/class3/function.cs - source
 
/infod/jeff/classSource/class3/function.cs - source
 +
 
<pre>
 
<pre>
C:\User\csharp>csc function.cs
 
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
 
for Microsoft (R) .NET Framework version 1.1.4322
 
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
 
 
 
C:\User\csharp>function.cs
 
 
C:\User\csharp>function.exe
 
 
Hello World!
 
Hello World!
 
Hello
 
Hello
Line 96: Line 109:
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/function.cs function.cs]
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/function.cs function.cs]
  
Passing variable into a function/method passes be instance. Variables passed by instance do not actually pass in the original variable rather a copy or new instance of the variable is passed. It you want to pass in the actual variable you need to pass be referance
+
 
Examples of passesing by instamce and passing by refecnce...
+
==Home work==
/infod/jeff/classSource/class3/instanceReference.aspx - Source
+
 
 +
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

Latest revision as of 03:21, 9 February 2016

Review Arrays

Review Generic Lists

List<int> scores = new List<int>();  //A list of ints 

List<string> names = new List<string>(); //A list of strings

//add some ints to the scores list
scores.Add(100);
scores.Add(7);

//Add some names to the names list
names.Add("Jeff");
names.Add("Uncle Eddie");

foreach(string name in names)
{
   Console.WriteLine(name);
}

Simple arrays and multidimensional arrays

OOP Arrays

Struct

Structures -struct are composed of several pieces of data that can be of different types.

struct student
{
public int oasis id;
public string name;
public double GPA;
}
struct player
{
public int playerID;
public string playerName;
public difficulty playerLevel;
}
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

public static string Hello ()
{
    return "Hello ";
}

public static string HelloToName (string Name)
{
    return "Hello " + Name;
}

Calling a finction in c#

string firstHello, jeffHello;   //declare some strings
firstHello = Hello();   //call the function Hello
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>

function.cs


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