Difference between revisions of "OOP Class7"

esse quam videri
Jump to: navigation, search
(In Class Project)
(Fancy Arrays)
Line 109: Line 109:
 
<csharp>string [] aryNames = new string[5];
 
<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";</csharp>
  
 
Arrays are actually objects and support various methods.
 
Arrays are actually objects and support various methods.
Line 123: Line 123:
 
* Reverse
 
* Reverse
 
* Sort
 
* Sort
 
  
 
GetValue
 
GetValue
Line 131: Line 130:
  
 
IndexOf
 
IndexOf
 
  
 
<csharp>int i =  Array.IndexOf(aryNames, "Colby");</csharp>
 
<csharp>int i =  Array.IndexOf(aryNames, "Colby");</csharp>
Line 194: Line 192:
 
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 214: Line 212:
 
add and element or two
 
add and element or two
 
<csharp>
 
<csharp>
myArrayList.Add("Feta");
+
myArrayList.Add("Feta");
myArrayList.Add("Colby");
+
myArrayList.Add("Colby");
myArrayList.Add("Gruyere");
+
myArrayList.Add("Gruyere");
myArrayList.Add("Edam");
+
myArrayList.Add("Edam");
myArrayList.Add("Colby");
+
myArrayList.Add("Colby");
 
</csharp>
 
</csharp>
 
This produces and Array list like
 
This produces and Array list like
Line 260: Line 258:
 
Pros and con of ArrayList
 
Pros and con of ArrayList
 
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.

Revision as of 14:23, 16 June 2009


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() { 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
dogStatic.cs - source


access-modifiers

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 DogSimpleUML.png

Dog with private age DogsimpleAgePrivate.png



In Class Project

  • Start XP Pre Midterm Practice



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


OOP Arrays

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

<csharp>string [] aryNames = new string[5];

aryNames [0] = "Feta"; aryNames [1] = "Colby"; aryNames [2] = "Gruyere"; aryNames [3] = "Edam"; aryNames [4] = "Colby";</csharp>

Arrays are actually objects and support various methods.

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

GetValue

<csharp>string s = aryNames.GetValue(1).ToString();</csharp>

s="Colby"

IndexOf

<csharp>int i = Array.IndexOf(aryNames, "Colby");</csharp>

i=1

LastIndexOf

<csharp>int Lasti= Array.LastIndexOf(aryNames, "Colby");</csharp>

Lasti=4

SetValue <csharp>

   aryNames.SetValue("Gouda", 1);
   string setValue = aryNames.GetValue(1).ToString();</csharp>

Now Array(1) == "Gouda"

Reverse

Reverse reverses the array

<csharp>Array.Reverse(aryNames);</csharp>

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

<csharp>Array.Sort(aryNames);</csharp>

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.

ArrayList

The Array List of now part of System.Collections. In order to user it you must add a using statement <csharp>using System.Collections;</csharp>

create an arraylist

<csharp>ArrayList myArrayList = new ArrayList();</csharp>

add and element or two <csharp> myArrayList.Add("Feta"); myArrayList.Add("Colby"); myArrayList.Add("Gruyere"); myArrayList.Add("Edam"); myArrayList.Add("Colby"); </csharp> This produces and Array list like

Feta
Colby
Gruyere
Edam
Colby

Insert an element

<csharp>myArrayList.Insert(1,"Gouda");</csharp>

Now our ArrayList Looks like

Feta
Gouda
Colby
Gruyere
Edam
Colby

Remove an element

<csharp>//remove by index myArrayList.RemoveAt(1); //remove by data myArrayList.Remove("Colby"); //removes first occurance of Colby </csharp>

Now our Array List looks like

Feta
Gruyere
Edam
Colby

Pros and con of ArrayList 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

<csharp>foreach (string n in aryNames) {

aryDropDown.Items.Add(n);

}</csharp>

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

UML of You class diagram.

700 px wide large image

175 px thumbnail

Make sure there is you name and email.

Quiz midterm Next Week

use Pair Programming to make tests and classes from the following UML

OOP Students Classes Diagram

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


Test Student()

Jeff Meyers says hello.
StudentDiagram.Student Jeff Meyers
StudentID = 0000
0 Courses

Test Student("Matina" , "Navratilova" )

Matina Navratilova says hello.
StudentDiagram.Student Matina Navratilova
StudentID = 0000
0 Courses

Test Student("Dan", "Rockwood")

Dan Rockwood says hello.
StudentDiagram.Student Dan Rockwood
StudentID = 0000
0 Courses

Make Some Course
CourseName: Object Oriented Programming CourseName: 36-1300

Add Course
StudentDiagram.Student Dan Rockwood
StudentID = 0000
5 Courses
CourseName: Object Oriented Programming CourseName: 36-1300
CourseName: Media Theory and Design 1 CourseName: 36-1000
CourseName: Media Theory and Design 2 CourseName: 36-2000
CourseName: English 101 CourseName: 52-1000
CourseName: Gym 101 CourseName: 11-1111
StudentDiagram.Student Dan Rockwood
StudentID = 0000
4 Courses
CourseName: Object Oriented Programming CourseName: 36-1300
CourseName: Media Theory and Design 1 CourseName: 36-1000
CourseName: English 101 CourseName: 52-1000
CourseName: Gym 101 CourseName: 11-1111
StudentDiagram.Student Dan Rockwood
StudentID = 0000
5 Courses
CourseName: Object Oriented Programming CourseName: 36-1300
CourseName: Media Theory and Design 1 CourseName: 36-1000
CourseName: test 101 CourseName: 00-0000
CourseName: English 101 CourseName: 52-1000
CourseName: Gym 101 CourseName: 11-1111

Test Instructor
StudentDiagram.Instructor Edna Krabappel
FacultyID = 0000
1 Course
CourseName: English 101 CourseName: 52-1000

Test Pencil
StudentDiagram.Pencil.isSharp = True
Dan has no pencil
StudentDiagram.Pencil.isSharp = True
All work and no play makes Dan a dull student.
StudentDiagram.Pencil.isSharp = False
All work and no play makes Dan a dull student.
StudentDiagram.Pencil.isSharp = False