CS 100 A Fall 1998 8 September Concepts
CS 100 A, Fall 1998, 8 September Concepts for this lecture: • Class • Instance of a class, (an object) • Fields (instance variables) • Declaration of class variables • Operator new • Class methods Reading in Holmes 6. 1 -6. 4 (191 -197) 6. 7 (202 -204) CS 100 A, 8 Sept. 1998. Lecture 3
Variable: a variable is a named box into which one can place a value of some type. int x -49 We want boxes (variables) that can contain more than one value, e. g. x 25 Coordinate c y 20 CS 100 A, 8 Sept. 1998. Lecture 3
Coordinate c 1 Coordinate c 5 x 25 y 25 x 6 y 21 Coordinate is a class. A class is like a type, except that YOU can declare it. c 1 and c 5 are instances of the class; they are objects. c 1. x and c 1. y are fields of object c 1. CS 100 A, 8 Sept. 1998. Lecture 3
Declaration in Java of class Coordinate: public class Coordinate { public int x; public int y; } • keywords public and class • name of the class. Convention: capitalize all words in a class names. E. g. String. Buffer, • body of the class, delimited by { } – field declarations. Just normal type and class declarations – methods and constructors (discussed later) CS 100 A, 8 Sept. 1998. Lecture 3
Type declaration and class declarations int x; 0 // x Coordinate w; // w null is a Java constant; it denotes the absence of an object. CS 100 A, 8 Sept. 1998. Lecture 3
Creating a new instance of a class: use new: Coordinate w; // w null w= new Coordinate(); x ? y ? // w CS 100 A, 8 Sept. 1998. Lecture 3
Referencing a field of an instance. x 3 y 5 // w // x 6 x= w. x; // // w x x 3 y 5 3 CS 100 A, 8 Sept. 1998. Lecture 3
Assigning to a field of an instance. (Later, we’ll investigate good programming practices for referring to and assigning to fields of an instance. x 3 y 5 // w w. x= 7; // x 7 y 5 w CS 100 A, 8 Sept. 1998. Lecture 3
Assigning one instance to another: creates alias: two names refer to the same object! // w x 3 y 5 // h x 7 y 1 h= w; // // w x 3 y 5 h x 7 y 1 CS 100 A, 8 Sept. 1998. Lecture 3
Classes can have methods that operate on the fields of the class public class Coordinate { public int x; public int y; // Set field x to p*p public void set. X(int p) { x= p*p; } // Set field y to q public void set. Y(int q) { y= q; } // Return the sum of the squares of the fields public int sum. Squares() { return x*x + y*y; } } CS 100 A, 8 Sept. 1998. Lecture 3 10
Execution of statement return <expression> terminates execution of the method (function) in which it appears and “returns” the value of <expression> to the place of call. Example of calls: c. set. X(3); c. set. Y(2); x x 9 y 2 //Store 85 in s s= c. sum. Squares(); CS 100 A, 8 Sept. 1998. Lecture 3 11
We discussed several concepts concerning a classes: • Definition of a class, including fields and methods, • Declaration of a class variable, • Creation of a new instance of a class and storing it in a class variable. • Referencing and assigning to a field of a class. • Assigning a class instance to a class variable, thus creating an alias. • Class methods (procedures and functions) We have looked only at the mechanics; we haven’t spend much time on using these things. That will come later. For now, it’s imperative that you learn the mechanics. The demonstrations using Code. Warrior on the Mac should help provide some perspective. CS 100 A, 8 Sept. 1998. Lecture 3 12
- Slides: 12