Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Road Map Introduction to object oriented programming. Classes
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Games and Simulations O-O Programming in Java The Walker School
Classes, Objects, and Methods
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Java Classes Using Java Classes Introduction to UML.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
The Java Programming Language
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops and Iteration for Statements, while Statements and do-while Statements.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 9, 2014 Carolyn Seaman Susan Martin University of Maryland, Baltimore.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Lecture 71 CS110 Lecture 7 February 17, 2004 Announcements –hw3 due Thursday Agenda –questions –hw3 tips –getters and setters – information hiding –delegation.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CSC 212 Object-Oriented Programming and Java Part 2.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Objects as a programming concept
Suppose we want to print out the word MISSISSIPPI in big letters.
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Data types and variables
Java Programming: Guided Learning with Early Objects
Namespaces, Scopes, Access privileges
CS 106A, Lecture 7 Parameters and Return
Classes, Objects, and Methods
Group Status Project Status.
Building Java Programs
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
February , 2009 CSE 113 B.
Object Oriented Programming in java
Building Java Programs
Just Enough Java 17-May-19.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Building Java Programs
Loops and Iteration CS 21a: Introduction to Computing I
References Revisted (Ch 5)
Corresponds with Chapter 5
Classes and Methods 15-Aug-19.
Presentation transcript:

Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions –Shapes application –counting –signatures –scope –classes vs objects –how parameters really work

Lecture 72 Shapes A 20x10 Screen with 3 HLines: RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB draw 3 Boxes (2 overlapping): RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG Character graphics on your terminal

Lecture 73 Counting 1,2,3,... (everyday, mathematics) 0,1,2,... (computer science) Screen models (x,y) coordinates –y value increases as you read down –(0,0) is upper left hand corner –Each location holds one pixel – a character –Frame of + ’s is not part of Screen 5  3 Screen with G at position (3,1), & at position (0,2) G &

Lecture 74 for loop start test step for (int i = 0; i < 5; i=i+1) { System.out.println(2*i + 1); body } Prints 1, 3, 5, 7, 9 on successive lines –do start –if test is true do body do step go back and test again –else loop is done, so do first line after body Use a for loop when you know how many repetitions you want (else use while loop) See ForDemo.java in JOI

Lecture 75 for loop HLine paintOn() method (lines 47,48) for ( int i = 0; i < length; i++ ){ s.paintAt( x+i, y, paintChar ); } Counts from i = 0 to i = length-1, executing what’s in the body each time –i=0: ask Screen s to put paintChar at (x,y) –i=1: ask Screen s to put paintChar at (x+1,y) –i=2: ask Screen s to put paintChar at (x+2,y) –and so on … at (x+length-1,y)

Lecture 76 for loop for ( int i = 0; i < length; i++ ){ s.paintAt( x+i, y, paintChar ); } Variable i is declared inside for statement Surround body with braces {...} for safety i++ is short for i = i+1 (or i += 1 ) Can do the same job other ways: for (int col=x+len-1; col >=x; col-- ){ s.paintAt( col, y, paintChar ); }

Lecture 77 while can replace for : int i = 0; while (i < 3) { for(int i=0;i<3;i++){ System.out.println(i); //ditto i++; } } for can replace while : //ask() returns boolean boolean more; for( ; ask(); ) { while ( ask() ) { // do something // do something } } for and while note empty start step start test step body

Lecture 78 for and while for loop better than while loop –fewer lines –control all on one line at top of loop –elegant, idiomatic –natural when loop count known in advance while loop better than for loop –reads more like pseudocode (English) –natural when loop count not known in advance (set somehow in body)

Lecture 79 Signatures HLine paintOn messages in HLine unit test (main) –line 106: hline1.paintOn(screen) –line 108: hline1.paintOn(screen, 0, 1) Two declarations for paintOn in HLine.java: –line 39: paintOn(Screen, int, int) –line 52: paintOn(Screen) line 54 delegates work to first paintOn (could use this ) JVM uses shape of message to select method Signature: method name & types of parameters

Lecture 710 Scope scope of a variable or method: where its unadorned name is visible to the program Usually: the block { … } where it’s declared Examples from HLine.java –scope of all fields: lines –scope of screen (line 100) : lines –scope of length (line 25): lines –scope of i (line 41): lines Scope of a method is the class it’s declared in public declaration does not change scope

Lecture 711 Scope To see a method or field outside its scope, qualify the name of the method or field: –account.getBalance() –System.out –this.contents But –account.balance will fail because balance is private

Lecture 712 static Java keyword for belongs to whole class rather than to an instance of the class Static things are rare, objects are common: too much static is bad design public static void main( ) –main() is a static method - it can run before any objects are created with new –TestShapes (like many testing programs) is all static: there is a TestShapes class, but never a TestShapes object (although main uses objects

Lecture 713 What can main() see? HLine is meant to be a client class –private fields –public getters, setters, other methods as appropriate HLine has a static main method, for unit testing main in HLine –can’t refer to length field or paintOn method, since those belong only to HLine objects –can instantiate an HLine object, and then send it a paintOn message

Lecture 714 Static tools in the library To invoke a static method, send a message to the class (there is no object) - syntax ClassName.methodName( args ) Math.sqrt( x ) Calendar.getInstance() a factory method -Java designers chose this rather than new Calendar UnitTest.java line 21: HLine.main(args) sends message to HLine class to run main() there

Lecture 715 static fields Syntax for accessing static field: ClassName.fieldName (e.g. System.out ) (no System constructor, no System object) Like global variables (in other languages) In Integer class (part of Java API) public static final int MAX_VALUE = ; final : Java keyword for “can’t be changed” int big = Integer.MAX_VALUE; // OK Integer.MAX_VALUE = 255; // error Naming convention for final fields: ALL_CAPS

Lecture 716 How parameters really work Box.java line 143 sends a message: box2.paintOn( screen, 2, 2 ); Execution shifts to method at Box.java line 52: public void paintOn( Screen s, int x, int y) Value of parameter – s in method is value of screen in message –x in method is (first) 2 in message –y in method is (second) 2 in message

Lecture 717 How parameters really work Name of parameter ( s ) in method declaration need not match the name of the value in the message ( screen ) You can’t even think they should match: –The value in the message might not even have a name (the 2 in the example) –The method can be written before the client (in some other class) has even been imagined - and the client programmer does not have access to the source code with the method declaration The type of the value in the message must match the type in the method declaration

Lecture 718 In Box main screen: Screen box1: Box box2: Box width: int 3 width:4 pntCh:'G' int char

Lecture 719 In Box paintOn screen: Screen box1: Box box2: Box s: Screen x: int 2 this: Box y: int 2 width: int 3 width:4 pntCh:'G' int char out of scope