Transcript .ppt

Announcements
• Newsgroup: cornell.class.cs100
• Assignment W1 due tomorrow at the
beginning of class
• Assignment P1 can be picked up at the end
of class
CS 100
Lecture 3
1
Today’s Topics
•
•
•
•
•
•
•
Review
Classes
Instance of a Class (an object)
Fields
Declaration of class variables
The new operator
Class methods
CS 100
Lecture 3
2
Review
• What’s the difference between a variable
and a constant?
• When might you want to use an ‘if’
statement?
• What is the purpose of ‘block statements’?
CS 100
Lecture 3
3
Review Example 1
final int THRESHOLD = 65;
System.out.println(“Enter temperature: ”);
int temperature = Integer.parseInt(stdin.readLine());
if (temperature < THRESHOLD) {
System.out.println(“It’s cold.\n”);
}
CS 100
Lecture 3
4
Review Example 2
final boolean FOURLEGS = true;
final boolean BARKS = true;
final boolean MEOWS = false;
if (FOURLEGS == false)
System.out.println(“not four-legged”);
else if (BARKS)
System.out.println(“4 legs, probably a dog”);
else if (MEOWS)
System.out.println(“4 legs, probably a cat);
else
System.out.println(“4 legs, no idea what.”);
CS 100
Lecture 3
5
Rewrite of Previous example:
final boolean FOURLEGS = true;
final boolean BARKS = true;
final boolean MEOWS = false;
if (FOURLEGS == false)
System.out.println(“not four-legged”);
if ( FOURLEGS && BARKS && (MEOWS == false))
System.out.println(“4 legs, probably a dog”);
if (FOURLEGS && MEOWS && (BARKS == false))
System.out.println(“4 legs, probably a cat);
if (FOURLEGS && (MEOWS == false) && (BARKS ==
false))
System.out.println(“4 legs, no idea what.”);
CS 100
Lecture 3
6
Review: Formatting Output
• What does the following produce?
• “\tCourse\tStudents\n\tCS100\tMany”
Course
CS100
CS 100
Students
Many
Lecture 3
7
Why Classes?
• Recall: a variable is a named box into which
one can place values
x
-532
• Suppose we want boxes (variables) that
contain more than one value?
• For example:
Coordinate c
x -532
y
CS 100
Lecture 3
25
8
More examples
• Coordinate c1
• Coordinate c3
x 25
y 25
x 3
y 4
• Coordinate is a class. It’s like a type, except
that you (the programmer) define it.
• c1 and c3 are instances of the class; we call
them objects
• c1.x, c1.y, c3.x, c3.y are fields of objects c1
and c3 respectively.
CS 100
Lecture 3
9
Declaring a class in Java
public class Coordinate {
public int x;
public int y; }
• keywords public and class
• name of the class -- naming convention:
capitalize all words in class names, e.g.
StringBuffer
• body of the class (within { and })
– field declarations (normal type and class declarations)
– methods and constructors (later in the course)
CS 100
Lecture 3
10
Type declarations -- Class
declarations
• int x;
// x
0
• Coordinate w;
// w
null
• null is a Java constant -- it denotes the
absence of an object
• Just as x hasn’t been assigned (so defaults
to 0), w hasn’t been assigned, it has only
been declared
CS 100
Lecture 3
11
Take a breath
• So, objects (like c, as declared in
Coordinate c) have a type -- the type is the
object’s class (here: Coordinate)
• Classes have methods and fields
– fields are data variables associated with a class
and its objects
– methods contain the executable code of a class
CS 100
Lecture 3
12
Creating a class instance
• To create a new instance of a class, use the
operator new
• Coordinate w;
null
// w
• w = new Coordinate();
// w
x ?
y ?
CS 100
Lecture 3
13
Referencing fields
x 3
y 5
// w
// x
6
x = w.x;
x 3
y 5
// w
// x
CS 100
3
Lecture 3
14
Assigning to fields
// w
x 3
y 5
// w
x 7
y 5
w.x = 7;
CS 100
Lecture 3
15
Instance1 = Instance2 ??
• Assigning
one instance
to another
creates
aliases -- 2
names refer
to the same
object.
CS 100
// w
x 3
y 5
// h
x 7
y 1
h = w;
// w
x 3
y 5
// h
x 7
y 1
Lecture 3
16
What if you want to copy?
// w
x 3
y 5
// h
x 7
y 1
// w
x 3
y 5
// h
x 3
y 5
h.x = w.x;
h.y = w.y;
CS 100
Lecture 3
17
Methods of Classes
Classes can have methods that operate on fields of the class
public class Coordinate {
public int x;
public int y;
// Set field x to p*p
public void setX(int p) {
x= p*p; }
// Set field y to q
public void setY(int q) {
y= q;}
// Return the sum of the squares of the fields
public int sumSquares() {
return x*x + y*y; }
}
CS 100
Lecture 3
18
Return types of methods
// Return the sum of the squares of the fields
public int sumSquares() {
return x*x + y*y;
}
• sumSquares should return an integer to wherever
it is called from
CS 100
Lecture 3
19
Returns
• return <expression>
– terminates execution of the method in which it
appears
– returns value of <expression> to the place of
the call to the method
• NB: <expression> must match the return
type in the method header
• If the type is not void, Java insists that a
return statement exist
• Can also have plain ‘return’ with no
expression if void
CS 100
Lecture 3
20
Method calls
• Suppose we want to call some of the
methods of class Coordinate
• Use object name followed by a dot ‘.’
followed by method name:
– c.setX(3); // x field of c becomes 9
– a = c.sumSquares(); // a becomes . . . 81?
CS 100
Lecture 3
21
Example
• c.setX(3)
• c.setY(2)
// c
x 9
y 2
• // Store 85 in s
s = c.sumSquares();
CS 100
Lecture 3
22
Another example
d = new Coordinate();
d.setX(c.sumSquares());
d.setY(c.x)
// d
CS 100
x 7225
Lecture 3
y 9
23