160 likes | 177 Views
CS100J Lecture 8. Previous Lecture Programming Concepts Classes, Objects, and Methods --- reiterated Accessors Encapsulation Java Constructs return statement Visibility modifiers public private This Lecture Programming concepts defining your own constructor chaining objects together
E N D
CS100J Lecture 8 • Previous Lecture • Programming Concepts • Classes, Objects, and Methods --- reiterated • Accessors • Encapsulation • Java Constructs • return statement • Visibility modifiers • public • private • This Lecture • Programming concepts • defining your own constructor • chaining objects together • searching • Java Constructs • Field and method modifier static • Object reference this • Conjunction&& • Reading: • Lewis & Loftus, Section 5.2 • Savitch, Chapter 5 Lecture 8
Problem Setting • We wish to represent rooms: • Class Room is a collection of rooms • Each room has a unique ID# • The assignment of ID#s to room objects should be automatic, unique, and immutable, i.e., not changeable. • Method idToRoom(int id) returns the room with ID# equal to id, or null if there is no such room. Lecture 8
Class Definition /* Collection of rooms. */ import java.io.*; publicclass Room { // Each room r has a unique r.id > 0. // nextId is the ID# of the next room // to be created. privatestatic int nextId = 1; private int id; // Create a new room with a unique ID#. public Room() { id = nextId; nextId = nextId + 1; } } Lecture 8
Object Instantiation and Constructors • Evaluation of the expression newclass-name() creates a new object of the given class-name and returns a reference to that new object. • A class can define its own constructor.A constructor is like a method, but … • Has the same name as the class • Is automatically invoked on newly instantiated objects to provide an opportunity for initialization • Thus, evaluation of the expression newclass-name() really does three things: • Create a new object of the given class-name • Invoke any (parameterless) user-defined constructor of the class • Return a reference to the new object. Lecture 8
Constructors • Class definition classclass-name { constructors-declarations-and-methods } • Constructor definition constructor-modifier class-name( parameter-list ) { declaration-and-statement-list } • where constructor-modifier can be • public • private (use to be explained later) • one other constructor-modifier later Lecture 8
Field Declarations • A field declaration can have field-modifiers • Declarations field-modifiers type name ; • Possible field-modifiers are: public private static others later • A private field is not visible from outside the class definition. • A static field is also called a class variable. • A class variable is not an instance variable of each object; rather, there is precisely one instance of a class variable no matter how many objects of the class are eventually created Lecture 8
A room object id 0 fields methods Another room object Another room object id id 0 0 fields fields methods methods 1 A class variable nextId Object Instances • An object is a collection of instance variables (known as fields) and methods • There is one class variable per static field declaration Lecture 8
Problem Setting, revisited • We wish to represent rooms: • Class Room is a collection of rooms • Each room has a unique ID# • Method idToRoom(int id) returns the room with ID# equal to id, or null if there is no such room. Lecture 8
Method Definitions, revisited • Method definitions method-modifier return-typemethod-name( parameter-list ) { statement-list } • Possible method-modifiers are: public private static others later • A private method is not visible from outside the class definition. • A staticmethod is also called a class method. • A class method is invoked on the class, not on an object of the class, e.g., class-name.method-name(expression-list) Lecture 8
Method idToRoom // Return the room with id# id, // or null if there is no such room. publicstatic Room idToRoom(int id) { . . . } • But how are we going to find the room? • Idea: keep all rooms chained together. • Objects can have fields that refer to other objects • Needed later... • The expression this evaluated in an instance method of object o, or in a constructor for object o, is a reference to o. Lecture 8
Another room object Another room object A room object id previous id previous id previous 3 1 2 fields fields fields null methods methods methods A class variable last Object Instances, revisited Lecture 8
Class Definition, revisited /* Collection of rooms. */ import java.io.*; publicclass Room { // Each room r has a unique r.id > 0. // nextId is the ID# of the next room // to be created. privatestatic int nextId = 1; private int id; // For each room r, r.previous is the // room created immediately before // r was created, or null if r was the // first room created. The most recent // room to have been created is last. privatestatic Room last; private Room previous; . . . } Lecture 8
Constructor Room, revisited /* Collection of rooms. */ import java.io.*; publicclass Room { . . . // For each room r, r.previous is the // room created immediately before // r was created, or null if r was the // first room created. The most recent // room to have been created is last. privatestatic Room last; private Room previous; // Create a new room with a unique ID# linked // to the previous room created. public Room() { id = nextId; nextId = nextId + 1; previous = last; last = this; } } Lecture 8
null A class variable last A room object id previous 0 fields null methods Another room object Another room object id previous id previous 0 0 fields fields null null methods methods 1 A class variable nextId Object Instances, revisited Lecture 8
Pattern for Searching // Search for something. // Start at the first place to look. r = the first place look; while ( r is not what we are looking for and there are still more places to look ) r = the next place to look ; // Now r is either what we were looking for, // or is an indication that there were no // more places to look. Lecture 8
Method idToRoom, revisited // Return the room with id# id, // or null if there is no such room. publicstatic Room idToRoom(int id) { Room r = last; while (r != null && r.id != id) r = r.previous; return r; } • expression && expression is called a conjunction. It istrueonly when both operands aretrue. If the left operand is false, the right operand is not evaluated. • Note: The order of the conjunction is important! In particular, it would be wrong to use the conjunction r.id != id && r != null • Why? Lecture 8