Difference between revisions of "OOP Class12"

esse quam videri
Jump to: navigation, search
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Line 8: Line 8:
  
 
Here is an example of sorting a list of ints or an int[]
 
Here is an example of sorting a list of ints or an int[]
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
int[] ints = new int[] { 5, 4, 2, 3, 1 };
 
int[] ints = new int[] { 5, 4, 2, 3, 1 };
 
             foreach (var number in ints)
 
             foreach (var number in ints)
Line 49: Line 49:
  
 
now with List
 
now with List
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
</csharp>
 
</csharp>
  
 
Now lets sort some dogs
 
Now lets sort some dogs
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
class Dog : IComparable
 
class Dog : IComparable
 
     {
 
     {
Line 90: Line 90:
  
 
and finally override some operators
 
and finally override some operators
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public static int Compare(Dog left, Dog right)
 
public static int Compare(Dog left, Dog right)
 
         {
 
         {
Line 184: Line 184:
 
<!--
 
<!--
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
  private void addCourseToolStripMenuItem_Click(object sender, EventArgs e)
 
  private void addCourseToolStripMenuItem_Click(object sender, EventArgs e)
 
         {
 
         {
Line 193: Line 193:
 
         }
 
         }
 
</csharp>
 
</csharp>
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
using System;
 
using System;
 
using System.Collections.Generic;
 
using System.Collections.Generic;

Revision as of 18:18, 25 January 2016


Interfaces Part2

There are many interfaces in the .net framework. Some that we have been working with are the [ICollection https://msdn.microsoft.com/en-us/library/system.collections.icollection(v=vs.110).aspx] [IList https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx] and [IEnumerable https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx]

These interfaces allow us to use methods in a List and Array.

Here is an example of sorting a list of ints or an int[] <syntaxhighlight lang="csharp" line="1" > int[] ints = new int[] { 5, 4, 2, 3, 1 };

           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           Array.Sort(ints);
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           
           ints = ints.Reverse().ToArray();
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
                       //mess it up again
           ints = new int[] { 5, 4, 2, 3, 1 };
           ints = ints.OrderBy(i => i).ToArray();
           foreach (var number in ints)
           {
               Console.Write(string.Format("{0}\t", number));
           }
           Console.WriteLine();
           //mess it up again
           ints = new int[] { 5, 4, 2, 3, 1 };
           //last way slightly uses agregate function to Order array by int, turns array into list so we can call foreach
           ints.OrderBy(i => i).ToList().ForEach(ii=>Console.Write(string.Format("{0}\t", ii)));
           Console.WriteLine();

</csharp>

now with List <syntaxhighlight lang="csharp" line="1" > </csharp>

Now lets sort some dogs <syntaxhighlight lang="csharp" line="1" > class Dog : IComparable

   {
       public string Name;
       protected int age;
       public int Age { get { return age; } set { age = value; } }
       public int Weight;
       public string BarkSound;
       public Dog()
       {
           this.Name = "fido";
           this.BarkSound = "woof!";
           this.Weight = 1;
       }
       public string About()
       {
           return string.Format("Hello my name is {0}. I'm {1} years old. I weigh {2} lbs", this.Name, this.Age, this.Weight);
       }


       public int CompareTo(object obj)
       {
           if (obj is Dog)
           {
               if (this.Age > ((Dog)obj).Age) return 1;
           }
           return 0;
       }

} </csharp>

and finally override some operators <syntaxhighlight lang="csharp" line="1" > public static int Compare(Dog left, Dog right)

       {
           if (object.ReferenceEquals(left, right))
           {
               return 0;
           }
           if (object.ReferenceEquals(left, null))
           {
               return -1;
           }
           return left.CompareTo(right);
       }
       public static bool operator <(Dog left, Dog right)
       {
           return (Compare(left, right) < 0);
       }
       public static bool operator >(Dog left, Dog right)
       {
           return (Compare(left, right) > 0);
       }

</csharp>


Strategy Pattern

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

http://www.dofactory.com/Patterns/PatternStrategy.aspx

Create Characters class and weapons class that uses the strategy pattern


http://iam.colum.edu/oop/classsource/ConsoleApplicationCharacters.zip

browser http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/ConsoleApplicationCharacters/ConsoleApplicationCharacters/ConsoleApplicationCharacters

CharactersStrategy.PNG

Use Case

http://en.wikipedia.org/wiki/Use_case

Diagrams

http://en.wikipedia.org/wiki/Use_case_diagram

Windows Course App Use Case

Actors

particiant outside of the system

Actor.png

Activity

something an actor does

Activity.png

Restaurant-UML-UC.png

Version 1 breif use case

Actor Admin

Course
#Add a course to the system
#Delete a course to the system
Student
#Add a student to the system
#Delete a student to the system
#Student add a course
#Student remove a course


CourseUseCase.png




Home work

Toastable project in moodle

Make one of your classes sortable by implementing IComparable