OOP Class6
Real barking dogs...
an example of how you don't need to know how all of the classes work just how to use them
Contents
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
OverLoading
You can overload a method to make it more flexible.
Simple Bark Method
<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 } } </csharp>
overloaded method that makes a dog bark more than once.
<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;
} } </csharp>
Overload Constructor
Overloading the constructor of you class can make it easier to use.
here is a simple dog contructor
<csharp> public class Dog {
public string BarkSound; // the sound of the dog's bark
public Dog() { BarkSound = "Woof!!!"; }
} </csharp>
we can modify this so that we can pass in the sound of the dogs bark when the object is created
<csharp> public class Dog {
public string BarkSound; // the sound of the dog's bark
public Dog(string newBarkSound) { this.BarkSound = newBarkSound; }
} </csharp>
or the name of the dog and the barksound
<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;
}
} </csharp>
you can overload the contructor forever as long as each oveload has a unique argument signature
Operator Overloading
You can overload operator in c# just like you overload constuctors
* Operator Overloading In C# - www.csharphelp.com * Operator Overloading in C# - HowToDoThings.com
<csharp>//Overloading unary operators public static return_type operator op (Type t) {
// Statements
}
//Overloading binary operators public static ClassType operator + ( object lhs, object rhs ) {
ClassType c
//code to implement class addition return c; }</csharp>
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
<csharp>public static ClassType operator + ( object lhs, object rhs ) {
ClassType c //code to implement class addition return c;
}</csharp>
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.
fractionConv.cs - source
Fraction class with implicit and explicit conversion
fractionConv_NoTest.cs - source
Fraction class with overridden operators
fractionConvOver.cs - source
In class build a test application does thigs with frations.
Using
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.
<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) { return ("The " + this.Color + " hydrant is clean."); } else { return ("The " + this.Color + " hydrant is not clean."); } }
}</csharp>
Dog Association example DogHydrant.cs - source
Containment - 'Has A'
A dog has an owner
<csharp>Dog fido = new Dog("fido");
fido.Owner = new Person("Sue");</csharp>
<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; } }</csharp>
Containment example
DogOwner.cs -source
and the this keyword
<csharp>//Accessor for private color allow color to be set and return color or 'dirty' + color
public string Color { get { //if current dog isClean the return dogs color if (this.isClean == true) { return color; } //else return 'dirty' and the dogs color else { return "dirty " + color; } } set { color = value; } }</csharp>
console /infod/jeff/classSource/class4/dogAccessor.cs - source web /infod/jeff/classSource/class4/dogAccessor.aspx - source
Dog private members, Overloaded with method class definition
<csharp>//Dog private members, Overloaded with method class definition public class Dog {
private string name; // the dog's name
private int age; // the dog's age private int weight; // the dog's weight private string barkSound; // the sound of the dog's bark private int barkCount; // how many times the dog has barked
public Dog() { barkSound = "Woof!!!"; } public Dog(string newName) { name = newName; barkSound = "Woof!!!"; } public Dog(string newName, string newBarkSound ) { name = newName; barkSound = newBarkSound; }
public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public int Weight { get { return weight; } set { weight = value; } } public string BarkSound { get { return barkSound; } set { barkSound = value; } } public int BarkCount //you can't set bark count //it only increments from the Bark() method { get { 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 void Bark() { //make dog bark Console.WriteLine (this.Name + " says " + this.barkSound); //add 1 to the number of times the dog has barked this.barkCount++ ; } public void Eat() { //put eat code here } }</csharp>
console
In class
Discuss Dog Diagram
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
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.
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.
Homework
use Pair Programming to make tests and classes from the following UML
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)
READ
Chapter 2 Introducin the UML in The Unified Modeling Language Users Guided (hand out)
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
quiz #2 next week