1 of 26

Introduction to Objects: Lecture #3

Douglas Blank, Bryn Mawr College, CS206, Spring 2013

2 of 26

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.

3 of 26

A simple Example

Let's create a simple Dog object:

class Dog {

}

Now, let's create an instance of it:

Dog snoopy = new Dog();

4 of 26

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";

5 of 26

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");

6 of 26

Making Objects

class Dog {

String name;

Dog(String name) {

this.name = name;

}

}

Dog dog = new Dog("Puppy");

7 of 26

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");

8 of 26

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);

}

}

9 of 26

Making Objects

Dog dog = new Dog("Snoopy");

System.out.println(dog.name);

dog.speak();

10 of 26

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");

11 of 26

Linked List

class LinkedList {

String name;

LinkedList next;

LinkedList(String name) {

this.name = name;

}

}

Exactly like Dog, except for recursive next

12 of 26

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.

13 of 26

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");

14 of 26

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();

15 of 26

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

...

}

}

16 of 26

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);

}

}

17 of 26

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.

  • If this.next is null, then it makes a new LinkedList and sets this.next to it.
  • If this.next is not null, then it simply calls this.next.add with the name and that does these two steps again, except this is now the second item in the list

ll.add("Thing 5");

18 of 26

Problem!

How do we make a list with nothing in it?

19 of 26

Problem!

How do we make a list with nothing in it?

  • We could make a special class to be the Head and a different class to be the rest
  • We could leave ll.name empty (null) if there is no first item

20 of 26

Solution: Make two constructors

class LinkedList {

String name;

LinkedList next;

LinkedList(String name) {

this.name = name;

}

LinkedList() {

this.name = null;

}

}

21 of 26

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);

}

}

22 of 26

Some new methods: length

java> LinkedList ll = new LinkedList();

java> ll.length();

0

java> ll.add("Hello");

java> ll.length();

1

23 of 26

Some new methods: length

class LinkedList {

...

int length() {

...

}

...

}

24 of 26

Some new methods: length

int length() {

if (name == null) {

return 0;

} else if (next == null) {

return 1;

} else {

return 1 + next.length();

}

}

25 of 26

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();

}

}

26 of 26

Challenge: insert(String name)

What would it take to insert a name such that the LinkedList would be in Alphabetical order?

  • Work your way down the list
  • Find the place where it goes
  • Insert it by changing this.next

Problem! What happens if we want to insert at the beginning?