De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie

Verwante presentaties


Presentatie over: "Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie"— Transcript van de presentatie:

1 Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie
klasse naam methode naam ( expressie ) ; . , object expressie property naam += variabele = expressie ; return expressie ; if ) ( expressie opdracht else opdracht blok foreach ( type naam in expr ) opdracht

2 Punten- klikker met bitmap } class MijnView : View { Bitmap b;
List<PointF> alles = new List<PointF>( ) ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } foreach( PointF p in alles) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; PointF p; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); alles.Add(p); this.Invalidate(); }

3 Tekst Analyse 26 9 5 1 14 6 8 public class DiagramView: View {
public string Invoer; protected override void OnDraw(Canvas cv) { base.OnDraw(cv); tellers 9 5 1 14 2 3 4 26 Length 6 8 7 int [ ] tellers; tellers = new int [26]; foreach(char c in Invoer) if (c>='a' && c<='z') tellers[ ]++; c-'a' int w = Width / max; int h = Height/ 26; , y=0; foreach(int a in tellers) { cv.DrawText( a.ToString()...); cv.DrawRect( x, y, x+a*w, y+h, verf ); y = y+h; } } }

4 Waarom lokale variabelen?
Als tussenresultaat in berekeningen private void tekenHuis (Canvas c, … ) { Paint verf = new Paint(); int x, int y, int br) (tx,ty) int tx = x+br/2; int ty = y–br–br/2; c.DrawRect ( … , verf); c.DrawLine ( … , verf); c.DrawLine ( … , verf); x, y-br, x+br, y x, y-br, tx, ty tx, ty, x+br, y-br } (x,y) br twee keer nodig twee keer nodig twee keer nodig drie keer nodig twee keer nodig

5 Waarom lokale variabelen?
Als tussenresultaat in berekeningen Color c = new Color(64,0,0); verf.Color = c; een keer nodig dan kan het ook zonder variabele verf.Color = new Color(64,0,0); KaartView kaart; kaart = new KaartView(this); SetContentView(kaart); SetContentView( new KaartView(this) );

6 Waarom lokale variabelen?
Als tussenresultaat in berekeningen Intent i = new Intent( this, typeof(Hallo) ); StartActivity( i ); StartActivity( new Intent( this, typeof(Hallo) ) ); KaartView kaart; kaart = new KaartView(this); SetContentView(kaart); SetContentView( new KaartView(this) );

7 Waarom lokale variabelen?
Als tussenresultaat in berekeningen Intent i = new Intent( this, typeof(Hallo) ); i.PutExtra( "wat", "hoi" ); StartActivity( i ); twee keer nodig? twee keer nodig? PutExtra is een void -methode in klasse Intent Intent StartActivity( new Intent( this, typeof(Hallo) ) . PutExtra( "wat", "hoi" ) ); StartActivity( new Intent( this, typeof(Hallo) ) . PutExtra( "wat", "hoi" ) );

8 Waarom methodes? private void tekenHuis(…) { … . DrawRect (…); … . DrawLine (…); … . DrawLine (…); } een keer definiëren protected override void OnDraw(Canvas c) { … . tekenHuis (…); … . tekenHuis (…); … . tekenHuis (…); } drie keer gebruiken drie keer gebruiken drie keer gebruiken

9 Waarom methodes? private void einde( object o, EventArgs ea) { this.Finish( ); } een keer definiëren protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; } een keer gebruiken

10 Waarom methodes? private void einde( object o, EventArgs ea) { this.Finish( ); } een keer definiëren protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; (object o, EventArgs ea) => { this.Finish( ) ; } ;

11 type kan automatisch bepaald worden
Waarom methodes? private void einde( object o, EventArgs ea) { this.Finish( ); } protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; type kan automatisch bepaald worden (object o, EventArgs ea) => { this.Finish( ) ; } ; body met één opdracht mag zonder accolades

12 Waarom methodes? protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; (o,ea) => this.Finish( ) ;

13 Waarom methodes? protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; (o,ea) => StartActivity( new Intent( this, typeof(Hallo) ) . PutExtra( "wat", "hoi" ) ) ;

14 PutExtra / GetStringExtra
class Hallo { protected override void OnCreate(Bundle b) { string tekst = Intent.GetStringExtra( "wat" ) ; if (tekst==null) tekst = "hallo" ; ?? protected override void OnCreate(Bundle b) { Button b = new Button( ); b.Click += einde; (o,ea) => StartActivity( new Intent( this, typeof(Hallo) ) . PutExtra( "wat", "hoi" ) ) ;

15 Hoofdstuk ListView

16 ListView public class KleurenApp : Activity { ListView kleurLijst;
ArrayAdapter<string> kleurAdp; string[] kleuren = {"rood", "wit", "blauw"}; protected override void OnCreate(Bundle b) { base.OnCreate(b); kleurLijst = new ListView(this); kleurAdp = new ArrayAdapter<string> (this, SimpleListItemChecked, kleuren); kleurLijst.Adapter = kleurAdp; kleurLijst.ChoiceMode = Multiple; kleurLijst.ItemClick += iklik; SetContentView(kleurLijst); } }

17 ListView public class KleurenApp : Activity { ListView kleurLijst;
KleurenAdapter kleurAdp; ArrayAdapter<string> kleurAdp; string[] kleuren = {"rood", "wit", "blauw"}; KleurItem[] kleuren = { new KleurItem("Red", Color.Red), new Kle List<KleurItem> kleurItems = new List<KleurItem>(kleuren); protected override void OnCreate(Bundle b) { base.OnCreate(b); kleurLijst = new ListView(this); kleurAdp = new KleurenAdapter (this, kleurItems); kleurAdp = new ArrayAdapter<string> (this, SimpleListItemChecked, kleuren); kleurLijst.Adapter = kleurAdp; kleurLijst.ChoiceMode = Multiple; kleurLijst.ItemClick += iklik; SetContentView(kleurLijst); } }

18 KleurItem public class KleurItem { public string Naam;
public Color Kleur; public KleurItem(string n, Color k) { Naam = n; Kleur = k; } }

19 Adapter public class KleurenAdapter : BaseAdapter<KleurItem> {
Activity activity; List<KleurItem> items; public KleurenAdapter (Activity a, List<KleurItem> ki) { this.activity = a; this.items = ki; } public override View GetView( int pos ) { , View oud TextView view ; = (TextView) oud; if (view==null) view = new TextView(activity); view.Text = items[pos].Naam; view.SetBackgroundColor(items[pos].Kleur); view.SetHeight(100); return view; } }

20 ListView public class KleurenApp : Activity {
public void klik(object o, EventArgs e) { Intent i = new Intent(this, typeof(KleurEdit)); StartActivityForResult( i, 9999 ); } public void iklik(object o, ItemClickEventArgs e) { Intent i = new Intent(this, typeof(KleurEdit)); int pos = e.Position; KleurItem item = kleurItems[pos]; i.PutExtra("naam", item.Naam ); i.PutExtra("kleur", item.Kleur.ToArgb() ); StartActivityForResult( i, pos ); } override void OnActivityResult(int pos, ... ) { ... } }

21 KleurEdit public class KleurEdit : Activity {
string naam; Color kleur; static int nummer = 1; EditText e; protected override void OnCreate(Bundle b) { base.OnCreate(b); zoals in H.3, plus naam = this.Intent.GetStringExtra("naam"); kleur = new Color( this.Intent.GetIntExtra("kleur",0) ; ) if (naam==null) naam = $"nieuw {nummer++}"; e = new EditText(this); e.Text = naam; } private void ok (object o, EventArgs e) { Intent i = new Intent( ); i.PutExtra("naam", e.Text ); i.PutExtra("kleur", kleur.ToArgb() ); SetResult(Result.Ok, i); Finish(); } }

22 ListView public class KleurenApp : Activity {
public void klik(object o, EventArgs e) { } public void iklik(object o, ItemClickEventArgs e) { } override void OnActivityResult (int pos, Result res , Intent data ) { if (res==Result.Ok) { naam = data.GetStringExtra("naam"); ... KleurItem k = new KleurItem(naam,kleur); if (pos==9999) kleurItems.Add( k ); else kleurItems[pos] = k; } else if (pos < 9999) kleurItems.RemoveAt( pos ); } }

23 Samenvatting

24 Schilderij class MijnAct : Activity { MijnView scherm;
protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); scherm.Click += klik; this.SetContentView(scherm); } } class MijnView : View { public MijnView(Context c) : base(c) { } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawLine(...); }

25 Cirkel- klikker } class MijnView : View { PointF p;
public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawCircle(p.X, p.Y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }

26 Punten- klikker met bitmap } class MijnView : View { PointF p;
Bitmap b; List<PointF> alles ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }

27 Punten- klikker met bitmap } class MijnView : View { Bitmap b;
List<PointF> alles ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; PointF p; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); alles.Add(p); this.Invalidate(); }

28 Punten- klikker met bitmap } class MijnView : View { Bitmap b;
List<PointF> alles ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } foreach( PointF p in alles) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; PointF p; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); alles.Add(p); this.Invalidate(); }

29 Punten- klikker met bitmap } class MijnView : View { Bitmap b;
List<PointF> alles = new List<PointF>( ) ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } foreach( PointF p in alles) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; PointF p; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); alles.Add(p); this.Invalidate(); }

30 Kompas class KompasView : View , ISensorListener { Bitmap b;
float Schaal; float Hoek; public KompasView(Context c) : base(c) { b = BitmapFactory.DecodeResource(...UU...); } SensorManager sm = c . GetSystemService(...); sm.RegisterListener(this, ...Orientation...); protected override void OnDraw(Canvas canv) { base.OnDraw(canv); } Schaal = this.Width / b.Width; Matrix mat = new Matrix(); mat.PostScale(Schaal, Schaal); mat.PostTranslate(-b.Width/2, -b.Height/2); mat.PostRotate(- Hoek); mat.PostTranslate(Width/2, Height/2); canv.DrawBitmap(b, mat, verf); public void OnSensorChanged(SensorEvent s) { } Hoek = s.Values[0]; this.Invalidate( ); }

31 Interface In de library In je eigen programma wat is er nodig om een
ISensorListener te zijn? In de library In je eigen programma deze methode! interface ISensorListener { } public void OnSensorChanged(SensorEvent s); ik beloof een ISensorListener te zijn class KompasView : View { , ISensorListener public KompasView(Context c) : base(c) { } en implementeer dus de methode! public OnDraw(Canvas c) { } public void OnSensorChanged(SensorEvent s) { } }

32 Machtsverhef-methode
private static double macht (double x, int n) { double result; int t; t = 0; result = 1; while ( t<n ) { t = t+1 ; } result = result * x ; return result; }

33 Herhaling met een teller
int t ; t = 0; t = 0; for while ( t<x ) for ( t=0 ; t<x ; t++ ) { // doe iets nuttigs // met t t ++ ; t ++ ; }

34 Machtsverhef-methode
private static double macht (double x, int n) { double result; int t; result = 1; for(t=0; t<n ; t++) result = result * x ; return result; }

35 RGB-Kubus 3x RadioButton SeekBar KubusView 5x RadioButton

36 Kubus class KubusView: View { public int Pos=0, Dim=2;
public KubusView(Context c) : base(c) { } protected override void OnDraw(Canvas canv) { base.OnDraw(canv); Paint verf = new Paint(); int a = 16; float v = 255/(a-1); int d = Math.Min(Width, Height) / a; for (int x=0; x<a; x++) { for (int y=0; y<a; y++) { if (Dim==2) verf.Color = new Color( v*x, v*y, Pos); canv.DrawRect ( d*x, d*y, d*(x+1), d*(y+1), verf); } } } }

37 Voorbeeld: Rente- berekening
door de gebruiker in te vullen EditText -objecten Button met een Click handler TextView met het resultaat

38 Rente-programma private void bereken(object o, EventArgs ea) {
double bedrag = double.Parse( bedragBox.Text ) ; double rente = double.Parse( renteBox.Text ); resultaat.Text = ""; for (int jaar=0; jaar<=10; jaar++) { resultaat.Text += $"na {jaar} jaar {bedrag}\n"; bedrag *= (1+0.01*rente); } }

39 opdracht blok return expressie ; if ( expressie ) opdracht else opdracht while ( expressie ) opdracht for ( expr ; expr ; expr ) opdracht , , declaratie foreach ( type naam in expr ) opdracht var try blok catch ( type naam ) blok

40 Onbedoelde oneindigheid
x=1; aantal = 0; while (aantal<10) x = x*2; aantal = aantal+1; accolades vergeten... { }

41 Onbedoelde oneindigheid
Puntkomma teveel… for (x=0; totaal<100; x++) ; totaal = totaal + x ; ;

42 Values versus Pointers
Color c; struct Color c = new Color(255,0,0); Rect r; class Rect r = new Rect(10,20,30,40); r null c R 255 Left 10 G Top 30 B Right 20 Bottom 40

43 Eigen klasse Kleur class Kleur { public byte Rood, Groen, Blauw; }
constructor-methode public Kleur( ) { } Rood=255; Groen=255; Blauw=255; constructor-methode met parameters public Kleur( byte r, byte g, byte b ) { } Rood=r; Groen=g; Blauw=b; nog een constructor-methode public Kleur( byte x) { } Rood=x; Groen=x; Blauw=x; methode die iets uitrekent public byte Grijswaarde( ) { } return (3*Rood + 6*Groen + Blauw) / 10; }

44 Methodes in de eigen klasse Kleur
elke klasse is automatisch subklasse van object class Kleur : object { public byte Rood, Groen, Blauw; methode die iets uitrekent public byte Grijswaarde( ) { } return (3*Rood + 6*Groen + Blauw) / 10; (byte) (0.3*Rood + 0.6*Groen + 0.1*Blauw); public void MaakDonkerder( ) { } void-methode die het object verandert Rood = (byte) (Rood * 0.9) ; Groen = (byte) (Groen * 0.9) ; Blauw = (byte) (Blauw * 0.9) ; Rood = Rood * 0.9; Groen = Groen * 0.9; Blauw = Blauw * 0.9; de variabelen van methode die een leesbare versie van de Kleur maakt override public string ToString( ) { } return $"{Rood} {Groen} {Blauw}"; }

45 Meer constructormethodes in klasse Kleur
class Kleur { public byte Rood, Groen, Blauw; constructor-methode met parameters public Kleur( byte r, byte g, byte b ) { } Rood=r; Groen=g; Blauw=b; constructor-methode die een kopie maakt public Kleur( Kleur orig ) { } Rood=orig.Rood; Groen=orig.Groen; Blauw=orig.Blauw; constructor-methode die een string ontleedt public Kleur( string s ) { } string[ ] woorden = s.Split( ); array van strings handige methode van string Rood = byte.Parse( woorden[0] ); Groen = byte.Parse( woorden[1] ); Blauw = byte.Parse( woorden[2] ); Rood = woorden[0]; Groen = woorden[1]; Blauw = woorden[2]; }

46 Intent: Hallo opstarten
public class Multi : Activity { public class Hallo : Activity { Button b1, b2, b3, b4, b5, b6; override void OnCreate(Bundle b) { base.OnCreate(b); public void klik1 (object o, EventArgs e) { TextView t = new TextView(this); Intent i; string s = Intent.GetStringExtra("wat"); i = new Intent (this, typeof(Hallo) ); t.Text = s; i.PutExtra("wat", "Hallo!!!"); SetContentView(t); } StartActivity(i); } } }

47 string versus char string char klasse primitief type
object-verwijzing directe waarde nul, een of meer… precies één symbool "" "A" "hello" 'A' operatoren, methoden properties, indexer operatoren == , == , + , <= Substring, ToUpper, Split Length s[t]

48 Tel symbool-frequentie
public static int Freq(char x, string s) { int aantal; aantal = 0; for (int t=0; t<s.Length; t++) if ( ) aantal++; s[t]==x return aantal; }

49 Tel symbool-frequentie
public static int Freq(char x, string s) { int aantal; aantal = 0; foreach ( char c in s ) for (int t=0; t<s.Length; t++) if ( ) aantal++; s[t]==x c == x return aantal; }

50 Tekst Analyse 26 9 5 1 14 6 8 public class DiagramView: View {
public string Invoer; protected override void OnDraw(Canvas cv) { base.OnDraw(cv); tellers 9 5 1 14 2 3 4 26 Length 6 8 7 int [ ] tellers; tellers = new int [26]; } }

51 Tekst Analyse 26 9 5 1 14 6 8 public class DiagramView: View {
public string Invoer; protected override void OnDraw(Canvas cv) { base.OnDraw(cv); tellers 9 5 1 14 2 3 4 26 Length 6 8 7 int [ ] tellers; tellers = new int [26]; foreach(char c in Invoer) if (c>='a' && c<='z') tellers[ ]++; c-'a' int w = Width / max; int h = Height/ 26; , y=0; foreach(int a in tellers) { cv.DrawText( a.ToString()...); cv.DrawRect( x, y, x+a*w, y+h, verf ); y = y+h; } } }

52 Gelijkheid van Strings
static bool Gelijk( string s, string t) { int m = s.Length; int n = t.Length; for (i=0; i<m && i<n; i++) { char c = s[i]; char d= t[i]; if (c!=d) return false; } return m==n; }

53 Gelijkheid van Strings
class String { static bool Equals( string s, string t) { int m = s.Length; this.Length; int n = t.Length; for (i=0; i<m && i<n; i++) { char c = s[i]; this[i]; char d= t[i]; if (c!=d) return false; } return m==n; } }

54 ListView public class KleurenApp : Activity { ListView kleurLijst;
ArrayAdapter<string> kleurAdp; string[] kleuren = {"rood", "wit", "blauw"}; protected override void OnCreate(Bundle b) { base.OnCreate(b); kleurLijst = new ListView(this); kleurAdp = new ArrayAdapter<string> (this, SimpleListItemChecked, kleuren); kleurLijst.Adapter = kleurAdp; kleurLijst.Choicemode = Multiple; kleurLijst.ItemClick += klik; SetContentView(kleurLijst); } public void klik(object o, ItemClickEventArgs e) { string s = kleuren[e.Position]; Toast.MakeText(...t...) . Show( ); } }


Download ppt "Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie"

Verwante presentaties


Ads door Google