Previous Lecture Object oriented programming Objects and classes

  • Slides: 15
Download presentation
§ Previous Lecture: Object oriented programming • Objects and classes • Methods • Modifiers

§ Previous Lecture: Object oriented programming • Objects and classes • Methods • Modifiers § Today’s Lecture: • Review (objects, classes, methods) • Method implementation and use • Constructor § Reading (JV): • Sec 5. 1 • Review Sec 4. 2 -4. 6, 5. 2 November 6, 2001 Lecture 18 1

Review questions § What is the difference between instance and class variables (and methods)?

Review questions § What is the difference between instance and class variables (and methods)? § How do you recognize a class variable (or method)? Declaration syntax: modifiers type name; Examples from Project 5: private int id; private static int next. ID; Room now; November 6, 2001 Lecture 18 2

Review questions, cont’d § Where do you put declarations for instance and static variables?

Review questions, cont’d § Where do you put declarations for instance and static variables? • Immediately inside the class definition (immediately inside class Room { … })? or • Inside a method definition (e. g. , inside method main)? § If a variable does not store a value of primitive type, what does it store? § What is the keyword for instantiating an object? November 6, 2001 Lecture 18 3

Methods Syntax: return-type method-name ( parameter-list ) { statement-list } § § § Modifiers

Methods Syntax: return-type method-name ( parameter-list ) { statement-list } § § § Modifiers can be used in the header return-type can be any primitive or nonprimitive types return-type void means nothing is returned from the method Methods that return values must have a return statement: return expression; Static methods cannot reference instance variables November 6, 2001 Lecture 18 4

Example class definition public class Account { private int balance; // current bal. private

Example class definition public class Account { private int balance; // current bal. private int deposits; // deposits to // date private int withdrawals; //withdrawal //to date // deposit d to account public void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account public void withdraw(int w) { balance -= w; withdrawals += w; } } November 6, 2001 Lecture 18 5

“Calling” a method § Class (static) method: class-name. method-name(expression-list) § Instance method: reference-variable. method-name(expr-list)

“Calling” a method § Class (static) method: class-name. method-name(expression-list) § Instance method: reference-variable. method-name(expr-list) November 6, 2001 Lecture 18 6

Using instance methods § § § Let m be a method in objects of

Using instance methods § § § Let m be a method in objects of class c Let r refer to object o of class c Then expression r. m( expression-list ) invokes method m in object o When method m is executed, field names signify the instance variables in object o Example: account 1. deposit(200); account 1 an Account variable balance 200 deposits 200 withdrawals 0 November 6, 2001 Lecture 18 7

Constructor § A constructor is used to create objects § Each class has a

Constructor § A constructor is used to create objects § Each class has a default constructor § Can define your own constructor: modifiers class-name( parameter-list ) { statements-list } § Use public as the modifier for now § Note: Looks like a method but it has no return type November 6, 2001 Lecture 18 8

Constructor invocation new class-name( expression-list ) § The value of above expression is a

Constructor invocation new class-name( expression-list ) § The value of above expression is a reference to a new object of the given class-name § The defined (or default) constructor is invoked on the new object created by new November 6, 2001 Lecture 18 9

Example constructor public class Room { // Each Room r has unique r. id>0.

Example constructor public class Room { // Each Room r has unique r. id>0. // next. ID is ID# of next Room to be // created. private static int next. ID; private int id; // Constructor public Room() { id = next. ID; next. ID = next. ID + 1; } } November 6, 2001 Lecture 18 10

After 3 calls of new Room() … next. ID id id id November 6,

After 3 calls of new Room() … next. ID id id id November 6, 2001 Lecture 18 11

Class Room § § Class Room is a collection of rooms Each Room has

Class Room § § Class Room is a collection of rooms Each Room has a unique ID# § Method find. Room(int target. ID) returns the Room with ID# equal to target. ID or returns null if there is no such room § But how to find the Room? Idea: keep all Rooms chained together § November 6, 2001 Lecture 18 12

Three instances of Room next. ID 4 id previous 1 null id previous 2

Three instances of Room next. ID 4 id previous 1 null id previous 2 id previous 3 last November 6, 2001 Lecture 18 13

Class definition public class Room { // Each Room r has unique r. id>0.

Class definition public class Room { // Each Room r has unique r. id>0. // next. ID is ID# of next Room to be // created. private static int next. ID; private int id; // In each Room r, r. previous is the // Room created immediately before r, // or isnull if r is 1 st Room. // last is the most recent Room to // have been created. private static Room last; private Room previous; . . . } November 6, 2001 Lecture 18 14

Constructor to create a chain of Rooms public class Room { private static int

Constructor to create a chain of Rooms public class Room { private static int next. ID; int id; static Room last; Room previous; // Constructor to create a chain of // Rooms public Room() { id = next. ID; next. ID = next. ID + 1; previous = last; last = this; } } November 6, 2001 Lecture 18 15