Introducn Computer Scence Shm Schocken IDC Herzlya Lecture 1-2: Lecture 1-2: A Taste Java Object-Orented Programmng A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 1 Lecture outlne Java background Java program example Basc syntax rules Program development lfe cycle A taste object orented programmng Homework exercse 1 A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 2 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Java background Bref hsry: 1995: nvented by James Goslng at Sun Mcrosystems (1995) Orgnal desgn objectve: a programmng language for Internet: safety portablty Actual accomplshment: a great language n almost every respect Borrows from many or languages: C / C, Pascal, Scheme, SmallTalk 1996: IDC adopts Java as CS11 programmng language 1999: Mcrost releases C# Why dd we adopt Java? Because Java... Is object orented (OO) Encourages good programmng habts Smlar C, but smpler more elegant Commercal Cool. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 3 Java program example Task: Prnt numbers 5 Algorthm: Java mplementan: ; ; whle whle ( ( prnt prnt 1 1 prnts prnts numbers numbers 5 5 publc publc PrntSomeNumbers PrntSomeNumbers publc publc statc statc vod vod man(strng[] man(strng[] args) args) declare declare an an nteger nteger varable varable set set t t nt nt ; ; whle whle ( ( prnt prnt current current value value System.out.prntln(); System.out.prntln(); 1; 1; System.out.prntln( De ); System.out.prntln( De ); A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 4 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Java syntax elements (frst approxman) keyword dentfer prnts prnts numbers numbers 5 5 comment publc publc PrntSomeNumbers PrntSomeNumbers publc publc statc statc vod vod man(strng[] man(strng[] args) args) declare declare an an nteger nteger varable varable set set t t nt nt ; ; symbol whle whle ( ( prnt prnt current current value value System.out.prntln(); System.out.prntln(); 1; 1; System.out.prntln( De ); System.out.prntln( De ); Strng numerc lteral lteral Words : Reserved words Identfers Lterals: Numbers Strngs (More later) Symbols: ( ) [ ],. ; - * / Comments Text begnnng wth A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 5 Java reserved words abstract ctnue for new swtch assert default go(*) package synchrzed boolean do f prvate ths break double mplements protected throw byte else mport publc throws case enum nstance return transent catch extends nt short try char fnal nterface statc vod fnally lg strct volatle cst(*) float natve super whle (*): Not used. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 6 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Java program structure Program (loosely defned): cssts e or more es Class: cssts e or more methods, e whch must be named Man() Method: a sequence statements Statement: ends wth a semcol (;) or enclosed n curly braces ( ) program publc publc PrntSomeNumbers PrntSomeNumbers publc publc statc publc vod man(strng[] xxx xxx args) publc statc vod man(strng[] args) nt ; publc yyy nt ; publc yyy publc PrntSomeNumbers whle ( publc PrntSomeNumbers whle ( publc statc vo publc statc vo prnt current value prnt current value System.out.prntln(); method System.out.prntln(); statements 1; 1; System.out.prntln( De ); System.out.prntln( De ); More methods follow... More methods follow... A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 7 Whte space prnts prnts numbers numbers 5 5 publc publc PrntSomeNumbers PrntSomeNumbers publc publc statc statc vod vod man(strng[] man(strng[] args) args) declare declare an an nteger nteger varable varable set set t t nt nt ; ; whle whle ( ( prnt prnt current current value value System.out.prntln(); System.out.prntln(); 1; 1; System.out.prntln( De ); System.out.prntln( De ); Same funcnalty publc publc PrntSomeNumbers PrntSomeNumbers publc publc statc statc vod vod man(strng[] man(strng[] args)nt args)nt ;whle ;whle (5)System.out.prntln();1;System.out.prntln( De ); (5)System.out.prntln();1;System.out.prntln( De ); Whte space comments ndentan (gnored by compler). Whte space, left programmer s dscren, s used for readablty Purpose: To make programs readable Important: Program readablty clarty are as mportant as program correctness (maybe more)! A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 8 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Syntax / semantcs / style Syntax: rules language: vocabulary grammar Semantcs: what a sentence n language means Style: how well do you say t? Natural languages: Sometmes t s allowed break syntax rules Occasally re s more than e meanng a sentence. Programmng languages: You are never allowed break syntax rules There s ly e semantc nterpretan: no ambguty. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 9 Lecture outlne Java background Java program example Basc syntax rules Program development lfe cycle A taste object orented programmng Homework exercse 1 A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 1 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Java program development lfe cycle Edt Xxx.java Java source fle javac Java compler Debuggng process Xxx. Bytecode fle java Java nterpreter Happy wth results? No Yes A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 11 The ols trade: bascs Plan text edr Complan execun: Debuggng. Run program 1. Observe program s execun 2. Fgure out what s wrg 3. Fx code n edr 4. Go step A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 12 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
The ols trade: Integrated Development Envrments IDE: a stware package that features a combnan : Edr (programmng-orented) Compler Debugger Project Manager Many more cool goodes Some Commercal IDEs: Eclpse (open source) Vsual Age IntelJ Jcrear NetBeans... A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 13 Debuggng That s what you ll do most semester Error types: Comple-tme errors: mostly syntax volans; detected by compler Run-tme errors: program passes complan, runs, but crashes Logcal errors: The program runs, dg somethng you ddn t want t do The program runs, but should be mproved for some reas. Murphy s Law: Anythng that can possbly go wrg, wll. Errors are portals dscovery (James Joyce) A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 14 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Lecture outlne Java background Java program example Basc syntax rules Program development lfe cycle A taste object orented programmng Homework exercse 1 A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 15 Introducng turtle Turtle descrpn (nformal) A turtle turtle s s a a turtle-lke turtle-lke graphcal graphcal mage mage that that moves moves screen screen under under program s program s ctrol. ctrol. When When turtle s turtle s tal tal s s down, down, movements movements are are traced traced (drawn (drawn screen). screen). When When tal tal s s up, up, movements movements are are not not traced. traced. Turtle abstracn (formal) A formal, formal, structured structured descrpn descrpn Turtle s Turtle s propertes propertes operans. operans. Amed Amed at at programmers programmers who who wsh wsh cstruct cstruct manpulate manpulate turtles turtles (Turtle (Turtle objects). objects). The The turtle turtle s s always always facng facng a a certan certan drecn, drecn, ts ts tal tal s s always always er er up up or or down. down. We We wsh wsh be be able able cstruct cstruct turtles turtles (Turtle (Turtle objects) objects) move move m m screen screen under under program program ctrol ctrol Turtle mplementan A publc publc Java Java,, named named Turtle, Turtle, that that mplements mplements Turtle Turtle abstracn. abstracn. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 16 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Turtle abstracn Turtle nterface Turtle API Turtle API (partal) The Turtle mplementan s a black box: we have no access ts code The Turtle abstracn (API) s a publcly avalable document The API specfes whch operans can be nvoked Turtle objects, how nvoke m Some se operans are desgned create new Turtle objects, whle ors are desgned manpulate exstng Turtle objects OOP Termnology: The words Abstracn Class nterface API Mean same thng: a structured, agreedup, user-orented way document funcnalty. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 17 Usng Turtle API Turtle API (partal) Turtle usage example publc publc TurtleDrawngDemo TurtleDrawngDemo publc publc statc statc vod vod man(strng[] man(strng[] args) args) Turtle Turtle leardo leardo new new Turtle(); Turtle(); leardo.taldown(); leardo.taldown(); leardo.moveforward(1); leardo.moveforward(1); leardo.turnrght(; leardo.turnrght(; leardo.moveforward(1); leardo.moveforward(1); A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 18 Inrducn Computer Scence Shm Schocken IDC Herzlya 21
Object orented programmng In OOP, much programmng actvty evolves around creatng manpulatng objects certan types. For example, leardo s an object type Turtle The rules for creatng manpulatng objects are specfed n nterfaces Some se es are mplemented by you; some es come from Java lbrary; some are mplemented by or programmers who you may or may not know For example, f somee wrote a named BouncngBall made t publcly avalable, programmers who develop applcans that need bouncng ball funcnalty can now use BouncngBall API Some OOP advantages Code reuse: no need re-nvent wheel Code csstency Dvde cquer Modularty. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 19 Homework Exercse 1 Play wth a smple Java program Experence debuggng Do some turtle graphcs Furr nstrucns: see course web ste. A Taste Java OO programmng, Shm Schocken, IDC Herzlya, www.ntro2cs.com slde 2 Inrducn Computer Scence Shm Schocken IDC Herzlya 21