01 Intro
01 Intro
Java Review
• We will also:
• study algorithms related to these data structures
• learn how to compare data structures & algorithms
• Goals:
• learn to think more intelligently about programming problems
• acquire a set of useful tools and techniques
Sample Problem I: Finding Shortest Paths
• Given a set of routes between pairs of cities, determine the
shortest path from city A to city B.
84 PORTLAND 49
CONCORD 74 PORTSMOUTH
83
63 54
134 44
ALBANY WORCESTER BOSTON
42
49
PROVIDENCE
NEW YORK 185
Requirements
• Lectures
• Sections
• optional but highly recommended
• start this week
• on Zoom; one section will be recorded
• Midterm exam
• Final exam
Additional Administrivia
• Instructor: Dave Sullivan
width 10 width 40
height 72
(You can also think of the methods as being inside the object,
but we won’t show them in our diagrams.)
Encapsulation
• Our classes should provide proper encapsulation.
Inheritance
• We can define a class that explicitly extends another class:
public class Animal {
private String name;
// other field definitions go here
• A class inherits the fields and methods of the class that it extends.
The Object Class
• If a class does not explicitly extend another class, it implicitly
extends Java’s Object class.
Object
Polymorphism
• An object can be used wherever an object of one of its
superclasses is called for.
• For example:
Animal a = new Dog();
Animal[] zoo = new Animal[100];
zoo[0] = new Ant();
zoo[1] = new Cat();
...
• For each method call, a new stack frame is added to the top
of the stack.
public class Foo {
public static int x(int i) {
int j = i - 2;
j 6
if (i >= 6) { i 8 x(8)
return i; // return 8
} return addr
return x(i + j); j 3
}
public static void i 5 x(5)
main(String[] args) {
return addr
System.out.println(x(5));
} args
}
public ArrayBag() {
this.items = new Object[DEFAULT_MAX_SIZE];
this.numItems = 0;
}
public ArrayBag(int maxSize) {
...
}
• The second one allows the client to specify the max # of items.
public ArrayBag() {
this.items = new Object[DEFAULT_MAX_SIZE];
this.numItems = 0;
}
public ArrayBag(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException(
"maxSize must be > 0");
}
this.items = new Object[maxSize];
this.numItems = 0;
}
...
}
b2
b1
args
items null null
…
numItems 0
Copying References
• A variable that represents an array or object is known as a
reference variable.
• Assigning the value of one reference variable to another
reference variable copies the reference to the array or object.
It does not copy the array or object itself.
int[] values = {5, 23, 61, 10};
int[] other = values;
other
values 5 23 61 10
• Given the lines above, what will the lines below output?
other[2] = 17;
System.out.println(values[2] + " " + other[2]);
Passing an Object/Array to a Method
• When a method is passed an object or array as a parameter,
the method gets a copy of the reference to the object or array,
not a copy of the object or array itself.
main main
a a
1 2 3 3 6 9
"hello, world"
b "hello, world"
message
args …
b "hello, world"
message
args …
b "hello, world"
message
args …
}
Would this work instead?