Lecture 71 CS110 Lecture 7 February 17, 2004 Announcements –hw3 due Thursday Agenda –questions –hw3 tips –getters and setters – information hiding –delegation.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 4: Writing Classes
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Lecture 161 CS110 Lecture 16 Tuesday, March 30, 2004 Announcements –hw7 due Thursday –pass/fail, withdraw deadline April 8 Agenda –Questions –toString.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Games and Simulations O-O Programming in Java The Walker School
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Classes CS 21a: Introduction to Computing I First Semester,
Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
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.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Java Java Translation Program Structure
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC 212 Object-Oriented Programming and Java Part 2.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Encapsulation and Constructors
Group Status Project Status.
February , 2009 CSE 113 B.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Classes CS 21a: Introduction to Computing I
In this class, we will cover:
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Lecture 71 CS110 Lecture 7 February 17, 2004 Announcements –hw3 due Thursday Agenda –questions –hw3 tips –getters and setters – information hiding –delegation –Shapes application –boxes and arrows

Lecture 72 hw3 Practice new Java vocabulary (Lens.java) Improve TextFile class Draw box-and-arrow pictures Explore the Java API

Lecture 73 getters and setters Good private String contents; public String getContents() public void setContents (String contents) aTextFile.setContents(“foo”) in client class Watch naming conventions Bad (public access to field itself) public String contents; aTextFile.contents = “foo” in client class

Lecture 74 getters and setters Hide implementation details from TextFile clients setContents(String contents) (line 51) –sets value of field and … –changes modification date –practice using this int getSize() (line 97) –looks like a getter but … –there is no size field - code delegates the job

Lecture 75 Delegation Pass along the message, asking some other object to do the work for you Important OO design pattern The King asked The Queen, and The Queen asked The Dairymaid: "Could we have some butter for The Royal slice of bread?" A. A. Milne, “The King’s Breakfast”,

Lecture 76 this Keyword for the object we are looking at Tricky - takes getting used to Settles ambiguity in variable names: 40 this.contents = contents; declared on line 25 on line 37 Send a message to yourself 76 this.setContents(contents+text); is the same as 76 setContents(contents+text); (don’t forget that it is a message: this is implicit)

Lecture 77 String tricks ("hello, " + "world"). equals("hello, world") + concatenates Strings remember to send equals message, don’t test with == Java can sometimes guess what you mean, converting a number to a String: ("balance: $" + 100). equals("balance: $100")

Lecture 78 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 79 Shapes classes Particular shapes: –horizontal line: class HLine –box: class Box –VLine, Frame, Triangle (hw4) Shapes are clients for Screen –Use Screen API (javadoc) –Don’t look at source code TestShapes is a test driver (client) for HLine and Box

Lecture 710 Screen API (javadoc)

Lecture 711 TestShapes Client for Screen, HLine, Box, self documenting interesting code fragments 28-31: create a Screen, declare and create two HLines 32,33 : paintOn message to HLine wants Screen and position as arguments: “ask the HLine to paint itself on a Screen” - Screen is invisible still 34: creates an anonymous new HLine which is then asked to paint itself on the Screen 35: draw message to Screen gets Terminal as an argument “ask the Screen to draw itself on a Terminal” – finally, everything is visible

Lecture 712 Variables and Values (review) Variable: named place to hold a value of a particular type Kinds of variables: fields (instance variables), local variables in methods, parameters Variables must be declared before use Type is either: –primitive ( int, char, boolean,... ) –reference to an instance (object) of some class Why “reference to” ? Draw pictures...

Lecture 713 Draw a picture of a variable - box with narrow border, showing name and type If type is primitive, show value inside box If type is a class then value is a reference to an object... Boxes and Arrows name: type accountNumber: int 2

Lecture 714 Draw a picture of an object - box with thick border, showing type, containing fields (which are just variables) The object’s methods are not part of this picture! Boxes and Arrows paintChar: 3 ‘x’ length: int char HLine

Lecture 715 Boxes and Arrows HLine h0 = new HLine(3,‘x’); HLine h1; h0: h1:null HLine paintChar: 3 ‘x’ length: int char HLine Value of h0 is a reference to (arrow to) an HLine object (which wouldn’t fit into the h0 box in any case) Value of h1 is null (reserved word but not a keyword)

Lecture 716 How References Work h1 = h0; h0.setLength(9); Variables h0 and h1 refer to the same HLine instance The HLine referred to by h1 sees the change since it’s the same HLine h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine

Lecture 717 Reference values h0 = new HLine(5,‘y’); h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine paintChar: 5 ‘y’ length: int char HLine

Lecture 718 Reference values h0 = h1; Now no variable refers to this HLine - it’s ready for garbage collection h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine paintChar: 5 ‘y’ length: int char HLine

Lecture 719 Bank main - Boxes and Arrows “Engulf and Devour” int 200 BankAccount balance: BankAccount balance: Terminal String bankName: Bank BankAccount account1: BankAccount account2: Terminal atm: int Bank javaBank:

Lecture 720 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 721 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 722 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 723 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 724 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 725 while can replace for : int i = 0; while (i < 3) { for(int i=0;i<3;i++){ System.out.println(i); //ditto i = i + 1; } } for can replace while : boolean more = true; for( ; ask(); ) { while ( more ) { // do something // do something } more = ask(); } For loop advantages: –fewer lines, control all on one line, elegant, idiomatic for and while note empty start step start test step

Lecture 726 Signatures HLine paintOn messages in HLine unit test (main) –line 116: hline1.paintOn(screen) –line 118: hline1.paintOn(screen, 0, 1) Two declarations for paintOn in HLine.java: –line 45: paintOn(Screen, int, int) –line 58: paintOn(Screen) delegates work to first paintOn JVM uses shape of message to select method Signature: method name & types of parameters