Difference between revisions of "OOP Class6"

esse quam videri
Jump to: navigation, search
(Homework)
 
(42 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:OOP]]
+
[[Category:IAM Classes]]
==What is UML==
 
;UML : Unified Modeling Language.
 
:"A modeling language is a language whose vocabulary and rules focus on the conceptual and physical representation of a system"
 
The Unified Modeling Language User Guide, Booch Rumbaugh Jacobson
 
ACM Press, Addison Wesley 1999 ISBN 0201571684
 
[http://www.amazon.com/gp/product/0201571684/104-7742183-3467114?v=glance&n=283155 The Unified Modeling Language User Guide]
 
==Simple Notation==
 
Class notation in UML
 
  
The name of the Class should go inside of a box
+
==Review==
 +
;Accessibility: restrict the visibility of a class and it's members
 +
;Classes: Abstraction that has properties and methods. properties and the ''nouns'' and methods are the ''verbs''Classes are used to define objects.
 +
;Objects: Instance of a class
 +
;Abstraction : Factor out details to work on fewer concepts at a time
 +
;Encapsulation: Allows us to use objects with out completely understanding how everything inside the object works. Encapsulation also allows the internal state of objects to be changed easily without affecting other objects or interfaces. Real barking dogs...[http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class4/DogBark DogBark] an example of how you don't need to know how all of the classes work just how to use them
 +
;Polymorphism: many forms that have the same attributes and abilities
 +
;UML : Unified Modeling Language.
  
{| align="center" cellspacing="0" style="border:2px solid black;"
+
TODO
|Class Name
+
;Virtual Functions: Functions that can be overridden.
|}
+
;Static: Members that are associayed with a class not an instance of the class
  
 +
==Shared Members - static members==
 +
aka Shared Properties
  
Then Two lines are added. The first line separates Attributes. The second line separates Operations.
+
Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type
Attributes are the equivalent of fields or properites in c# and Operations are the same as methods.
 
  
{|  align="center" border = 0
+
<syntaxhighlight lang="csharp">public class Dog
|
+
{
 +
//some code
  
{|  cellspacing="0" style="border:2px solid black;"
+
static private int dogCount; // total number of dogs
|Class Name
 
|-
 
| style="border-Top:2px solid black;" |&nbsp;
 
|-
 
| style="border-Top:2px solid black;" |&nbsp;
 
|}
 
  
|
+
public Dog()
 
+
{
{| align="center" cellspacing="0" Border = 0
+
barkSound = "Woof!!!";
| <--'''Class Name'''
+
//Add a dog to the total dog count
|-
+
dogCount++;
|  <--'''Attributes'''
+
}
|-
+
public Dog(string newName)
| <--'''Opertaions'''
+
{
|}
+
name = newName;
 
+
barkSound = "Woof!!!";
|}
+
//Add a dog to the total dog count
 +
dogCount++;
 +
}</syntaxhighlight>
 +
}
  
 +
Static dougCount Example <br />
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/dogStatic.cs dogStatic.cs] - source
  
A Rectagle Class might look like this
 
  
{|  align="center" cellspacing="0" style="border:2px solid black;"
 
|Rectangle
 
|-
 
| style="border-Top:2px solid black;" | height
 
|-
 
| width
 
|-
 
| style="border-Top:2px solid black;" | area()
 
|-
 
| add()
 
|-
 
| move()
 
|-
 
| copy()
 
|-
 
| isEmpty()
 
|}
 
  
You can also specify Attribute types with a :
 
  
{|  align="center" cellspacing="0" style="border:2px solid black;"
 
|Rectangle
 
|-
 
| style="border-Top:2px solid black;" | height : Float
 
|-
 
| width : Float
 
|-
 
| style="border-Top:2px solid black;" | area()
 
|-
 
| add()
 
|-
 
| move()
 
|-
 
| copy()
 
|-
 
| isEmpty()
 
|}
 
  
Default vlaues are specified with =  
+
==OverLoading==
  
{|  align="center" cellspacing="0" style="border:2px solid black;"
+
You can overload a method to make it more flexible.
|Rectangle
 
|-
 
| style="border-Top:2px solid black;" | height : Float = 6.0
 
|-
 
| width : Float = 4.0
 
|-
 
| style="border-Top:2px solid black;" | area()
 
|-
 
| add()
 
|-
 
| move()
 
|-
 
| copy()
 
|-
 
| isEmpty()
 
|}
 
  
 +
Simple Bark Method
  
Operator return type are also specifed with a :. Opertor parameter signatures are specified by ( '''name''' : '''Type''' )
+
<syntaxhighlight lang="csharp">
 
 
{|  align="center" cellspacing="0" style="border:2px solid black;"
 
|Rectangle
 
|-
 
| style="border-Top:2px solid black;" | height : Float = 6.0
 
|-
 
| width : Float = 4.0
 
|-
 
| style="border-Top:2px solid black;" | area() : Float
 
|-
 
| add()(r:Rectangle)
 
|-
 
| move()
 
|-
 
| copy() :Rectangle
 
|-
 
| isEmpty() : Boolean = true
 
|}
 
 
 
==UML Resources==
 
[http://www.gnome.org/projects/dia/ Dia A Drawing Program]
 
 
 
[http://dia-installer.sourceforge.net/ Dia for windows]
 
 
 
[http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class6/DiagramDog Dog Diagrams with Dia]
 
 
 
==Dog Exmaples==
 
Simple Dog Class from week 5 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogSimple.cs DogSimple.cs - source]<br />
 
[[Image:DogSimple.png]]
 
<csharp>using System;
 
 
 
//Dog simple class definition
 
 
public class Dog
 
public class Dog
 
{
 
{
 
  public string Name; // the dog's name
 
  public string Name; // the dog's name
public int Age; // the dog's age
+
public int Weight; // the dog's weight
 
 
public string BarkSound; // the sound of the dog's bark
 
public string BarkSound; // the sound of the dog's bark
 
 
Line 148: Line 64:
 
BarkSound = "Woof!!!";
 
BarkSound = "Woof!!!";
 
}
 
}
 
+
public void Bark() {  
+
public string Bark() {  
//put bark code here
+
string strBark = this.BarkSound;
 +
                barkCount ++;
 +
                return strBark;
 
}
 
}
 +
   
 
public void Eat() {
 
public void Eat() {
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}
 
+
</syntaxhighlight>
  
Dog UML with Types
+
overloaded method that makes a dog bark more than once.
  
Dog Class with Types from week 4 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogWithTypes.cs DogWithTypes.cs - source]<br /><br />
+
<syntaxhighlight lang="csharp">
[[Image:DogWithTypes.png]]
 
<csharp>using System;
 
 
//Dog simple class definition
 
 
public class Dog
 
public class Dog
 
{
 
{
 
  public string Name; // the dog's name
 
  public string Name; // the dog's name
private int Age; // the dog's age
+
public int Weight; // the dog's weight
 
 
public string BarkSound; // the sound of the dog's bark
 
public string BarkSound; // the sound of the dog's bark
private int barkCount;    //The number of time the dog has barked
+
 
public Dog()
 
public Dog()
 
{
 
{
Line 179: Line 93:
 
public string Bark() {  
 
public string Bark() {  
 
string strBark = this.BarkSound;
 
string strBark = this.BarkSound;
        barkCount ++;
+
                barkCount ++;
        return strBark;
+
                return strBark;
 
}
 
}
 
      
 
      
public void Eat() {
+
public string Bark(int numBarks) {  
//put eat code here  
+
string strBark = "";
 +
                for(int i =0; i < numBarks; i++)  //bark many times
 +
                {
 +
                  strBark += this.BarkSound;
 +
                  barkCount ++;
 +
                }
 +
                return strBark;
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
 +
You can overload a method as much as you want as long as each overload has a '''unique argument signature'''
 +
 
 +
 
 +
==Overload Constructor==
 +
 
 +
Overloading the constructor of you class can make it easier to use.
 +
 
 +
here is a simple dog contructor
 +
 
 +
<syntaxhighlight lang="csharp">
 +
public class Dog
 +
{
 +
 +
public string BarkSound; // the sound of the dog's bark
 +
 +
public Dog()
 +
{
 +
BarkSound = "Woof!!!";
 +
}
 +
 +
}
 +
</syntaxhighlight>
 +
 
 +
we can modify this so that we can pass in the sound of the dogs bark when the object is created
 +
 
 +
<syntaxhighlight lang="csharp">
 +
public class Dog
 +
{
 +
 +
public string BarkSound; // the sound of the dog's bark
 +
 +
public Dog(string newBarkSound)
 +
{
 +
this.BarkSound = newBarkSound;
 
}
 
}
}</csharp>
+
 +
}
 +
</syntaxhighlight>
  
 +
or the name of the dog and the barksound
  
  
 +
<syntaxhighlight lang="csharp">
 +
public class Dog
 +
{
 +
 +
public string BarkSound; // the sound of the dog's bark
 +
        public string Name;
 +
 +
public Dog(string newBarkSound, string NewName)
 +
{
 +
this.BarkSound = newBarkSound;
 +
                this.Name = newName;
 +
 +
}
 +
 +
}
 +
</syntaxhighlight>
  
 +
you can have as many overoads as you want as long as each overload has a unique '''argument signature'''
  
==Inheritance - 'Is A'==
+
<!--
  
[http://ia300033.us.archive.org/1/items/arsdigitac04n03/Lecture_03.html ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 3]
+
==Operator Overloading==
  
C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from.
+
You can overload operator in c# just like you overload constuctors
  
A Basenji is an African barkless dog.
+
    * Operator Overloading In C# - www.csharphelp.com
A Basenji is a dog.
+
    * Operator Overloading in C# - HowToDoThings.com
syntax
 
  
<csharp>public class Basenji : Dog
+
<syntaxhighlight lang="csharp">//Overloading unary operators
 +
public static return_type operator op (Type t)
 
{
 
{
     //override constructor
+
  // Statements
     public Basenji() : base()
+
}
 +
 
 +
//Overloading binary operators
 +
public static ClassType operator + ( object lhs, object rhs )
 +
{
 +
ClassType c
 +
//code to implement class addition
 +
return c;
 +
}</syntaxhighlight>
 +
 
 +
dog addition? extra credit
 +
 
 +
How could you overload + operator so that it returns a new dog when two dogs are added together...
 +
 
 +
 
 +
 
 +
 
 +
 
 +
===Operator Overloading Fraction Class===
 +
 
 +
<syntaxhighlight lang="csharp">public static ClassType operator + ( object lhs, object rhs )
 +
{
 +
    ClassType c
 +
     //code to implement class addition
 +
    return c;
 +
}</syntaxhighlight>
 +
 
 +
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
 +
Fraction class
 +
 
 +
    * Create fractions from two numbers x/y or from whole numbers
 +
    * Convert fractions to whole numbers int
 +
    * Convert fractions to float or double
 +
    * override ToString() to describe Fraction
 +
 
 +
 
 +
Fraction Class Example of operator overloading.<br />
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv.cs fractionConv.cs] - source<br />
 +
Fraction class with implicit and explicit conversion<br />
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv_NoTest.cs fractionConv_NoTest.cs] - source<br />
 +
Fraction class with overridden operators<br />
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConvOver.cs fractionConvOver.cs] - source<br />
 +
 
 +
In class build a test application does thigs with frations.<br />
 +
Using <br />
 +
 
 +
http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv_NoTest.cs fractionDone_NoTest.cs] create a test class that...
 +
 
 +
* Creates a fraction out of integers (ie cast int into fractions)
 +
* Adds, subtracts, and reduces some fractions
 +
* Creates floting point numbers out of fractions (is cast fraction into double)
 +
* Checks the eqaulity of some fractions
 +
 
 +
==Class Relationships==
 +
Four Class Relation ships
 +
 
 +
* Association - 'Uses A'
 +
* Containment - 'Has A'
 +
* Inheritance - 'Is A'
 +
* Interfaces - implements or derives from We'll do this next week in [[OOP Class6]]
 +
 
 +
==Association - 'Uses A'==
 +
 
 +
A dog uses a hydrant to relieve them self.
 +
 
 +
<syntaxhighlight lang="csharp">//Dog simple class definition
 +
public class Dog
 +
{
 +
    //some dog code...
 +
   
 +
     public void Relieve(Hydrant h)
 +
    {
 +
    h.Clean = false;
 +
    }
 +
}   
 +
 
 +
public class Hydrant
 +
{
 +
  public string Color; //the color of the hydrant
 +
  public bool Clean;  //if the hydrant is clean or dirty
 +
 
 +
  public Hydrant()
 +
  {
 +
    Color = "red";
 +
    Clean = true;
 +
  }
 +
 
 +
  public override string ToString ()
 +
  {
 +
    if (this.Clean == true)
 
     {
 
     {
        barkSound = "Basenjis don't bark, but they do howl and growl.";
+
    return ("The " + this.Color + " hydrant is clean.");
        dogCount++;
 
 
     }
 
     }
     public override void Bark()
+
     else
 
     {
 
     {
        Console.WriteLine("Basenjis dont bark.");
+
    return ("The " + this.Color + " hydrant is not clean.");
 
     }
 
     }
+
  }
// some more code....
+
}</syntaxhighlight>
}
+
 
 +
Dog Association example
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogHydrant.cs DogHydrant.cs] - source
 +
 
 +
==Containment - 'Has A'==
 +
A dog has an owner
 +
 
 +
 
 +
<syntaxhighlight lang="csharp">Dog fido = new Dog("fido");
 +
fido.Owner = new Person("Sue");</syntaxhighlight>
  
 +
<syntaxhighlight lang="csharp">//Dog simple class definition
 
public class Dog
 
public class Dog
{
+
{
// some dog code...
+
    public string Name;        // the dog's name
//methods need to be marked virtual or protected to be overridden
+
    public string BarkSound;    // the sound of the dog's bark
     public virtual void Bark()  
+
    public Person Owner;    // the dogs owner
     {  
+
   
         //make dog bark
+
     public Dog(string dogName)
         Console.WriteLine (this.Name + " says " + this.barkSound);
+
     {
         //add 1 to the number of times the dog has barked
+
         Name = dogName;
         this.barkCount++ ;
+
         BarkSound = "Woof!!!";
 +
    }
 +
 
 +
    public string Bark() {
 +
        string s;
 +
        s = this.Name + " says " + this.BarkSound;
 +
         //Make sure the the dog has an owner
 +
         if (!(this.Owner == null))
 +
          s += "\n" + Owner.Name + " says be quiet.";
 +
        return s;
 
     }
 
     }
}</csharp>
+
}  
  
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/Basenji.cs Basenji.cs] -source
+
public class Person
 +
  {
 +
    public string Name;      //the color of the hydrant
 +
   
 +
    public Person(string newName)
 +
    {
 +
      Name = newName;
 +
    }
 +
  }</syntaxhighlight>
  
==Virtual Functions==
 
  
A class modifier that indicates that a method can be overridden by a derived class.
+
Containment example<br>
We will look at virtual function when we look at abstract classes
+
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogOwner.cs DogOwner.cs] -source
 +
 
  
==Abstract Classes==
 
abstract - A class modifier that specifies that the class must be derived from to be instantiated.
 
  
<csharp>abstract public class Mammal
 
{
 
private string birth;
 
private string skin;
 
  
public Mammal()
+
and the this keyword
{
 
birth = " gives birth to live young.";
 
skin = " has Hair.";
 
}
 
  
public string Birth
+
<syntaxhighlight lang="csharp">//Accessor for private color allow color to be set and return color or 'dirty' + color
{
+
    public string Color
get
+
    {
{
+
        get
return birth;
+
        {
}
+
            //if current dog isClean the return dogs color
}
+
            if (this.isClean == true)
public string Skin
+
            {
{
+
                return color;
get
+
            }
{
+
            //else return 'dirty' and the dogs color
return skin;
+
            else {
}
+
                return "dirty " + color;
}
+
            }
}</csharp>
+
        }
 +
        set
 +
        {
 +
          color = value;
 +
        }
 +
    }</syntaxhighlight>
  
Inheritance example /infod/jeff/classSource/class5/DogMammal.cs -source
+
console /infod/jeff/classSource/class4/dogAccessor.cs - source
 +
web /infod/jeff/classSource/class4/dogAccessor.aspx - source
  
===Polymorphic Types===
+
Dog private members, Overloaded with method class definition
[http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 Polymorphism (computer science)]
 
Different types implementing the same functionality.<br>
 
[http://www.webopedia.com/TERM/p/polymorphism.html  polymorphism webopedia]
 
The C# Station Tutorial - [http://www.csharp-station.com/Tutorials/Lesson09.aspx Lesson 9: Polymorphism]
 
  
<csharp>abstract public class Mammal
+
<syntaxhighlight lang="csharp">//Dog private members, Overloaded  with method class definition
 +
public class Dog
 
{
 
{
private string name;
+
private string name; // the dog's name
private string birth;
+
private int age; // the dog's age
private string skin;
+
private int weight; // the dog's weight
 
+
private string barkSound; // the sound of the dog's bark
public Mammal()
+
private int barkCount; // how many times the dog has barked
 +
 +
public Dog()
 +
{
 +
barkSound = "Woof!!!";
 +
}
 +
public Dog(string newName)
 
{
 
{
name = "none";
+
name = newName;
birth = " gives birth to live young.";
+
barkSound = "Woof!!!";
skin = " has Hair.";
 
 
}
 
}
+
public Dog(string newName, string newBarkSound )
public Mammal(string newName)
 
 
{
 
{
 
name = newName;
 
name = newName;
birth = " gives birth to live young.";
+
barkSound = newBarkSound;
skin = " has Hair.";
 
 
}
 
}
  
public string Birth
+
public string Name
 
{
 
{
 
get
 
get
 
{
 
{
return birth;
+
return name;
 +
}
 +
set
 +
{
 +
name = value;
 
}
 
}
 
}
 
}
public string Skin
+
public int Age
 
{
 
{
 
get
 
get
 
{
 
{
return skin;
+
return age;
 +
}
 +
set
 +
{
 +
age = value;
 
}
 
}
 
}
 
}
public string Name
+
public int Weight
 
{
 
{
 
get
 
get
 
{
 
{
return name;
+
return weight;
 
}
 
}
 
set
 
set
 
{
 
{
name = value;
+
weight = value;
 
}
 
}
 
}
 
}
public virtual void Bark()
+
public string BarkSound
 
{
 
{
Console.WriteLine("Some Mammals Bark.");
+
get
 +
{
 +
return barkSound;
 +
}
 +
set
 +
{
 +
barkSound = value;
 +
}
 
}
 
}
}
+
public int BarkCount //you can't set bark count
 
+
//it only increments from the Bark() method
public class Dog : Mammal
 
{
 
public string BarkSound; // the sound of the dog's bark
 
 
public Dog(string newName)
 
 
{
 
{
Name = newName;
+
get
BarkSound = "Woof!!!";
+
{
 +
return barkCount;
 +
}
 +
}
 +
   
 +
    public string About()
 +
{
 +
//return a string with some information about the dog
 +
        string about = "";
 +
//this refers to current object
 +
about +=("\nThe dogs name is " + this.name + ".");
 +
about +=("\nIt is " + this.age + " years old.");
 +
about +=("\nIt weighs " + this.weight + " lb(s).");
 +
about +=("\nIts bark sounds like '" + this.barkSound + "'");
 +
about +=("\nIt has barked " + this.barkCount + " time(s)" );
 +
        about += about.Replace("\n","
 +
");
 +
        return about;
 
}
 
}
  
public override void Bark() {  
+
public void Bark() {  
base.Bark();
+
//make dog bark
Console.WriteLine(this.Name + " says " + this.BarkSound);
+
Console.WriteLine (this.Name + " says " + this.barkSound);
 +
//add 1 to the number of times the dog has barked
 +
this.barkCount++ ;
 
}
 
}
 
public void Eat() {
 
public void Eat() {
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}
+
}</syntaxhighlight>
 +
 
 +
console
 +
 
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverloadMethodScoped.cs dogOverloadMethodScoped.cs]
 +
 
 +
==In class==
 +
 
 +
Discuss Dog Diagram
  
public class Basenji : Dog
+
[[OOP Full Dog Diagram]]
{
 
//override constructor
 
public Basenji(string newName) : base(newName)
 
{
 
BarkSound = "basenjis don't bark, but they do howl and growl.";
 
}
 
 
public override void Bark()
 
{
 
base.Bark();
 
Console.WriteLine("\nBasenjis dont bark.");
 
}
 
    // some more code....
 
}</csharp>
 
  
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogPoly.cs DogPoly.cs] -source
+
-->
  
==Structs==
 
- remember these from week 2
 
  
Lightweight alternatives to classes. Structs do not support inheritance or destructors. Structs are value typed objects similar to ints bool etc... while classes are refence types. Structs are more memory efficient and and faster than classes.
 
Syntax
 
  
[ attributes] [access-modifiers] struct identifier
 
[:interface-list {struct members}
 
  
<csharp>struct Dog
 
{
 
public string name;
 
public string weight;
 
public int age;
 
}</csharp>
 
  
This is a pretty lightweight dog and is pretty useless as a dog (It's can't even bark what fun is that) so I won't make an example.
+
==Association - 'Uses A'==
  
A good example of a stuct would be and something like a point. There may be many many points in a structure or graph and we would want the points
+
A dog uses a hydrant to relieve them self.
to be a lightweight as possible. Since the point object won't have an methods this is a good time to use a struct.
 
  
<csharp>struct Point
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 +
public class Dog
 
{
 
{
     public int x;
+
    //some dog code...
     public int y;
+
   
 +
     public void Relieve(Hydrant h)
 +
    {
 +
    h.Clean = false;
 +
     }
 +
}   
  
     public Point(int x, int y)
+
public class Hydrant
 +
{
 +
  public string Color; //the color of the hydrant
 +
  public bool Clean;  //if the hydrant is clean or dirty
 +
 
 +
  public Hydrant()
 +
  {
 +
    Color = "red";
 +
     Clean = true;
 +
  }
 +
 
 +
  public override string ToString ()
 +
  {
 +
    if (this.Clean == true)
 
     {
 
     {
        this.x = x;
+
    return ("The " + this.Color + " hydrant is clean.");
        this.y = y;
 
 
     }
 
     }
 
+
     else
     public Point Add(Point pt)
 
 
     {
 
     {
        Point newPt;
+
    return ("The " + this.Color + " hydrant is not clean.");
 
 
        newPt.x = x + pt.x;
 
        newPt.y = y + pt.y;
 
 
 
        return newPt;
 
 
     }
 
     }
}</csharp>
+
  }
 +
}</syntaxhighlight>
  
==In class==
+
Dog Association example
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogHydrant.cs DogHydrant.cs] - source
  
Discuss Dog Diagram
+
==Containment - 'Has A'==
 +
A dog has an owner
  
[[OOP Full Dog Diagram]]
 
  
==Pair Programming==
+
<syntaxhighlight lang="csharp">Dog fido = new Dog("fido");
 +
fido.Owner = new Person("Sue");</syntaxhighlight>
  
[http://www.extremeprogramming.org/rules/pair.html Pair Programming]
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 +
public class Dog
 +
{
 +
    public string Name;        // the dog's name
 +
    public string BarkSound;    // the sound of the dog's bark
 +
    public Person Owner;    // the dogs owner
 +
   
 +
    public Dog(string dogName)
 +
    {
 +
        Name = dogName;
 +
        BarkSound = "Woof!!!";
 +
    }
  
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)
+
    public string Bark() {
 +
        string s;
 +
        s = this.Name + " says " + this.BarkSound;
 +
        //Make sure the the dog has an owner
 +
        if (!(this.Owner == null))
 +
          s += "\n" + Owner.Name + " says be quiet.";
 +
        return s;
 +
    }
 +
}   
  
Pairs are good because
+
public class Person
*Pairs keep each other on track
+
  {
*A partner can help when you are stuck
+
    public string Name;      //the color of the hydrant
*Keep each other accountable
+
   
 +
    public Person(string newName)
 +
    {
 +
      Name = newName;
 +
    }
 +
  }</syntaxhighlight>
  
  
Some XP Principles
+
Containment example<br>
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogOwner.cs DogOwner.cs] -source
  
Fail - If you are having trouble succeeding fail.
+
==Homework==
  
Baby Steps - Do the smallest thing that you possible can that moves you in the right direction
+
<!--
  
If you do decide to dive and conqure the problem please remember that integration process is unpredictable and can be quite difficult. Try to integrate your code often. Try posting up your code and emailing or plan on meeting several times.
+
use Pair Programming to make tests and classes from the following UML
  
When topair 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 Students Classes Diagram]]
  
I would like you and your paired partner to create and demonstate classes in c# from the diagram below.
+
If you need to ad any supprting methods or properties that are not on the diagram feel free.
  
 +
Create a UML Diagram of your c# classes (not the tv and radio yeah the pong machine and ninja and kungfumonkey and stuff)
 +
-->
  
  
==Homework==
+
<!--
 +
Chapter 2 Introduction the UML in The Unified Modeling Language Users Guided (hand out)
  
use Pair Programming to make tests and classes from the following UML
+
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
 
+
-->
[[OOP Students Classes Diagram]]
 
 
 
If you need to ad anysupprting methods or propertites that are not on the diagram feel free.
 
  
Create a UML Diagram of your c# classes (not the tv and radio yeah the pong machine and ninja and kungfumonkey and stuff)
+
*Create A Cat Containment and Association Project based off of the followong UML
 +
[[Image:CatConainmentAssociation.PNG]]
  
===READ===
+
The Program should have output similar to
Chapter 2 Introducin the UML in The Unified Modeling Language Users Guided (hand out)
+
<pre>
 +
Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
 +
w sounds like Meow!. punkin doesn't have a toy to play with
 +
Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
 +
w sounds like Meow!. punkin plays with SparkleyBall
 +
The Basement LitterBox is clean
 +
The Basement LitterBox is dirty
 +
The Basement LitterBox is clean
 +
</pre>
  
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
 
  
quiz #2 next week
+
*Create a fourth class the uses or is used by one of your other classes
 +
**Association
 +
*Create a fifth class the contains or is contained by one of your other classes
 +
**Containment
 +
**Create a UML diagram that show the relationships of your classes post export the diagram as a png (I'll show this in class) post diagram to demo fourth and fifth class

Latest revision as of 16:30, 10 June 2019


Review

Accessibility
restrict the visibility of a class and it's members
Classes
Abstraction that has properties and methods. properties and the nouns and methods are the verbsClasses are used to define objects.
Objects
Instance of a class
Abstraction 
Factor out details to work on fewer concepts at a time
Encapsulation
Allows us to use objects with out completely understanding how everything inside the object works. Encapsulation also allows the internal state of objects to be changed easily without affecting other objects or interfaces. Real barking dogs...DogBark an example of how you don't need to know how all of the classes work just how to use them
Polymorphism
many forms that have the same attributes and abilities
UML 
Unified Modeling Language.

TODO

Virtual Functions
Functions that can be overridden.
Static
Members that are associayed with a class not an instance of the class

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

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++;
	}

}

Static dougCount Example
dogStatic.cs - source



OverLoading

You can overload a method to make it more flexible.

Simple Bark Method

public class Dog
{
 	public string Name;		// the dog's name
	
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog()
	{
		BarkSound = "Woof!!!";
	}
 
	public string Bark() { 
		string strBark = this.BarkSound;
                barkCount ++;
                return strBark;
	}
    
	public void Eat() {
		//put eat code here 
	}
}

overloaded method that makes a dog bark more than once.

public class Dog
{
 	public string Name;		// the dog's name
	
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog()
	{
		BarkSound = "Woof!!!";
	}
 
	public string Bark() { 
		string strBark = this.BarkSound;
                barkCount ++;
                return strBark;
	}
    
	public string Bark(int numBarks) { 
		string strBark = "";
                for(int i =0; i < numBarks; i++)  //bark many times
                {
                   strBark += this.BarkSound;
                   barkCount ++;
                }
                return strBark;
	}
}

You can overload a method as much as you want as long as each overload has a unique argument signature


Overload Constructor

Overloading the constructor of you class can make it easier to use.

here is a simple dog contructor

public class Dog
{
 	
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog()
	{
		BarkSound = "Woof!!!";
	}
 
}

we can modify this so that we can pass in the sound of the dogs bark when the object is created

public class Dog
{
 	
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog(string newBarkSound)
	{
		this.BarkSound = newBarkSound;
	}
 
}

or the name of the dog and the barksound


public class Dog
{
 	
	public string BarkSound;	// the sound of the dog's bark
        public string Name;
	
	public Dog(string newBarkSound, string NewName)
	{
		this.BarkSound = newBarkSound;
                this.Name = newName;

	}
 
}

you can have as many overoads as you want as long as each overload has a unique argument signature




Association - 'Uses A'

A dog uses a hydrant to relieve them self.

//Dog simple class definition
public class Dog
{
    //some dog code...
    
    public void Relieve(Hydrant h)
    {
     h.Clean = false;
    }
}    

public class Hydrant
{
  public string Color; //the color of the hydrant
  public bool Clean;   //if the hydrant is clean or dirty
  
  public Hydrant()
  {
    Color = "red";
    Clean = true;
  }
  
  public override string ToString ()
  {
    if (this.Clean == true)
    {
     return ("The " + this.Color + " hydrant is clean.");
    }
    else
    {
     return ("The " + this.Color + " hydrant is not clean.");
    }
  }
}

Dog Association example DogHydrant.cs - source

Containment - 'Has A'

A dog has an owner


Dog fido = new Dog("fido");
fido.Owner = new Person("Sue");
//Dog simple class definition
public class Dog
{
    public string Name;        // the dog's name
    public string BarkSound;    // the sound of the dog's bark
    public Person Owner;    // the dogs owner
    
    public Dog(string dogName)
    {
        Name = dogName;
        BarkSound = "Woof!!!";
    }

    public string Bark() { 
        string s;
        s = this.Name + " says " + this.BarkSound;
        //Make sure the the dog has an owner
        if (!(this.Owner == null))
           s += "\n" + Owner.Name + " says be quiet.";
        return s;
    }
}    

public class Person
  {
    public string Name;      //the color of the hydrant
    
    public Person(string newName)
    {
      Name = newName;
    }
  }


Containment example
DogOwner.cs -source

Homework

  • Create A Cat Containment and Association Project based off of the followong UML

CatConainmentAssociation.PNG

The Program should have output similar to

Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
w sounds like Meow!. punkin doesn't have a toy to play with
Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
w sounds like Meow!. punkin plays with SparkleyBall
The Basement LitterBox is clean
The Basement LitterBox is dirty
The Basement LitterBox is clean


  • Create a fourth class the uses or is used by one of your other classes
    • Association
  • Create a fifth class the contains or is contained by one of your other classes
    • Containment
    • Create a UML diagram that show the relationships of your classes post export the diagram as a png (I'll show this in class) post diagram to demo fourth and fifth class