Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

81
Oct 2007 1 Section 9 Graphic Programming

Transcript of Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Page 1: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

1

Section 9

Graphic Programming

Page 2: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

2

Page 3: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

3

using System;using System.Windows.Forms;using System.Drawing;

class GFrame1:Form { protected int x=10, y=20; protected Label l1; protected Font f=new Font("Times New Roman",20,FontStyle.Bold); :

Page 4: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

4

:public GFrame1(){

l1=new Label(); l1.Font=f; l1.Text="Welcome"; l1.SetBounds(x,y,160,43); Controls.Add(l1);

}}

public class Test91{public static void Main(string[] args){

Application.Run(new GFrame1());}}

Page 5: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

5

Page 6: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

class GFrame2 :GFrame1 { protected Label l2=new Label();

public GFrame2():base(){ l2.Text="Home";

l2.SetBounds(x,y+40,160,43); l2.Font = f; Controls.Add(l2);

}}

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame2());}}}

Page 7: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

7

Page 8: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

class GFrame2 :GFrame1 { protected Button b1=new Button();

public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23);

Controls.Add(b1); }

}

public class Test93{public static void Main(string[] args){

Application.Run(new GFrame2());}}

Page 9: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

9

Page 10: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

10

class GFrame2 :GFrame1 { protected Button b1=new Button();

public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click);

Controls.Add(b1);}

private void button1_Click(object sender, EventArgs e){ l1.Text="Goodbye";}}

public class Test94{public static void Main(string[] args){

Application.Run(new GFrame2());}}

Page 11: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

11

Using Paint Feature

Page 12: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

12

class GFrame2 :GFrame1 { protected Button b1=new Button();

protected bool start = false; public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click);

Controls.Add(b1); }

private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); }

Page 13: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

13

protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true) g.DrawString("Everyone",f,brush,30,60); }

}

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame2());}}

Page 14: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

14

Page 15: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

15

First a new Version of the Base class

Page 16: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

using System;using System.Windows.Forms;using System.Drawing;using System.Threading;

class GFrame1:Form { protected int x=10, y=20; protected Font f=

new Font("Times New Roman",20,FontStyle.Bold); public GFrame1(){ }

protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); g.DrawString("Welcome",f,brush,x,y);}} :

Page 17: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

class GFrame2 :GFrame1 { protected Button b1=new Button();

protected bool start = false; public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click);

Controls.Add(b1); }

private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); }

:

Page 18: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){ x+=15;

Thread.Sleep(1000);this.Refresh();}

}}

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame2());}}

Page 19: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

19

I.e restarted from left of screen

Page 20: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

20

class GFrame2 :GFrame1 { : protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){ x+=15;

Thread.Sleep(1000);

if (x>280)x=10; // reset this.Refresh();}

}}

}

Page 21: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

21

I.e bounces back

Page 22: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

class GFrame2 :GFrame1 { :

private int step=15; : protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){

x+=step;Thread.Sleep(1000);

if ((x>180)||x<10)step=step*-1;this.Refresh();}

}}

Page 23: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

23

Ex91.Rewrite ‘Test91’ so textmoves vertically up &down screen.

Note: Repositioning of Button

Page 24: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

24

Page 25: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

25

using System;using System.Windows.Forms;using System.Drawing;using System.Threading;

class GFrame3:Form { protected int x=10, y=10; protected SolidBrush brush;

public GFrame3(){ brush =new SolidBrush(Color.Blue); }

:

Page 26: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

26

protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics;

g.FillEllipse(brush,x,y,50,50); }

}

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame3());}}

Page 27: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

27

Page 28: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

28

class GFrame4 :GFrame3 { protected Button b1=new Button();

public GFrame4():base(){

b1.Text= "Change Colour"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click);

Controls.Add(b1); }

private void button1_Click(object sender, EventArgs e){ brush.Color=Color.Yellow; this.Refresh();}

} public class Test92{

public static void Main(string[] args){Application.Run(new GFrame4());}}

}

Page 29: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

29

Page 30: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

30

class GFrame4 :GFrame3 { protected Button b1=new Button(); private bool start = false; private int step=8;

public GFrame4():base(){ b1.Text= "Change Colour"; b1.SetBounds(180,100,90,23); b1.Click += new EventHandler(this.button1_Click);

Controls.Add(b1); }

private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); }

:

Page 31: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

31

: protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); if (start==true){ y+=step;

Thread.Sleep(500); if ((y>219)||y<10)step=step*-1; this.Refresh();}

} }

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame4());}}

Page 32: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

32

Ex92. Rewrite ‘Test99’ so the ball moves around the Frame

Page 33: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

33

Ex93. Rewrite ‘Test99’ so the ball moves vertically up & down the Frame but not go as high next time & eventually stops

Page 34: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

34

Ex94. Rewrite ‘Test99’ so the ball initially diagonally & bounces off each wall ….

Page 35: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

35

Ex95. Rewrite ‘Ex94’ so the ball initially diagonally & bounces off each wall accelerating as it reflects off wall

Page 36: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

36

class GFrame3 extends JFrame { protected Container content; protected int x=10, y=30; protected Color c=Color.blue;

public GFrame3(){ content=getContentPane(); content.setLayout(null); setSize(300,300); setVisible(true);}

public void paint(Graphics g){ super.paint(g); g.drawLine(x, y+40, x+30,y);}

}public class Test910{

public static void main(String[] args){ GFrame3 e = new GFrame3();}}

Ex96. This Application produces the following result

Page 37: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

37

Now complete the application (using inheritence) so it draws atriangle which moves up & down screen after button clicked

Page 38: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

38

Page 39: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

39

Ex97. Now rewrite the application so the triangle moves around screen

Page 40: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

40

GUI Front End

Page 41: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

41

using System;using System.Windows.Forms;using System.Drawing;using System.Threading;

class Counter{ protected int value; private String name;

public Counter(int v, String n){ this.value = v; this.name = n;}

public void increment(){this.value++;}

public int read_value(){ return this.value;}}

Page 42: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

42

class MyCounter:Counter{

public MyCounter(int v, String n):base(v,n) { }

public void decrement() {this.value--;}

public void add(int amt){ this.value+=amt;}}

Page 43: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

43

class GFrame:Form {

private Label lmain=new Label();private Label l1=new Label();private Button b_incr=new Button();private Button b_decr=new Button();private Button b_add=new Button();private TextBox t1=new TextBox();

private TextBox t2=new TextBox(); private Font f=new Font("Times New Roman",15,FontStyle.Bold);

private MyCounter c =new MyCounter(1,"Score");

Page 44: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

44

: public GFrame():base(){

lmain.Text="Counter Application"; lmain.Font=f; l1.Text="Value"; l1.Font=f; b_incr.Text= "Incr"; b_decr.Text= "Decr"; b_add.Text= "Add Amt"; t1.Text= "1"; lmain.SetBounds(10,10,220,43);

l1.SetBounds(22,60,70,23); t1.SetBounds(102,60,90,23); t1.Text= " "+c.read_value(); b_incr.SetBounds(12,90,90,23);

b_decr.SetBounds(102,90,90,23); b_add.SetBounds(12,120,90,23); t2.SetBounds(102,120,90,23);

:

Page 45: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

45

: b_incr.Click += new EventHandler(this.button1_Click); b_add.Click += new EventHandler(this.button3_Click);

Controls.Add(lmain); Controls.Add(l1);

Controls.Add(t1); Controls.Add(b_incr); Controls.Add(b_decr); Controls.Add(b_add); Controls.Add(t2);

}

Page 46: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

46

private void button1_Click(object sender, EventArgs e){ c.increment();

t1.Text=" "+c.read_value();}

private void button3_Click(object sender, EventArgs e){ int amt=int.Parse(t2.Text);

c.add(amt); t1.Text=" "+c.read_value();}

}

Page 47: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

47

public class Test92{public static void Main(string[] args){

Application.Run(new GFrame());}}

Page 48: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

48

Ex98. Put a GUI Front End on the following Account class

class Account{ private int accNo; private int balance; public Account(int No, int bal){ this.balance=bal;

this.accNo=No;}public int getAccNo(){return accNo;}public int getBalance() {return balance;}public void credit(int amount){ balance+=amount;}public boolean debit(int amount){ if (amount > balance) return false;

else { balance = balance - amount; return true;}}

}

Page 49: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

49

Page 50: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

50

Ex99. Put this GUI front -end with the following Time class

Page 51: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

51

class Time{ private int sec; private int min;

private int hour; public Time(int h,int m, int s){ this.hour = h;

this.min = m; this.sec = s;}

public void increment_sec(){this.sec++; if (sec==60){ sec=0; min++;}

if (min==60){min=0; hour++;} } public void decrement_sec(){ if (sec>0) sec--;

else { sec=59; if(min>0) min--; else {min=59; hour--;}}}

public String read_time(){ return ""+hour+":"+min+":"+sec;}

Page 52: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

52

Ex910. Change Q72 from DOS menu to GUI

class Item{private int lotno;private String bidder;private int bid;

public Item(int l,String bn, int b){...public void nextbid(String b_name, int amt){...public void print_details(){ }}

public class Q72{public static void main(...

Note: next bid only accepted if > current bid

Online Auction

Page 53: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

53

Generics

Page 54: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

54

class ArrayTest

{ private int[] values;

public ArrayTest(int[] v)

{

values = v;}

public int first()

{

if (values.Length == 0) throw new Exception();

return values[0];}

public void printAll()

{

Console.WriteLine(); Console.Write("[");

foreach (int el in values)

Console.Write(" " + el);

Console.WriteLine("]");

}}

Version 1

Page 55: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

55

class GenericsEx

{

public static void Main()

{

int[] a = { 2, 6, 3, 5 };

ArrayTest at=new ArrayTest(a);

int res = at.first();

Console.WriteLine(“First=" + res);

at.printAll();

}

}

Page 56: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

56

class ArrayTest

{ private double[] values;

public ArrayTest(double[] v)

{

values = v;}

public double first()

{

if (values.Length == 0) throw new Exception();

return values[0];}

public void printAll()

{

Console.WriteLine(); Console.Write("[");

foreach (double el in values)

Console.Write(" " + el);

Console.WriteLine("]");

}}

Version 2

Page 57: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

57

class GenericsEx

{

public static void Main()

{

double[] a = { 2.25, 6.5, 3.25, 5.5 };

ArrayTest at=new ArrayTest(a);

double res = at.first();

Console.WriteLine("Sum=" + res);

at.printAll();

}

}

Page 58: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

58

class ArrayTest<T>

{ private T [] values;

public ArrayTest(T[] v)

{

values = v;}

public T first()

{

if (values.Length == 0) throw new Exception();

return values[0];}

public void printAll()

{ Console.WriteLine(); Console.Write("[");

foreach (T el in values)

Console.Write(" " + el);

Console.WriteLine("]");

}}

Version 3

Page 59: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

59

class GenericsEx

{

public static void Main()

{

double[] a = { 2.25, 6.5, 3.25, 5.5 };

ArrayTest<double> at=new ArrayTest<double>(a);

double res = at.first();

Console.WriteLine("First=" + res);

at.printAll();

}

}

Page 60: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

60

interface First

{

int Add();

}

class Second<T> where T : First

{

T val1,val2;

public Second(T v1, T v2)

{

val1 = v1; val2 = v2;

}

public int sum()

{

return val1.Add()+val2.Add();

}}

Page 61: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

61

class MyT : First

{ private int v1,v2;

public MyT(int a, int b){v1=a; v2=b;}

public int Add()

{

return v1 + v2;

}

}

public class Test

{public static void Main()

{ MyT mt1 = new MyT(1, 2);

MyT mt2 = new MyT(3, 4);

Second<MyT> sec = new Second<MyT>(mt1, mt2);

Console.WriteLine("Sum=" + sec.sum());

}}

Page 62: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

62

//Error

public class Test

{public static void Main()

{ MyT mt1 = new MyT(1, 2);

MyT mt2 = new MyT(3, 4);

Second<int> sec = new Second<int>(mt1, mt2);

Console.WriteLine("Sum=" + sec.sum());

}}

Page 63: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

63

class ArrayTest<T> where T:IComparable

{

private T[] values;

public ArrayTest(T[] v)

{

values = v;}

public T largest()

{

if (values.Length == 0) throw new Exception();

T res = values[0];

foreach (T el in values)

if (res.CompareTo(el)<0) res=el;

return res;

}

:

Page 64: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

64

class GenericsEx

{

public static void Main()

{

double[] a = { 2.25, 6.5, 3.25, 5.5 };

ArrayTest<double> at = new ArrayTest<double>(a);

double res = at.largest();

Console.WriteLine("Largest=" + res);

at.printAll();

}

}

Page 65: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

65

IEnumerator Interface

• has following members: • Property: Current • Method: MoveNext • Method: Reset

• "Supports a simple iteration over a collection.", as well as: :

Page 66: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

66

IEnumerable Interface

• 1 method called

• GetEnumerator.

• "Exposes the enumerator

• support the ForEach semantics

Page 67: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

67

foreach loop Iterates through a composition

i.e IEnumerator type

public class Array3{

static int add(int []b)

{ int total=0;

foreach (int element in b)

total = total+ element;

return total;}

:

b-Must be an IEnumerable typeImplied call to b.GetEnumator()

Page 68: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

68

Yield

Only used in an Iterator Block

// yield-example.cs

using System;

using System.Collections;

public class List {

;

Page 69: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

69

public static IEnumerable Power(int number, int exponent)

{ int counter = 0;

int result = 1;

while (counter++ < exponent)

{ result = result * number;

yield return result; } }

Page 70: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

70

static void Main()

{ // Display powers of 2 up to the exponent 8:

foreach (int i in Power(2, 8))

{ Console.Write("{0} ", i); } } }

2 4 8 16 32 64 128 256

Page 71: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

71

IEnumerator Interface

If you want to Iterate through a collection in C#, the collection must Implement this interface

Supports functions like:

- Void Reset() , - bool MoveNext() ,- object Current

Page 72: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

72

class MyClass : IEnumerable<string>{   IEnumerator<string> Letter   {      get      {         yield return "A";         yield return "B";         yield return "C";      }   }

   public IEnumerator<string> GetEnumerator()   {      return Letter;   }

}

Page 73: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

73

class MainClass{   static void Main()   {      MyClass mc1 = new MyClass();      foreach (string s in mc1)         Console.Write("{0} ", s);   }}

Mc – IEnumerable typeImplied call to getEnumerator- Foreach works on IEnumerator type

Page 74: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

74

Generics:

public class ArrayList<T>{

     private T[] itemArray;         public void Add(T newItem)     {...}         public T this[int i]     {...}

}

Page 75: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

75

ArrayList<string> strArrList = new ArrayList<string>();

strArrList.Add(“fred”);

string str = strArrList[0];

Page 76: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

76

public class ObjectList<ItemType> : CollectionBase { private ArrayList list = new ArrayList();

public int Add(ItemType value) { return list.Add(value); }

public void Remove(ItemType value) { list.Remove(value); }

public ItemType this[int index] { get { return (ItemType)list[index]; } set { list[index] = value; } } }

Page 77: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

77

ObjectList<string> list = new ObjectList<string>();

// Add two strings.

list.Add("blue");

list.Add("green");

:

Page 78: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

78

public class GenericList<T>{private class Node { public Node(T t) { next = null; data = t; } private Node next; public Node Next { get { return next; } set { next = value; }} private T data; public T Data { get { return data; } set { data = value; } } }

Inner Class Node

Page 79: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

79

private Node head;

// constructor public GenericList() { head = null; }

// T as method parameter type: public void AddHead(T t) { Node n = new Node(t); n.Next = head; head = n; }

Page 80: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

80

public T RemoveFirst() { if (head == null) throw new Exception(); else { T res = head.Data; head = head.Next; return res; } } public IEnumerator<T> GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } }}

Page 81: Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Oct 2007 SDP-MSc Slide

81

class TestGenericList{ static void Main() { GenericList<char> list = new GenericList<char>();

for (int x = 0; x < 10; x++) { list.AddHead((char)(x+65)); } foreach (char i in list) { System.Console.Write(i + " "); } }}