1 of 39

Lecture 6:

ArrayLists & Inheritance

CS 136: Spring 2024

Katie Keith

2 of 39

Record on Zoom

3 of 39

Weekly routine

Wed.

Thurs.

Fri.

Sun.

Mon.

Tues.

Sat.

Lab due 10pm for Weds labs

Plus

Quiz 1 (Feb 28)

Midterm Exam (March 20)

Final Project

Final Exam

Honor

Code

Two Late Days on Labs

*Max 1 per lab

Start Early!

Lab released

Attend lab

Attend lab

Lab due 10pm for Thurs labs

4 of 39

  • Lab 1 due today (Weds lab groups) or tomorrow (Thurs lab groups)
  • Quiz 1 on Friday
  • Lab 2 released
    • This will likely be challenging. I’d encourage you to stay with the challenge.
    • The process of teaching yourself code and packages someone else wrote is a very important skill to have and will help you learn and grow as a computer scientist.
    • Getting started: Take a 1+ hours just to read and understand the starter code and requirements (before writing a line of code). Katie typically does this by printing out the code and working through it on paper.

📣 Announcements

5 of 39

  • Arrays of Objects
  • Packages
  • Using ArrayLists
  • Wrapper types
  • Inheritance

🎯 Today’s Learning Objectives

6 of 39

📚Readings

  • Sedgewick and Wayne. Algorithms. Section 1.2.
  • Oracle’s Java Docs for ArrayList

7 of 39

Task: Object-oriented Zoo!

8 of 39

Animal.java

💻

9 of 39

Reference type with toString()

public class Animal{

public String name;

public final String species;

public Animal(String name, String species){

this.name = name;

this.species = species;

}

public String toString(){

return this.name +" the "+ this.species + "!";

}

}

When we create a new class (new reference type), we typically write a custom toString() method which provides a readable string representation of the object (and can help us debug when the object is printed).

This is helpful because we will know the values of the object’s instance variables.

10 of 39

Review: Making arrays

Making arrays in Java requires three steps:

  1. Declare the array name and type
  2. Create the array
  3. Initialize the array values

double[] a;

a = new double[10];

for (int i = 0; i < 10; i++){

a[i] = 0.0;

}

We’re using new because an Array is a reference type

All elements in an array are the same type (here, a double)

We specify up-front the fixed number of elements in the array

The square brackets allow us to access the ith element of a.

Remember: We start at index 0.

1.

2.

3.

1.

2.

3.

11 of 39

Example: Arrays of objects

Animal[] animalTracker = new Animal[3];

animalTracker[0] = new Animal("Leo", "Lion");

animalTracker[1] = new Animal("Manny", "Elephant");

animalTracker[2] = new Animal("Stripes", "Zebra");

Declares and initializes an array with three elements, where each element is will be an object from the Animal class.

As we initialize each element in the array, we also have to use the new keyword since each of these elements is a new object.

12 of 39

Java’s Packages

In Java, a package is a grouping of related classes.

To create a package we:

  1. Choose a name for that package (conventionally lower case)
  2. Create a folder with that same name
  3. Use the line package name; at top of every Class file within that folder

In our example, this looks like

package zoostuff;

13 of 39

package zoostuff;

Zoo.java

Animal.java

ZooKeeper.java

💻

14 of 39

Compiling entire packages

mkdir bin

javac -d bin zoostuff/*.java

java -cp bin zoostuff.Zookeeper

Terminal

Makes a new folder called “bin”

Here, * is a “wildcard” so this gets all files that end with .java

When used with javac, -d stands for “destination directory” for the .class files

When used with java, -cp stands for “classpath” and tells JVM where to look for the .class files

Now we call the package name (zoostuff) followed by the class we want to execute main in (here, Zookeeper)

15 of 39

  • Arrays of Objects
  • Packages
  • Using ArrayLists
  • Wrapper types
  • Inheritance

🎯 Today’s Learning Objectives

16 of 39

Review: Array capacity is immutable

Animal[] animalTracker = new Animal[2];

animalTracker[0] = new Animal("Leo", "Lion");

animalTracker[1] = new Animal("Manny", "Elephant");

animalTracker[2] = new Animal("Stripes", "Zebra");

Q: When might that be a problem?

17 of 39

ArrayList

An ArrayList (also called a “dynamic array”) acts like an Array but an ArrayList’s capacity can grow as needed.

import java.util.ArrayList;

To use ArrayLists in Java, we’ll need this import statement at the top of a .java file.

18 of 39

This week’s road-map

  • Today: Using ArrayLists
  • Weds: Implementing ArrayLists data structure ourselves

19 of 39

Declaring and Creating an ArrayList

ArrayList<String> animalNames = new ArrayList<String>();

Variable name

Type of each element of the ArrayList

These angle brackets indicate this is a generic type

(We’ll dig into generics a little later on this week.)

Default capacity. Unlike Arrays we don’t have to specify an initial capacity and our ArrayList will grow dynamically.

20 of 39

ArrayLists: size vs. capacity

An ArrayList’s size refers to the number of non-null elements it contains.

An ArrayList’s capacity refers to the size of the array that backs the ArrayList.

“Leo”

“Manny”

“Stripes”

null

int size = animalNames.size(); // 3

// In our example, the capacity is 4 (last element is null)

Our “model” of the underlying array that backs the ArrayList

21 of 39

ArrayLists need Wrapper Types

ArrayList<Integer> numbers = new ArrayList<Integer>(10);

Need to use the wrapper type

Optional: we can declare an initial capacity of the ArrayList here.

22 of 39

Wrapper types

When we need to represent the value from a primitive type as a reference type, Java supplies built-in reference types known as wrapper types for each of the eight primitive types.

23 of 39

Autoboxing and unboxing

Java automatically converts between values from a wrapper type and the corresponding primitive type.

24 of 39

ArrayLists need Wrapper Types

ArrayList<Integer> numbers = new ArrayList<Integer>(10);

Need to use the wrapper type

Optional: we can declare an initial capacity of the ArrayList here.

25 of 39

Adding elements to an ArrayList

animalNames.add(“Leo”);

animalNames.add(“Manny”);

animalNames.add(“Stripes”);

ArrayList<String> animalNames = new ArrayList<String>();

Instance method

“Leo”

“Manny”

“Stripes”

null

26 of 39

Retrieving elements from an ArrayList

animalNames.get(0); // “Leo”

animalNames.get(2); // “Stripes”

Instance method

“Leo”

“Manny”

“Stripes”

null

27 of 39

ArrayLists expand automatically

animalNames.add(“Moo Deng”);

“Leo”

“Manny”

“Stripes”

“Mark”

“Toni”

Preview Weds:

In our implementation, we’ll have to choose a growth factor, create a new array, copy elements, and update the references…

When an ArrayList is at capacity, and a new element is added, the underlying ArrayList expands capacity automatically.

“Moo Deng”

null

null

null

null

“Leo”

“Manny”

“Stripes”

“Mark”

“Toni”

28 of 39

Removing elements from an ArrayList

animalNames.remove(“Manny”);

“Leo”

“Manny”

“Stripes”

“Mark”

null

“Leo”

null

“Stripes”

“Mark”

null

One more null element

Element shift: each element after the removed element is shifted one index to the left

If there are duplicates, only the first occurrence of the element is removed

29 of 39

Replacing elements in an ArrayList

animalNames.set(1, "Moo Deng");

“Leo”

“Moo Deng”

“Stipes”

“Mark”

null

Index

“Leo”

“Manny”

“Stripes”

“Mark”

null

30 of 39

Enhanced for-loop

for (String name : animalName){

System.out.println(name+ " is here!");

}

ArrayList

Type of the local variable (must match declared type of elements in the ArrayList).

In Java, to iterate over Arrays, ArrayLists (or other collections), we can use an “enhanced for-loop”

Local variable that holds the current element of the collection or array on each iteration of the loop.

31 of 39

public static ArrayList<String> compareRemove(ArrayList<String> animalNames){

for (String name : animalNames){

if ( name.charAt(0) > 'M'){

animalNames.remove(name);

}

}

return animalNames;

}

Suppose write compareRemove() below to remove animals’ names from the ArrayList if they begin after the letter ‘M’. However, we get the following runtime error. What is wrong and how could we fix it?

animalNames

“Leo”

“Moo Deng”

“Stipes”

“Mark”

“Stipes”

💡Think-pair-share

32 of 39

AnimalsArrayList.java

💻

33 of 39

  • Arrays of Objects
  • Packages
  • Using ArrayLists
  • Wrapper types
  • Inheritance

🎯 Today’s Learning Objectives

34 of 39

Inheritance: subclassing

Java supports an inheritance mechanism known as subclassing. Subclassing enables a programmer to add functionality to a class without rewriting an entire class from scratch.

extends

Superclass

Subclass

The extends keyword in Java enforces that the subclass inherits all instance methods and instance variables from the superclass.

class FastCat extends Animal{

...

}

35 of 39

Implementing inherited classes

class FastCat extends Animal{

public double maxSpeed; //MPH

public FastCat(String name, String species, double maxSpeed){

super(name, species);

this.maxSpeed = maxSpeed;

}

@Override

public String toString(){

return this.name + " the "+ this.species + " is super fast,"

+ this.maxSpeed +"max MPH!";

}

}

In Java, the keyword extends indicates that the class is inheriting from a superclass

Here, super calls the superclass’s constructor from the subclass’s constructor

subclass

superclass

The @ symbol is an “annotation” and @Override indicates that the annotated method overrides a method in the superclass with the same name. This will be checked by the compiler.

36 of 39

FastCats.java

💻

37 of 39

A subclass is the type of its superclass

A subclass not only inherits the instance variables and methods of the superclass, but it also inherits the types of the superclass.

ArrayList<Animal> theAnimals = new ArrayList<Animal>();

theAnimals.add(new Animal("Leo", "Lio"));

theAnimals.add(new FastCat("Claire", "Cheetah", 70.0));

Legal because FastCat is a Animal type due to inheritance

38 of 39

Style: JavaDoc comments and @param

/**

* An instance method to increase the maxSpeed of a FastCat

* @param amountIncrease is the amount to increase the FastCats's speed

*/

public void increaseSpeed(int amountIncrease){

this.maxSpeed += amountIncrease;

}

In JavaDocs, annotations start with @

Here, @param describes the input parameters of a method

This indicates this is a JavaDoc comment: a special type of block comment that describes a method’s specification.

39 of 39

  • Arrays of Objects
  • Packages
  • Using ArrayLists
  • Wrapper types
  • Inheritance

🎯 Today’s Learning Objectives