CS100A Lecture 8 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS100A Lecture 8

Description:

The value of expression. new class-name() is a reference to a new object of the given class-name ... A static method is also called a class method. ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 17
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: CS100A Lecture 8


1
CS100A 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

2
Problem Setting
  • We wish to represent rooms
  • Class Room is a collection of rooms
  • Each room has a unique ID
  • The ID should be automatically assigned, unique,
    and immutable, i.e., not changeable.
  • Method findRoom(int targetId) returns the room
    with ID equal to targetId, or null if there is
    no such room.

3
Class Definition
  • / Collection of rooms. /
  • import java.io.
  • public class Room
  • // Each room r has a unique r.id gt 0.
  • // nextId is the ID of the next room
  • // to be created.
  • private static int nextId 1
  • private int id
  • // Create a new room with a unique ID.
  • public Room()
  • id nextId
  • nextId nextId 1

4
Constructors
  • A constructor is used to create an objects.
  • A class can define its own constructor.
  • Class definition
  • class class-name
  • constructors
  • declarations
  • methods
  • Constructor definition
  • constructor-modifier
  • class-name( parameter-list )
  • statement-list

5
Constructor Invocation
  • The value of expression
  • new class-name()
  • is a reference to a new object of the given
    class-name
  • If there is a user-defined constructor with no
    parameters, then it is invoked on the new object
    created by new.

6
Field Declarations
  • A field declarations 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

7
Object Instances
  • An object is a collection of instance variables
    (known as fields) and methods
  • There is one class variable per static field
    declaration

8
Problem Setting, revisited
  • We wish to represent rooms
  • Class Room is a collection of rooms
  • Each room has a unique ID
  • Method findRoom(int targetId) returns the room
    with ID equal to targetId, or null if there is
    no such room.

9
Method Definitions, revisited
  • Method definitions
  • method-modifier
  • return-type method-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 static method is also called a class method.
  • A class method is invoked on the class, not on an
    object of the class, e.g.,

10
Method findRoom
  • // Return the room with id targetId,
  • // or null if there is no such room.
  • public static Room findRoom(int targetId)
  • . . .
  • But how are we going to find the room?
  • Idea keep all rooms chained together.
  • Objects can have fields that refer to other
    objects
  • The expression
  • this
  • evaluated in an instance method of object o
    is a reference to o.

11
Object Instances, revisited
12
Class Definition, revisited
  • / Collection of rooms. /
  • import java.io.
  • public class Room
  • // Each room r has a unique r.id gt 0.
  • // nextId is the ID of the next room
  • // to be created.
  • private static 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.
  • private static Room last
  • private Room previous
  • . . .

13
Constructor Room, revisited
  • / Collection of rooms. /
  • import java.io.
  • public class 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.
  • private static 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

14
Object Instances, revisited

15
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.

16
Method findRoom, revisited
  • // Return the room with id targetId,
  • // or null if there is no such room.
  • public static Room findRoom(int targetId)
  • Room r last
  • while (r ! null r.id ! targetId)
  • r r.previous
  • return r
  • expression expression is called a conjunction.
    It is true only when both operands are true. 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 ! targetId r ! null
  • Why?
Write a Comment
User Comments (0)
About PowerShow.com