Topic27 Classes Objects 1
Topic27 Classes Objects 1
3
Clicker 1
What kind of assignment handout
do you prefer?
Why?
4
Example - Monopoly
Classes Needed:
If we had to start
from scratch what
classes would we
5
need to create?
A programming problem
Given a file of cities' (x, y) coordinates,
which begins with the number of cities:
6
50 20
90 60
10 72
74 98
5 136
150 91
8
Clients of objects
client program: A program that uses
objects.
– Example: Zombies is a client of DrawingPanel
and Graphics.
DrawingPanel.java (class)
Zombie.java (client program)
public class DrawingPanel {
public class Zombie {
...
main(String[] args) {
}
new DrawingPanel(...)
new DrawingPanel(...)
...
}
}
9
Classes and objects
class: A program entity that represents either:
1. A program / module, or
2. A template for a new type of objects.
creates
12
Our task
In the following slides, we will implement a
Point class as a way of learning about
defining classes.
13
Point objects (desired)
Point p1 = new Point(5, -2);
Point p2 = new Point(); // origin, (0, 0)
Data in each Point object:
Field name Description
x the point's x-coordinate
y the point's y-coordinate
A.Syntax error
B.Runtime error
C.false
D.true
E.no output
16
Object state:
Fields
17
Point class, version 1
public class Point {
private int x;
private int y;
}
– Save this code into a file named Point.java.
Declaration syntax:
access_modifier type name;
– Example:
public class Student {
// each Student object has a name and
// gpa field (instance variable)
private String name;
private double gpa;
}
19
Accessing fields
Other classes can access/modify an object's fields.
– depending on the access modifier
– access: variable.field
– modify: variable.field = value;
Example:
Point p1 = new Point();
Point p2 = new Point();
System.out.println("the x-coord is " + p1.x); // access
p2.y = 13; // modify
20
A class and its client
Point.java is not, by itself, a runnable
program.
– A class can be used by client programs.
Point.java (class of objects)
PointMain.java (client program) public class Point {
public class PointMain { int x;
main(String args) { int y;
Point p1 = new Point(); }
p1.x = 7;
p1.y = 2;
22
Client code redundancy
Suppose our client program wants to draw
Point objects:
// draw each city
Point p1 = new Point();
p1.x = 15;
p1.y = 37;
g.fillOval(p1.x, p1.y, 3, 3);
g.drawString("(" + p1.x + ", " + p1.y + ")", p1.x, p1.y);
24
Problems with static solution
We are missing a major benefit of objects: code reuse.
– Every program that draws Points would need a draw method.
The syntax doesn't match how we're used to using
objects.
draw(p1, g); // static (bad)
The point of classes is to combine state and behavior.
– The draw behavior is closely related to a Point's data.
– The method belongs inside each Point object.
25
Instance methods
instance method (or object method): Exists inside
each object of a class and gives behavior to each object.
public type name(parameters) {
statements;
}
– same syntax as static methods, but without static keyword
Example:
public void shout() {
System.out.println("HELLO THERE!");
}
26
Instance method example
public class Point {
private int x;
private int y;
// Draws this Point object with the given pen.
public void draw(Graphics g) {
...
}
}
The draw method no longer has a Point p parameter
How will the method know which point to draw?
– How will the method access that point's x/y data?
27
Point objects w/ method
Each Point object has its own copy of the draw method, which
operates on that object's state:
p1.draw(g); x 7 y 2
p2.draw(g);
public void draw(Graphics g) {
// this code can see p1's x and y
}
x 4 y 3
p2
public void draw(Graphics g) {
// this code can see p2's x and y
}
28
The implicit parameter
implicit parameter:
The object on which an instance method is called.
– During the call p1.draw(g);
the object referred to by p1 is the implicit parameter.
30
method questions
Write a method translate that changes a
Point's location by a given dx, dy amount.
32