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

Slides:



Advertisements
Similar presentations
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Nov 2005 MSc Slide 1 - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class)
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Section 2 - Selection and Repetition. Equality and Relational Operators.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
New Features in C# 2.0 Sergey Baidachni MCT, MCSD, MCDBA.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Nov 2005 MSc Slide 1 First we look at a standard Dos based application Combining Decorator & Factory Patterns.
Nov 2005 SDP-MSc Slide 1 Section 10 Functional Programming in C#
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
ILM Proprietary and Confidential -
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Sep 2005 SDP-MSc Slide 1 Section 7 Object Oriented Programming.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Even-Driven Programming and basic Graphical User Interface.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Data Type Review Structured Data Types Abstraction Collection © John Urrutia, 2011 All Rights Reserved.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Microsoft.NET Вторая лекция. Reference and value types public class RefType { } public struct ValueType : IDisposable { public int A, B; public ValueType(int.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns.
Advanced .NET Programming I 2nd Lecture
Abstract Window ToolKit (AWT)
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
null, true, and false are also reserved.
Data Structures ADT List
Data Structures ADT List
Conditional Statements
ADT list.
Chapter 6 Array-Based Lists.
Data Structures ADT List
Presentation transcript:

Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming

Oct 2007 SDP-MSc Slide 2

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); :

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());} }

Oct 2007 SDP-MSc Slide 5

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());} }

Oct 2007 SDP-MSc Slide 7

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());} }

Oct 2007 SDP-MSc Slide 9

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());} }

Oct 2007 SDP-MSc Slide 11 Using Paint Feature

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(); }

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());} }

Oct 2007 SDP-MSc Slide 14

Oct 2007 SDP-MSc Slide 15 First a new Version of the Base class

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);} } :

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(); } :

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());} }

Oct 2007 SDP-MSc Slide 19 I.e restarted from left of screen

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();} }

Oct 2007 SDP-MSc Slide 21 I.e bounces back

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();} }

Oct 2007 SDP-MSc Slide 23 Ex91. Rewrite ‘Test91’ so text moves vertically up & down screen. Note: Repositioning of Button

Oct 2007 SDP-MSc Slide 24

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); } :

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());} }

Oct 2007 SDP-MSc Slide 27

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());}} }

Oct 2007 SDP-MSc Slide 29

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(); } :

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());} }

Oct 2007 SDP-MSc Slide 32 Ex92. Rewrite ‘Test99’ so the ball moves around the Frame

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

Oct 2007 SDP-MSc Slide 34 Ex94. Rewrite ‘Test99’ so the ball initially diagonally & bounces off each wall ….

Oct 2007 SDP-MSc Slide 35 Ex95. Rewrite ‘Ex94’ so the ball initially diagonally & bounces off each wall accelerating as it reflects off wall

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

Oct 2007 SDP-MSc Slide 37 Now complete the application (using inheritence) so it draws a triangle which moves up & down screen after button clicked

Oct 2007 SDP-MSc Slide 38

Oct 2007 SDP-MSc Slide 39 Ex97. Now rewrite the application so the triangle moves around screen

Oct 2007 SDP-MSc Slide 40 GUI Front End

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

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

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");

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); :

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

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();} }

Oct 2007 SDP-MSc Slide 47 public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame());} }

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

Oct 2007 SDP-MSc Slide 49

Oct 2007 SDP-MSc Slide 50 Ex99. Put this GUI front -end with the following Time class

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

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

Oct 2007 SDP-MSc Slide 53 Generics

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

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(); }

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

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(); }

Oct 2007 SDP-MSc Slide 58 class ArrayTest { 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

Oct 2007 SDP-MSc Slide 59 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("First=" + res); at.printAll(); }

Oct 2007 SDP-MSc Slide 60 interface First { int Add(); } class Second where T : First { T val1,val2; public Second(T v1, T v2) { val1 = v1; val2 = v2; } public int sum() { return val1.Add()+val2.Add(); }}

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 sec = new Second (mt1, mt2); Console.WriteLine("Sum=" + sec.sum()); }}

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 sec = new Second (mt1, mt2); Console.WriteLine("Sum=" + sec.sum()); }}

Oct 2007 SDP-MSc Slide 63 class ArrayTest 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; } :

Oct 2007 SDP-MSc Slide 64 class GenericsEx { public static void Main() { double[] a = { 2.25, 6.5, 3.25, 5.5 }; ArrayTest at = new ArrayTest (a); double res = at.largest(); Console.WriteLine("Largest=" + res); at.printAll(); }

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: :

Oct 2007 SDP-MSc Slide 66 IEnumerable Interface 1 method called GetEnumerator. "Exposes the enumerator support the ForEach semantics

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 type Implied call to b.GetEnumator()

Oct 2007 SDP-MSc Slide 68 Yield Only used in an Iterator Block // yield-example.cs using System; using System.Collections; public class List { ;

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

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

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

Oct 2007 SDP-MSc Slide 72 class MyClass : IEnumerable { IEnumerator Letter { get { yield return "A"; yield return "B"; yield return "C"; } } public IEnumerator GetEnumerator() { return Letter; } }

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 type Implied call to getEnumerator - Foreach works on IEnumerator type

Oct 2007 SDP-MSc Slide 74 Generics: public class ArrayList { private T[] itemArray; public void Add(T newItem) {...} public T this[int i] {...} }

Oct 2007 SDP-MSc Slide 75 ArrayList strArrList = new ArrayList (); strArrList.Add(“fred”); string str = strArrList[0];

Oct 2007 SDP-MSc Slide 76 public class ObjectList : 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; } } }

Oct 2007 SDP-MSc Slide 77 ObjectList list = new ObjectList (); // Add two strings. list.Add("blue"); list.Add("green"); :

Oct 2007 SDP-MSc Slide 78 public class GenericList { 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

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

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 GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } }}

Oct 2007 SDP-MSc Slide 81 class TestGenericList { static void Main() { GenericList list = new GenericList (); for (int x = 0; x < 10; x++) { list.AddHead((char)(x+65)); } foreach (char i in list) { System.Console.Write(i + " "); }