Difference between revisions of "OOP Class6"

esse quam videri
Jump to: navigation, search
 
(33 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:OOP]]
+
[[Category:IAM Classes]]
  
Real barking dogs...
+
==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.
  
 
+
TODO
[http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class4/DogBark DogBark]
+
;Virtual Functions: Functions that can be overridden.
 
+
;Static: Members that are associayed with a class not an instance of the class
an example of how you don't need to know how all of the classes work just how to use them
 
  
 
==Shared Members - static members==
 
==Shared Members - static members==
Line 13: Line 19:
 
Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type
 
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
+
<syntaxhighlight lang="csharp">public class Dog
 
{
 
{
 
//some code
 
//some code
Line 22: Line 28:
 
{
 
{
 
barkSound = "Woof!!!";
 
barkSound = "Woof!!!";
//Add a dog to the total dog count
+
//Add a dog to the total dog count
 
dogCount++;
 
dogCount++;
 
}
 
}
Line 31: Line 37:
 
//Add a dog to the total dog count
 
//Add a dog to the total dog count
 
dogCount++;
 
dogCount++;
}</csharp>
+
}</syntaxhighlight>
 +
}
  
 
Static dougCount Example <br />
 
Static dougCount Example <br />
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/dogStatic.cs dogStatic.cs] - source
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/dogStatic.cs dogStatic.cs] - source
  
 +
 +
 +
 +
 +
==OverLoading==
 +
 +
You can overload a method to make it more flexible.
 +
 +
Simple Bark Method
 +
 +
<syntaxhighlight lang="csharp">
 +
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
 +
}
 +
}
 +
</syntaxhighlight>
 +
 +
overloaded method that makes a dog bark more than once.
 +
 +
<syntaxhighlight lang="csharp">
 +
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;
 +
}
 +
}
 +
</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;
 +
}
 +
 +
}
 +
</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'''
 +
 +
<!--
  
 
==Operator Overloading==
 
==Operator Overloading==
Line 44: Line 179:
 
     * Operator Overloading in C# - HowToDoThings.com
 
     * Operator Overloading in C# - HowToDoThings.com
  
<csharp>//Overloading unary operators
+
<syntaxhighlight lang="csharp">//Overloading unary operators
 
public static return_type operator op (Type t)
 
public static return_type operator op (Type t)
 
{
 
{
Line 56: Line 191:
 
//code to implement class addition
 
//code to implement class addition
 
return c;
 
return c;
}</csharp>
+
}</syntaxhighlight>
  
 
dog addition? extra credit
 
dog addition? extra credit
Line 62: Line 197:
 
  How could you overload + operator so that it returns a new dog when two dogs are added together...
 
  How could you overload + operator so that it returns a new dog when two dogs are added together...
  
==OverLoading==
+
 
  
  
Line 68: Line 203:
 
===Operator Overloading Fraction Class===
 
===Operator Overloading Fraction Class===
  
<csharp>public static ClassType operator + ( object lhs, object rhs )
+
<syntaxhighlight lang="csharp">public static ClassType operator + ( object lhs, object rhs )
 
{
 
{
 
     ClassType c
 
     ClassType c
 
     //code to implement class addition
 
     //code to implement class addition
 
     return c;
 
     return c;
}</csharp>
+
}</syntaxhighlight>
  
 
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
 
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
Line 113: Line 248:
 
A dog uses a hydrant to relieve them self.
 
A dog uses a hydrant to relieve them self.
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 146: Line 281:
 
     }
 
     }
 
   }
 
   }
}</csharp>
+
}</syntaxhighlight>
  
 
Dog Association example
 
Dog Association example
Line 155: Line 290:
  
  
<csharp>Dog fido = new Dog("fido");
+
<syntaxhighlight lang="csharp">Dog fido = new Dog("fido");
fido.Owner = new Person("Sue");</csharp>
+
fido.Owner = new Person("Sue");</syntaxhighlight>
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 189: Line 324:
 
       Name = newName;
 
       Name = newName;
 
     }
 
     }
   }</csharp>
+
   }</syntaxhighlight>
  
  
Line 200: Line 335:
 
  and the this keyword
 
  and the this keyword
  
<csharp>//Accessor for private color allow color to be set and return color or 'dirty' + color
+
<syntaxhighlight lang="csharp">//Accessor for private color allow color to be set and return color or 'dirty' + color
 
     public string Color
 
     public string Color
 
     {
 
     {
Line 219: Line 354:
 
           color = value;
 
           color = value;
 
         }
 
         }
     }</csharp>
+
     }</syntaxhighlight>
  
 
console /infod/jeff/classSource/class4/dogAccessor.cs - source
 
console /infod/jeff/classSource/class4/dogAccessor.cs - source
Line 226: Line 361:
 
Dog private members, Overloaded with method class definition
 
Dog private members, Overloaded with method class definition
  
<csharp>//Dog private members, Overloaded  with method class definition
+
<syntaxhighlight lang="csharp">//Dog private members, Overloaded  with method class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 327: Line 462:
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
console
 
console
Line 339: Line 474:
 
[[OOP Full Dog Diagram]]
 
[[OOP Full Dog Diagram]]
  
==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
+
==Association - 'Uses A'==
*Pairs keep each other on track
 
*A partner can help when you are stuck
 
*Keep each other accountable
 
  
 +
A dog uses a hydrant to relieve them self.
  
Some XP Principles
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 +
public class Dog
 +
{
 +
    //some dog code...
 +
   
 +
    public void Relieve(Hydrant h)
 +
    {
 +
    h.Clean = false;
 +
    }
 +
}   
  
Fail - If you are having trouble succeeding fail.
+
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.");
 +
    }
 +
  }
 +
}</syntaxhighlight>
  
Baby Steps - Do the smallest thing that you possible can that moves you in the right direction
+
Dog Association example
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogHydrant.cs DogHydrant.cs] - source
  
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.
+
==Containment - 'Has A'==
 +
A dog has an owner
  
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 demonstate classes in c# from the diagram below.
+
<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 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;
 +
    }
 +
  }</syntaxhighlight>
 +
 +
 +
Containment example<br>
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogOwner.cs DogOwner.cs] -source
  
 
==Homework==
 
==Homework==
 +
 +
<!--
  
 
use Pair Programming to make tests and classes from the following UML
 
use Pair Programming to make tests and classes from the following UML
Line 374: Line 577:
  
 
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 UML Diagram of your c# classes (not the tv and radio yeah the pong machine and ninja and kungfumonkey and stuff)
 +
-->
  
===READ===
+
 
Chapter 2 Introducin the UML in The Unified Modeling Language Users Guided (hand out)
+
<!--
 +
Chapter 2 Introduction the UML in The Unified Modeling Language Users Guided (hand out)
  
 
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
 
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
 +
-->
 +
 +
*Create A Cat Containment and Association Project based off of the followong UML
 +
[[Image:CatConainmentAssociation.PNG]]
 +
 +
The Program should have output similar to
 +
<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>
 +
  
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