Difference between revisions of "OOP Class7"

esse quam videri
Jump to: navigation, search
(Properties Private instance data members - accessors)
 
(28 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:OOP]]
+
[[Category:IAM Classes]]
 
+
[[OOP Arrays]]
 
 
==Shared Members - static members==
 
aka Shared Properties
 
 
 
Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type
 
 
 
<csharp>public class Dog
 
{
 
//some code
 
 
 
static private int dogCount; // total number of dogs
 
  
public Dog()
+
==Review==
{
 
barkSound = "Woof!!!";
 
//Add a dog to the total dog count
 
dogCount++;
 
}
 
public Dog(string newName)
 
{
 
name = newName;
 
barkSound = "Woof!!!";
 
//Add a dog to the total dog count
 
dogCount++;
 
}</csharp>
 
  
Static dougCount Example <br />
+
{{OOP Review Midterm}}
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/dogStatic.cs dogStatic.cs] - source
 
  
 +
Cat app with multiple toys
  
 +
Course App in console and windows
  
==access-modifiers==
+
==Structs==
public +
 
:most permissive access. No restrictions on access
 
private -
 
:least permissive access level. Only available to the class in which it was declared
 
protected #
 
:accessible within its class and by derived classes
 
internal
 
:accessible only within files in the same assembly
 
protected internal
 
:combination of protected and internal
 
Public Dog
 
[[Image:DogSimpleUML.png]]
 
  
Dog with private age
+
Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes
[[Image:DogsimpleAgePrivate.png]]
+
Syntax
  
==Private instance data members - accessors==
+
[ attributes] [access-modifiers] struct identifier
Microsoft has stared calling private variables with accessors Properties
+
[:interface-list {struct members}
  
Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpful when you want to also do other things when a data member is changed or change the rutern value under certain conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible
+
<syntaxhighlight lang="csharp">struct Dog
 
 
<csharp>//private string color read/write
 
private string color;
 
 
 
public string Color
 
 
{
 
{
  get
+
public string name;
  {
+
public string weight;
  return color;
+
public int age;
  }
+
}</syntaxhighlight>
  set
 
  {
 
  color = value;
 
  }
 
}
 
 
 
//private string color read only
 
private string color;
 
 
 
public string Color
 
{
 
  get
 
  {
 
  return color;
 
  }
 
}</csharp>
 
  
 +
==Enumerators==
  
Another property that is a good candidate for a private instance data member is the dogs age
+
Enumerators are used to set predefined list of named constants.
 +
Syntax
  
<csharp>
+
[ attributes] [modifiers] enum identifier
private string age;
+
[:base-type {enumerator-list};  
  
public int Age
+
<syntaxhighlight lang="csharp">//An enumerator for ServingSizes at BK
 +
enum ServingSizes : uint
 
{
 
{
   //age can only be accessed with get there is no set accessor
+
   Small = 0,
  //age must be set with HappyBirthday()
+
  Regular = 1,
  get
+
  Large = 2,
 +
  SuperSize = 3
 +
}</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="csharp">//another more useful example
 +
// forced sequence to start 
 +
// from 1 instead of 0 (default)
 +
enum Months
 
   {
 
   {
  return age;
+
    January = 1, February , March, April ,
  }
+
    May , June , July , August , Sept , Oct , Nov , Dec
}
+
   }</syntaxhighlight>
 
 
public int HappyBirthday()
 
{
 
   age++;
 
  return age;
 
}
 
 
 
 
 
</csharp>
 
 
 
===In class===
 
 
 
==In Class Project==
 
 
 
*Build a Dog class with a private variable called age.
 
*Make a public Accessor for the private age the is read only
 
*Make a method that increments the age of your dog
 
*Make a test class for the new property and accessor
 
  
<!--In class build triangle class.
+
Enumerator Example [http://iam.colum.edu/Poop/gbrowser.php?file=/classsource/class2/Enum.cs Enum.cs]
Properties
 
  
* sideA
 
* sideB
 
* sideC
 
 
Methods
 
 
* Area //A method that return the area of the triangle
 
 
      use Heronian formula which is able to compute the area of a triange by knowing the length of the three sides.
 
      triangle area given a,b,c = sqrt(s(s-a)(s-b)(s-c)) when s = (a+b+c)/2 (Heron's formula)
 
 
<csharp>// Heronian formula
 
double s = (a + b + c) / 2.0;
 
double dArea = Math.Sqrt(s*(s-a)*(s-b)*(s-c));
 
</csharp>
 
 
Once you have built the triangle class build a class to test it.
 
The test class should create a triangle with
 
sideA= 3
 
sideB= 4
 
sideC= 5
 
 
and then display the area as text in the console.
 
 
http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/tri_class.cs tri_class.cs]
 
-->
 
 
 
 
Here's a class of dogs that return different color depending on whether on not they are clean. It uses private private instance data members
 
 
 
 
==Visual Studio 2005==
 
 
Pass out MSDNAA Visual Studio 2005 DVD
 
 
Visual Studio Demo
 
 
Demo of students diagram
 
 
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class7/StudentDiagram/StudentDiagram
 
 
http://iam.colum.edu/oop/classsource/class7/StudentDiagram.zip
 
 
 
[[OOP Arrays]]
 
  
 
==Fancy Arrays==
 
==Fancy Arrays==
Line 178: Line 67:
 
single dimension arrays
 
single dimension arrays
  
<csharp>string [] aryNames = new string[5];
+
<syntaxhighlight lang="csharp">string [] aryNames = new string[5];
 
  
 
  
aryNames [0] = "Feta";
+
aryNames [0] = "Feta";
aryNames [1] = "Colby";
+
aryNames [1] = "Colby";
aryNames [2] = "Gruyere";
+
aryNames [2] = "Gruyere";
aryNames [3] = "Edam";
+
aryNames [3] = "Edam";
aryNames [4] = "Colby";</csharp>
+
aryNames [4] = "Colby";</syntaxhighlight>
  
 
Arrays are actually objects and support various methods.
 
Arrays are actually objects and support various methods.
Line 194: Line 83:
 
* Reverse
 
* Reverse
 
* Sort
 
* Sort
 
  
 
GetValue
 
GetValue
  
<csharp>string s = aryNames.GetValue(1).ToString();</csharp>
+
<syntaxhighlight lang="csharp">string s = aryNames.GetValue(1).ToString();</syntaxhighlight>
 
:s="Colby"
 
:s="Colby"
  
 
IndexOf
 
IndexOf
  
 
+
<syntaxhighlight lang="csharp">int i =  Array.IndexOf(aryNames, "Colby");</syntaxhighlight>
<csharp>int i =  Array.IndexOf(aryNames, "Colby");</csharp>
 
 
:i=1
 
:i=1
  
 
LastIndexOf
 
LastIndexOf
  
<csharp>int Lasti= Array.LastIndexOf(aryNames, "Colby");</csharp>
+
<syntaxhighlight lang="csharp">int Lasti= Array.LastIndexOf(aryNames, "Colby");</syntaxhighlight>
 
:Lasti=4
 
:Lasti=4
  
 
SetValue
 
SetValue
<csharp>
+
<syntaxhighlight lang="csharp">
 
     aryNames.SetValue("Gouda", 1);
 
     aryNames.SetValue("Gouda", 1);
     string setValue = aryNames.GetValue(1).ToString();</csharp>
+
     string setValue = aryNames.GetValue(1).ToString();</syntaxhighlight>
 
Now Array(1) == "Gouda"
 
Now Array(1) == "Gouda"
  
 
Reverse
 
Reverse
 
:Reverse reverses the array
 
:Reverse reverses the array
<csharp>Array.Reverse(aryNames);</csharp>
+
<syntaxhighlight lang="csharp">Array.Reverse(aryNames);</syntaxhighlight>
 
 
 
 
 
The Reversed array looks like Reverse
 
The Reversed array looks like Reverse
Line 234: Line 121:
 
:The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
 
:The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
 
   
 
   
<csharp>Array.Sort(aryNames);</csharp>
+
<syntaxhighlight lang="csharp">Array.Sort(aryNames);</syntaxhighlight>
 
 
 
 
 
The Sorted Array List Looks like
 
The Sorted Array List Looks like
Line 265: Line 152:
 
types of collections
 
types of collections
  
    * set - no index
+
* set - no index
    * list - has index
+
* list - has index
    * map - has key/value pair
+
* map - has key/value pair
  
 
Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.
 
Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.
Line 275: Line 162:
 
Map types also have an index but it is not necessarily an integer. It could be anything unique.
 
Map types also have an index but it is not necessarily an integer. It could be anything unique.
  
===ArrayList===
+
===List===
The Array List of now part of System.Collections. In order to user it you must add a using statement
+
The List of now part of System.Collections. In order to user it you must add a using statement
<csharp>using System.Collections;</csharp>
+
<syntaxhighlight lang="csharp">using System.Collections.Generic;</syntaxhighlight>
  
 
create an arraylist
 
create an arraylist
  
<csharp>ArrayList myArrayList =  new ArrayList();</csharp>
+
<syntaxhighlight lang="csharp">List<String> myList =  new List<String>();</syntaxhighlight>
  
 
add and element or two
 
add and element or two
<csharp>
+
<syntaxhighlight lang="csharp">
myArrayList.Add("Feta");
+
myList.Add("Feta");
myArrayList.Add("Colby");
+
myList.Add("Colby");
myArrayList.Add("Gruyere");
+
myList.Add("Gruyere");
myArrayList.Add("Edam");
+
myList.Add("Edam");
myArrayList.Add("Colby");
+
myList.Add("Colby");
</csharp>
+
</syntaxhighlight>
This produces and Array list like
+
This produces and List like
 
<pre>
 
<pre>
 
Feta
 
Feta
Line 302: Line 189:
 
Insert an element
 
Insert an element
  
<csharp>myArrayList.Insert(1,"Gouda");</csharp>
+
<syntaxhighlight lang="csharp">myList.Insert(1,"Gouda");</syntaxhighlight>
 
 
 
 
 
Now our ArrayList Looks like
 
Now our ArrayList Looks like
Line 316: Line 203:
 
Remove an element
 
Remove an element
  
<csharp>//remove by index
+
<syntaxhighlight lang="csharp">//remove by index
myArrayList.RemoveAt(1);
+
myList.RemoveAt(1);
 
//remove by data
 
//remove by data
myArrayList.Remove("Colby"); //removes first occurance of Colby
+
myList.Remove("Colby"); //removes first occurance of Colby
</csharp>  
+
</syntaxhighlight>  
  
Now our Array List looks like
+
Now our List looks like
 
<pre>
 
<pre>
 
Feta
 
Feta
Line 329: Line 216:
 
Colby</pre>
 
Colby</pre>
  
Pros and con of ArrayList
+
Pros and con of Generic Lists
 
Pros
 
Pros
 
 
* No fixed size
 
* No fixed size
 
* Insert and Remove
 
* Insert and Remove
  
 
Cons
 
Cons
 
 
* Slower than arrays
 
* Slower than arrays
 
* Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.
 
* Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.
Line 349: Line 234:
 
foreach
 
foreach
  
<csharp>foreach (string n in aryNames)
+
<syntaxhighlight lang="csharp">foreach (string n in aryNames)
 
{
 
{
 
  aryDropDown.Items.Add(n);
 
  aryDropDown.Items.Add(n);
}</csharp>
+
}</syntaxhighlight>
 +
 
 +
==Pair Programming==
 +
 
 +
[http://www.extremeprogramming.org/rules/pair.html Pair Programming]
 +
 
 +
We are going to pair off to do our next assignment. Many of the paired programming principles come from XP (http://www.extremeprogramming.org/ Extreeme Progamming)
 +
 
 +
Pairs are good because
 +
*Pairs keep each other on track
 +
*A partner can help when you are stuck
 +
*Keep each other accountable
 +
 
 +
 
 +
Some XP Principles
  
 +
Fail - If you are having trouble succeeding fail.
  
 +
Baby Steps - Do the smallest thing that you possible can that moves you in the right direction
  
<!--
+
Integrate and communicate your code often. Try posting up your code and emailing or plan on meeting several times.
  
==Motorvehicle Diagram==
+
When two pair programmers are together one you should sit a one computer (yeah that right two of you at one computer) and one should type while the other watches and reflects. Feel free to slide the keyboard back and forth when someone get tired, stuck or has a new idea.
  
[[Oop Motorvehicle Diagram]] -->
+
I would like you and your paired partner to create and demonstrate classes in c# from the diagram below.
  
 
==Home Work==
 
==Home Work==
  
Build and Demonstrate The classes in the Student Diagram in VS2005.
 
  
Build a UML Diagram that shows your classes and how they interact.
+
 
 +
Practice for Medterm Exam [[OOP Students Classes Diagram]]
 +
 
 +
If you need to add any supporting methods or properties that are not on the diagram feel free.
 +
 
 +
 
 +
<pre>
 +
CourseName:Object Oriented Programming CoureseNumber:36-2600
 +
Hello my name is Jeff Meyers my ID is 47167
 +
I'm teaching:
 +
CourseName:Object Oriented Programming CoureseNumber:36-2600
 +
Hello my name is Sean  my ID is 666
 +
I'm taking:
 +
CourseName:Object Oriented Programming CoureseNumber:36-2600
 +
CourseName:Writing and Retoric CoureseNumber:42-1000
 +
ConsoleApplicationExtreemeCourse.Pencil is sharp
 +
ConsoleApplicationExtreemeCourse.Pencil is not sharp
 +
</pre>

Latest revision as of 16:30, 10 June 2019

OOP Arrays

Review

  • OOP Class1 Introduction to .NET and C#
    • Compliled vs Interpreted Languages
  • OOP Class2 C# fundamentals
    • Basic Data Types
    • Operators
    • Conversion Casting
    • Constants
    • Arrays
      • Single Arrays
      • Multi Dimensianal Arrays
      • Jagged Arrays
      • Enumerators
  • OOP Class3 CSharp Control Structures
    • Operators
    • Branching Statements
      • Branching
        • If
        • Switch
      • Looping
        • for
        • foreach
        • do
        • while
  • OOP Class4 Classes and Objects
    • Encapsulation
    • Classes
      • Fields
      • Methods
      • Constuctor
    • Overloading
  • OOP Class5 Object Relationships
    • Access Modifiers
    • Accessors
    • Static Members
    • Relationships
      • Association 'Uses a'
      • Containment 'Has a'
      • Inhertance 'Is a'
  • OOP Class6 Object Relation Ships 2 / UML
    • UML
    • Inheritance 'Is A'
    • Virual Functions Override
    • Polymorphism
    • Structs
  • OOP Class7 Polymorphism / Visual Studio 2005
    • Fancy Arrays
    • Colections
    • Polymorphism

Cat app with multiple toys

Course App in console and windows

Structs

Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes Syntax

[ attributes] [access-modifiers] struct identifier [:interface-list {struct members}

struct Dog
{
 public string name;
 public string weight;
 public int age;
}

Enumerators

Enumerators are used to set predefined list of named constants. Syntax

[ attributes] [modifiers] enum identifier [:base-type {enumerator-list};

//An enumerator for ServingSizes at BK
enum ServingSizes : uint
{
  Small = 0,
  Regular = 1,
  Large = 2,
  SuperSize = 3
}
//another more useful example
// forced sequence to start  
// from 1 instead of 0 (default)
enum Months 
  {
    January = 1, February , March, April ,
    May , June , July , August , Sept , Oct , Nov , Dec 
  }

Enumerator Example Enum.cs


Fancy Arrays

Arrays

Regular Array objects are like list collections

Arrays are groups of variables of the same type

Syntax
        type [] identifier

single dimension arrays

string [] aryNames = new string[5];
  		
aryNames [0] = "Feta";
aryNames [1] = "Colby";
aryNames [2] = "Gruyere";
aryNames [3] = "Edam";
aryNames [4] = "Colby";

Arrays are actually objects and support various methods.

  • GetValue
  • IndexOf
  • LastIndexOf
  • SetValue
  • Reverse
  • Sort

GetValue

string s = aryNames.GetValue(1).ToString();
s="Colby"

IndexOf

int i =  Array.IndexOf(aryNames, "Colby");
i=1

LastIndexOf

int Lasti= Array.LastIndexOf(aryNames, "Colby");
Lasti=4

SetValue

    aryNames.SetValue("Gouda", 1);
    string setValue = aryNames.GetValue(1).ToString();

Now Array(1) == "Gouda"

Reverse

Reverse reverses the array
Array.Reverse(aryNames);

The Reversed array looks like Reverse

0 Colby
1 Edam
2 Gruyere
3 Gouda
4 Feta

Sort

The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
Array.Sort(aryNames);

The Sorted Array List Looks like

0 Colby
1 Edam
2 Feta
3 Gouda
4 Gruyere

Array Example 2

Array2.cs

Pros and Cons of working with arrays pros

  • East to use
  • Fast to alter
  • Flast to iterate though
  • Specific type of elements ( single type)

cons

  • Fixed size
  • Insterting is difficult

Collections

Collections are groups of objects. types of collections

  • set - no index
  • list - has index
  • map - has key/value pair

Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.

List types of collections have an integer for an indexer. This gives them a unique identifier and an order.

Map types also have an index but it is not necessarily an integer. It could be anything unique.

List

The List of now part of System.Collections. In order to user it you must add a using statement

using System.Collections.Generic;

create an arraylist

List<String> myList =  new List<String>();

add and element or two

myList.Add("Feta");
myList.Add("Colby");
myList.Add("Gruyere");
myList.Add("Edam");
myList.Add("Colby");

This produces and List like

Feta
Colby
Gruyere
Edam
Colby

Insert an element

myList.Insert(1,"Gouda");

Now our ArrayList Looks like

Feta
Gouda
Colby
Gruyere
Edam
Colby

Remove an element

//remove by index
myList.RemoveAt(1);
//remove by data
myList.Remove("Colby");		//removes first occurance of Colby

Now our List looks like

Feta
Gruyere
Edam
Colby

Pros and con of Generic Lists Pros

  • No fixed size
  • Insert and Remove

Cons

  • Slower than arrays
  • Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.

ArrayList Example

ArrayList.cs

Polymorphism

Arrays and Collections of objects allow for polymorphism.

foreach

foreach (string n in aryNames)
{
 aryDropDown.Items.Add(n);
}

Pair Programming

Pair Programming

We are going to pair off to do our next assignment. Many of the paired programming principles come from XP (http://www.extremeprogramming.org/ Extreeme Progamming)

Pairs are good because

  • Pairs keep each other on track
  • A partner can help when you are stuck
  • Keep each other accountable


Some XP Principles

Fail - If you are having trouble succeeding fail.

Baby Steps - Do the smallest thing that you possible can that moves you in the right direction

Integrate and communicate your code often. Try posting up your code and emailing or plan on meeting several times.

When two pair programmers are together one you should sit a one computer (yeah that right two of you at one computer) and one should type while the other watches and reflects. Feel free to slide the keyboard back and forth when someone get tired, stuck or has a new idea.

I would like you and your paired partner to create and demonstrate classes in c# from the diagram below.

Home Work

Practice for Medterm Exam OOP Students Classes Diagram

If you need to add any supporting methods or properties that are not on the diagram feel free.


CourseName:Object Oriented Programming CoureseNumber:36-2600
Hello my name is Jeff Meyers my ID is 47167
I'm teaching:
CourseName:Object Oriented Programming CoureseNumber:36-2600
Hello my name is Sean  my ID is 666
I'm taking:
CourseName:Object Oriented Programming CoureseNumber:36-2600
CourseName:Writing and Retoric CoureseNumber:42-1000
ConsoleApplicationExtreemeCourse.Pencil is sharp
ConsoleApplicationExtreemeCourse.Pencil is not sharp