A Taste of Java and Object-Oriented Programming

Similar documents
News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Pass by Reference vs. Pass by Value

Outline. CIS 110: Introduction to Computer Programming. Review: Interactive Sum. More Cumulative Algorithms. Interactive Sum Trace (2)

Terminal Window. 11. Section 7 Exercises Program Memory Exercise 7-1 Swap Values in an Array Working memory Global Memory. 2 nd call 3 rd call

Notes on Organizing Java Code: Packages, Visibility, and Scope

Assembler. Building a Modern Computer From First Principles.

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Outline. CIS 110: Intro to Computer Programming. What Do Our Programs Look Like? The Scanner Object. CIS 110 (11fa) - University of Pennsylvania 1

CMPS 10 Introduction to Computer Science Lecture Notes

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Esc101 Lecture 1 st April, 2008 Generating Permutation

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

On Some Entertaining Applications of the Concept of Set in Computer Science Course

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Midterms Save the Dates!

Computer models of motion: Iterative calculations

Brave New World Pseudocode Reference

OPL: a modelling language

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Loop Transformations, Dependences, and Parallelization

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

LLVM passes and Intro to Loop Transformation Frameworks

Agenda & Reading. Simple If. Decision-Making Statements. COMPSCI 280 S1C Applications Programming. Programming Fundamentals

CS1100 Introduction to Programming

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Oracle Database: SQL and PL/SQL Fundamentals Certification Course

Computer Hardware 6. The

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Module Management Tool in Software Development Organizations

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

Lecture 5: Multilayer Perceptrons

K-means and Hierarchical Clustering

COMP 202 Java in one week

PHYSICS-ENHANCED L-SYSTEMS

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Intro. Iterators. 1. Access

Midterms Save the Dates!

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

Machine Learning: Algorithms and Applications

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

Nachos Project 3. Speaker: Sheng-Wei Cheng 2010/12/16

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

presentations Design simple Visual brevity will help a great talk. Shopping Preference Before&After Continued Continued Inside 15% Outside 85%

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

Programming in Fortran 90 : 2017/2018

A Binarization Algorithm specialized on Document Images and Photos

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Outline. Discriminative classifiers for image recognition. Where in the World? A nearest neighbor recognition example 4/14/2011. CS 376 Lecture 22 1

CSE 326: Data Structures Quicksort Comparison Sorting Bound

An Optimal Algorithm for Prufer Codes *

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Java Bytecode (binary file)

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Concurrent models of computation for embedded software

3D vector computer graphics

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Priority queues and heaps Professors Clark F. Olson and Carol Zander

Steps for Computing the Dissimilarity, Entropy, Herfindahl-Hirschman and. Accessibility (Gravity with Competition) Indices

PBRT core. Announcements. pbrt. pbrt plug-ins

CS 534: Computer Vision Model Fitting

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

SENSITIVITY ANALYSIS IN LINEAR PROGRAMMING USING A CALCULATOR

Setup and Use. Version 3.7 2/1/2014

Dijkstra s Single Source Algorithm. All-Pairs Shortest Paths. Dynamic Programming Solution. Performance. Decision Sequence.

CSc 520 Principles of Programming Languages. Example Language Translators. What s a Language Translator??? 2: Translators

PYTHON IMPLEMENTATION OF VISUAL SECRET SHARING SCHEMES

Building Java Programs. Introduction to Programming and Simple Java Programs

Sorting and Algorithm Analysis

Programming Assignment Six. Semester Calendar. 1D Excel Worksheet Arrays. Review VBA Arrays from Excel. Programming Assignment Six May 2, 2017

Design and Analysis of Algorithms

Architecture Evolution

Tsinghua University at TAC 2009: Summarizing Multi-documents by Information Distance

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Introduction to Java Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Composition of UML Described Refactoring Rules *

9. BASIC programming: Control and Repetition

Report on On-line Graph Coloring

Collaboratively Regularized Nearest Points for Set Based Recognition

Computer Components. Software{ User Programs. Operating System. Hardware

High-Boost Mesh Filtering for 3-D Shape Enhancement

Polyhedral Compilation Foundations

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Key Differences Between Python and Java

CHAPTER 4. Applications of Boolean Algebra/ Minterm and Maxterm Expansions

Computer Components. Software{ User Programs. Operating System. Hardware

Alignment Results of SOBOM for OAEI 2010

GSLM Operations Research II Fall 13/14

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

2 rd class Department of Programming. OOP with Java Programming

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

index.pdf March 20,

Introduction to Programming Using Java (98-388)

Load Balancing for Hex-Cell Interconnection Network

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

Transcription:

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