Introduction to Objects: Lecture #3
Douglas Blank, Bryn Mawr College, CS206, Spring 2013
Objects in Java
An object is a new type of thing, like String, int, or float.
You define what goes into an object using the class keyword.
You create an instance of an object using the new keyword.
A simple Example
Let's create a simple Dog object:
class Dog {
}
Now, let's create an instance of it:
Dog snoopy = new Dog();
A less simple Example
Let's create a simple Dog object:
class Dog {
String name;
}
Now, let's create an instance of it:
Dog snoopy = new Dog();
snoopy.name = "Snoopy";
Making Objects
Dog snoopy = new Dog();
snoopy.name = "Snoopy";
We can make a special "constructor" function that takes the name as an argument:
Dog snoopy = new Dog("Snoopy");
Making Objects
class Dog {
String name;
Dog(String name) {
this.name = name;
}
}
Dog dog = new Dog("Puppy");
Making Objects
Dog dog1 = new Dog("Puppy");
Dog dog2 = new Dog("Sparky");
Dog dog3 = new Dog("Gracie");
Dog dog4 = new Dog("Mr. Puddles");
Dog dog5 = new Dog("Kirena");
Dog dog6 = new Dog("Pepper");
Dog dog7 = new Dog("Woofie");
Dog dog8 = new Dog("Boo");
Making Objects
import Myro;
class Dog {
String name;
Dog(String name) {
this.name = name;
}
void speak() {
Myro.speak("woof woof. My name is " +
this.name);
}
}
Making Objects
Dog dog = new Dog("Snoopy");
System.out.println(dog.name);
dog.speak();
Linked List
A linked list is a recursive data structure that can keep track of a chain of elements.
LinkedList ll = new LinkedList("Thing 1");
ll.next = new LinkedList("Thing 2");
ll.next.next = new LinkedList("Thing 3");
Linked List
class LinkedList {
String name;
LinkedList next;
LinkedList(String name) {
this.name = name;
}
}
Exactly like Dog, except for recursive next
Methods
Functions in a class
If you use "static" to describe a method, then you call it without an instance.
Otherwise, the method can only be called from an instance.
Linked List
Rather than:
LinkedList ll = new LinkedList("Thing 1");
ll.next = new LinkedList("Thing 2");
ll.next.next = new LinkedList("Thing 3");
How about:
LinkedList ll = new LinkedList("Thing 1");
ll.add("Thing 2");
ll.add("Thing 3");
Object Functions
A function defined as part of a class are called "methods"
There can be "static" methods (call via the class name)
MyFunctions.volume(4, 5, 6);
There can be regular methods (call via the instance)
snoopy.speak();
Adding Methods
class LinkedList {
String name;
LinkedList next;
LinkedList(String name) {
this.name = name;
}
void add(String name) {
if (next == null)
next = new LinkedList(name);
else
...
}
}
Adding Methods
class LinkedList {
String name;
LinkedList next;
LinkedList(String name) {
this.name = name;
}
void add(String name) {
if (next == null)
next = new LinkedList(name);
else
next.add(name);
}
}
How does ll.add("Name") work?
When you call ll.add("Name"), it checks to see if ll.next is null on the first item (this) in the list.
ll.add("Thing 5");
Problem!
How do we make a list with nothing in it?
Problem!
How do we make a list with nothing in it?
Solution: Make two constructors
class LinkedList {
String name;
LinkedList next;
LinkedList(String name) {
this.name = name;
}
LinkedList() {
this.name = null;
}
}
Recursive Methods for �Recursive Data
void add(String name) {
if (this.name == null) { // base case
this.name = name;
} else if (next == null) { // base case
next = new LinkedList(name);
} else {
next.add(name);
}
}
Some new methods: length
java> LinkedList ll = new LinkedList();
java> ll.length();
0
java> ll.add("Hello");
java> ll.length();
1
Some new methods: length
class LinkedList {
...
int length() {
...
}
...
}
Some new methods: length
int length() {
if (name == null) {
return 0;
} else if (next == null) {
return 1;
} else {
return 1 + next.length();
}
}
Some new methods: length
int length() {
if (name == null) { // base case
return 0;
} else if (next == null) { // base case
return 1;
} else {
return 1 + next.length();
}
}
Challenge: insert(String name)
What would it take to insert a name such that the LinkedList would be in Alphabetical order?
Problem! What happens if we want to insert at the beginning?