Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts

Similar presentations


Presentation on theme: "CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts"— Presentation transcript:

1 CS100J Lecture 8 Previous Lecture This 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 Field and method modifier static Object reference this Conjunction && Reading: Lewis & Loftus, Section 5.2 Savitch, Chapter 5 CS 100 Lecture 8

2 Problem Setting We wish to represent widgets:
Class Widget is a collection of widgets Each widget has a unique serial number (ID#) The assignment of ID# to widgets should be automatic, unique, and immutable, i.e., not changeable. There should be no limit on the number of widgets that can be created. Method Widget idToWidget(int id) returns a reference to the widget with ID# equal to id, or null if there is no such widget. CS 100 Lecture 8

3 Class Definition /* Collection of widgets. */ import java.io.*;
public class Widget { // Each widget w has a unique w.id > 0. // nextId is the ID# of the next widget // to be created. private static int nextId = 1; private int id; // Create a new widget with a unique ID#. public Widget() { id = nextId; nextId = nextId + 1; } CS 100 Lecture 8

4 Object Instantiation and Constructors
Evaluation of the expression new class-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 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. CS 100 Lecture 8

5 Constructors Class definition Constructor definition
class class-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 CS 100 Lecture 8

6 Field Declarations A field declaration can have field-modifiers
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 CS 100 Lecture 8

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 nextId 1 A class variable id fields methods A widget object id fields methods Another widget object id fields methods Another widget object CS 100 Lecture 8

8 Problem Setting, revisited
We wish to represent widgets: Class Widget is a collection of widgets Each widget has a unique ID# Method idToWidget(int id) returns the widget with ID# equal to id, or null if there is no such widget. CS 100 Lecture 8

9 Method Definitions, revisited
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., class-name.method-name(expression-list) CS 100 Lecture 8

10 Method idToWidget But how are we going to find the widget?
// Return the widget with id# id, // or null if there is no such widget. public static Widget idToWidget(int id) { . . . } But how are we going to find the widget? Idea: keep all widgets 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. CS 100 Lecture 8

11 Object Instances, revisited
id previous 1 fields methods A widget object null id previous 2 fields methods Another widget object id previous 3 fields methods Another widget object last A class variable CS 100 Lecture 8

12 Class Definition, revisited
/* Collection of widgets. */ import java.io.*; public class Widget { // Each widget w has a unique w.id > 0. // nextId is the ID# of the next widget // to be created. private static int nextId = 1; private int id; // For each widget w, w.previous is the // widget created immediately before // w was created, or null if w was the // first widget created. The most recent // widget to have been created is last. private static Widget last; private Widget previous; . . . } CS 100 Lecture 8

13 Constructor Widget, revisited
/* Collection of widgets. */ import java.io.*; public class Widget { . . . // For each widget w, w.previous is the // widget created immediately before // r was created, or null if r was the // first widget created. The most recent // widget to have been created is last. private static Widget last; private Widget previous; // Create a new widget with a unique ID# // linked to the previous widget created. public Widget() { id = nextId; nextId = nextId + 1; previous = last; last = this; } CS 100 Lecture 8

14 Object Instances, revisited
nextId 1 A class variable last null A class variable id previous fields methods A widget object null id previous fields methods Another widget object null id previous fields methods Another widget object null CS 100 Lecture 8

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. CS 100 Lecture 8

16 Method idToWidget, revisited
// Return the widget with id# id, // or null if there is no such widget. public static Widget idToWidget(int id) { Widget r = last; while (r != null && r.id != id) 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 != id && r != null Why? CS 100 Lecture 8


Download ppt "CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts"

Similar presentations


Ads by Google