Primitives, Objects, ADTs, Inheritance
Theoretical Lab 2
CS61BL Summer 2025
CS 61BL Summer 2024
Announcements
CS 61BL Summer 2025
CS 61BL Summer 2024
Content Review (Static, ADTs)
CS 61BL Summer 2025
CS 61BL Summer 2024
Structure of a Class
public class CS61BStudent { // Class Declaration
public int idNumber; // Instance Variables
public int grade;
public static String instructor = “Hug”; // Class (Static) Variables
public CS61BStudent (int id) { // Constructor
this.idNumber = id; // this refers to the instance of the CS61BStudent we are in
this.grade = 100;
}
public void watchLecture() { // Instance Method
...
}
public static String getInstructor() { // Class (Static) Method
...
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
Instantiating Classes
public class CS61BLauncher {
public static void main(String[] args) {
CS61BStudent studentOne; // Declare a new variable of class CS61BStudent
studentOne = new CS61BStudent(32259); // Instantiate and assign to our new instance
CS61BStudent studentTwo = new CS61BStudent(19234); // Both at once
studentOne.watchLecture(); // Instance methods are called on instance
CS61BStudent.getInstructor(); // Static methods can be called on the class OR the
instance
CS61BStudent.watchLecture(); // Fails. Which student is watching lecture?
studentOne.getInstructor(); // Works, though is seen as bad practice.
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
this vs. static
CS 61BL Summer 2025
CS 61BL Summer 2024
Data Structures Review
List
ex. LinkedList, ArrayList
Map
ex. HashMap, TreeMap
Set
ex. HashSet, TreeSet
Array
ex. String[], int[]
CS 61BL Summer 2025
CS 61BL Summer 2024
A Few Data Structures APIs
List
LinkedList a = new LinkedList<Cat>();
// out of bounds error! The list is empty
Cat z = a.get(0);
// add to list
a.add(someCat);
// get 0th element of the list
Cat x = a.get(0);
Map
HashMap<String, Integer> b = new HashMap<>();
// add a key-value pair to the map
b.put(“hi”, 3);
// get the value for key “hi”
Integer y = b.get(“hi”);
Set
HashSet<Dog> c = new HashSet<Dog>();
// add to set
c.add(aDog);
// check if set contains aDog
boolean hasADog = c.contains(aDog);
String[] d = new String[50];
// k is null
String k = d[2];
// put a String into the array
d[32] = “hello”;
// only 50 positions in the array! Out of bounds error
String s = d[50];
Array
* note all the ways we can use <> when we initialize List/Map/Set
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q1)
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = __________________________________;
for (int i = _____; __________; _________) {
int newId = _______________;
Student newStudent = ____________________;
____________________________ = __________________________________;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = _____; __________; _________) {
int newId = _______________;
Student newStudent = ____________________;
____________________________ = __________________________________;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = _______________;
Student newStudent = ____________________;
____________________________ = __________________________________;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = ids.get(i);
Student newStudent = ____________________;
____________________________ = __________________________________;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = ids.get(i);
Student newStudent = new Student(newID);
____________________________ = __________________________________;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = ids.get(i);
Student newStudent = new Student(newID);
students[i] = newStudent;
__________________________________;
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = ids.get(i);
Student newStudent = new Student(newID);
students[i] = newStudent;
idToStudent.put(newID, newStudent);
}
__________________________________ = __________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1A Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
public CS61BL(int capacity, List<Integer> ids, String semester) {
students = new Student[capacity];
for (int i = 0; i < capacity; i++) {
int newId = ids.get(i);
Student newStudent = new Student(newId);
students[i] = newStudent;
idToStudent.put(newID, newStudent);
}
this.semester = semester;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
public static String university = “UC Berkeley”;
public String semester;
public Student[] students;
public Map<Integer, Student> idToStudent = new HashMap<>();
// constructor
...
// methods here
}
Add the following methods to the class:
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
...
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (Student student : students) {
student.watchLecture();
}
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (Student student : students) {
student.watchLecture();
}
}
public int updateGrade(Integer sid, int points) {
...
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (Student student : students) {
student.watchLecture();
}
}
public int updateGrade(Integer sid, int points) {
Student student = idToStudent.get(sid);
student.grade += points;
return student.grade;
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (Student student : students) {
student.watchLecture();
}
}
public int updateGrade(Integer sid, int points) {
Student student = idToStudent.get(sid);
student.grade += points;
return student.grade;
}
public static void changeUniversity(String newUniversity) {
...
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1B Welcome (back) to CS61BL
public class CS61BL {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (Student student : students) {
student.watchLecture();
}
}
public int updateGrade(Integer sid, int points) {
Student student = idToStudent.get(sid);
student.grade += points;
return student.grade;
}
public static void changeUniversity(String newUniversity) {
university = newUniversity;
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
1C Welcome (back) to CS61BL (Extra)
idToStudent associates a student ID with a Student. Studentalso has other variables like grade and instructor. Would it make sense for us to also have mappings from these attributes to a Student?
CS 61BL Summer 2025
CS 61BL Summer 2024
1C Welcome (back) to CS61BL (Extra)
idToStudent associates a student ID with a Student. Student also has other variables like grade and instructor. Would it make sense for us to also have mappings from these attributes to a Student?
Not really: A map requires a unique key to look up some value, but grade and instructor aren’t good examples of unique keys (ie. what if two students both had a grade of 90? Which Student should 90 map to? Likewise, every Student shares the same instructor because it is a static variable, so the mapping isn’t clear).
CS 61BL Summer 2025
CS 61BL Summer 2024
Content Review (GRoE, Box & Pointer Diagrams)
CS 61BL Summer 2025
CS 61BL Summer 2024
GRoE: Golden Rule of Equals
“Given variables y and x:
y = x copies all the bits from x into y.”
Java is pass-by-value: when you call a function and give it some arguments, the function called receives an exact copy of those arguments, tied to its own local variables.
“Copies all the bits” means different things for primitive vs. reference types.
CS 61BL Summer 2025
CS 61BL Summer 2024
Primitive vs. Reference Types
Examples: byte, short, int, long, float, double, boolean, char
Examples: Strings, Arrays, Linked Lists, Dogs, etc.
CS 61BL Summer 2025
CS 61BL Summer 2024
Back to the GRoE
“Given variables y and x:
y = x copies all the bits from x into y.”
CS 61BL Summer 2025
CS 61BL Summer 2024
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
1
2
3
5
5
x
arr
CS 61BL Summer 2025
CS 61BL Summer 2024
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
doSomething(x, arr);
...
public void doSomething(int y, int[] other) {
y = 9;
other[2] = 4;
}
1
2
3
5
5
x
arr
5
y
other
CS 61BL Summer 2025
CS 61BL Summer 2024
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
doSomething(x, arr);
...
public void doSomething(int y, int[] other) {
y = 9;
other[2] = 4;
}
1
2
4
5
5
x
arr
9
y
other
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q2)
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
Write what would be printed after the main method is executed.
Java visualizer: https://tinyurl.com/48uk72kc
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
Party size: 2
Pikachu 17 Ash
Pikachu 18 Team Rocket
Pikachu 18 Cynthia
Java visualizer: https://tinyurl.com/48uk72kc
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
On line 28, is level:
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
On line 28, is level:
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
If we were to call Pokemon.printStats() at the end of our main method, what would happen?
CS 61BL Summer 2025
CS 61BL Summer 2024
2 Static Electricity
Error!
CS 61BL Summer 2025
CS 61BL Summer 2024
Content Review (Linked Lists)
CS 61BL Summer 2025
CS 61BL Summer 2024
Linked Lists
Linked Lists are modular lists that are made up of nodes that each contain a value and a pointer to the next node. To access values in a Linked List, you must use dot notation.
Example: intList.get(2)
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q3)
CS 61BL Summer 2025
CS 61BL Summer 2024
3 Cardinal Directions
Link to Java Visualizer: https://tinyurl.com/BPCardinalDirections
Draw out the resulting diagram after executing all the lines.
CS 61BL Summer 2025
CS 61BL Summer 2024
Break (If Time)
CS 61BL Summer 2025
CS 61BL Summer 2024
Content Review (Interfaces)
CS 61BL Summer 2025
CS 61BL Summer 2024
Classes
Subclasses (or child classes) are classes that inherit from another class. This means that they have access to all of the functions and variables of their parent class in addition to any functions and variables defined in the child class.
Example: Corgi, Pitbull
Superclasses or parent classes are classes that are inherited by another class.
Example: Dog
Dog
Corgi
Pitbull
CS 61BL Summer 2025
CS 61BL Summer 2024
Fun with Methods
Method Overloading is done when there are multiple methods with the same name, but different parameters.
public void barkAt(Dog d) { System.out.print(“Woof, it’s another dog!”); }
public void barkAt(CS61BStaff s) { System.out.print(“Woof, what is this?”); }
Method Overriding is done when a subclass has a method with the exact same function signature as a method in its superclass. It is usually marked with the @Override tag.
In Dog class:
public void speak() { System.out.print(“Woof, I’m a dog!”); }
In Corgi Class, which inherits from Dog:
@Override
public void speak() { System.out.print(“Woof, I’m a corgi!”); }
CS 61BL Summer 2025
CS 61BL Summer 2024
Interfaces
Interfaces are implemented by classes. They describe a narrow ability that can apply to many classes that may or may not be related to one another.
They do not usually implement the methods they specify, but can do so with the default keyword. Interface methods are inherently public, which must be specified in the subclass that implements them (subclasses must override and implement non-default interface methods ).
Interfaces cannot be instantiated. (ie. Friendly f = new Friendly(); does not compile)
Dog
Cute
Friendly
CS61BStaff
CS 61BL Summer 2025
CS 61BL Summer 2024
Abstract Classes
Abstract classes are extended by classes. By default, method implementations are provided, but methods can be denoted abstract. Any classes that extend an abstract class must then implement that abstract method.
Abstract classes cannot be instantiated.
CS 61BL Summer 2025
CS 61BL Summer 2024
Interfaces vs. Classes
CS 61BL Summer 2025
CS 61BL Summer 2024
Static vs. Dynamic Type
A variable’s static type is specified at declaration, whereas its dynamic type is specified at instantiation (e.g. when using new).
Dog d = new Corgi();
Static type of d is Dog
Dynamic type of d is Corgi
Though interfaces cannot be instantiated, they can be static types (ie. Cute c = new Corgi();)
CS 61BL Summer 2025
CS 61BL Summer 2024
Casting
Casting allows us to tell the compiler to treat the static type of some variable as whatever we want it to be (need to have a superclass/subclass relationship). If the cast is valid, for that line only we will treat the static type of the casted variable to be whatever we casted it to.
Casting Checks:
Compile Time: Does the cast type have an inheritance relationship with the static type? (Can’t be sibling classes)
Runtime: Is the dynamic type a subclass (or the same) as the cast type?
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q4)
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
Object[] shapes = new Object[3];
shapes[0] = new Rectangle(3, 4);
shapes[1] = new Circle(5);
shapes[2] = new Triangle(3, 4, 5);
for (Object shape : shapes) {
System.out.println(shape.getArea() / shape.getPerimeter());
}
For the first iteration of the loop, the variable shape in the for-each loop has static type ________________
and dynamic type ________________________.
Object
Rectangle
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
Object[] shapes = new Object[3];
shapes[0] = new Rectangle(3, 4);
shapes[1] = new Circle(5);
shapes[2] = new Triangle(3, 4, 5);
for (Object shape : shapes) {
System.out.println(shape.getArea() / shape.getPerimeter());
}
Will this code compile? If not, why not, and how to fix it?
(Hint: it can require declaring something new and making changes to the provided code.)
We can create an interface called Shape that has the methods getArea() and
getPerimeter() and have all the shape classes implement this interface. Then, we can do… (see next page)
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
Shape[] shapes = new Shape[3];
shapes[0] = new Rectangle(3, 4);
shapes[1] = new Circle(5);
shapes[2] = new Triangle(3, 4, 5);
for (Shape shape : shapes) {
System.out.println(shape.getArea() / shape.getPerimeter());
}
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
Shape s1 = new Shape();
Triangle t1 = new Triangle(3, 4, 5);
Shape s2 = new Circle(5);
Rectangle r1 = new Square(5);
Square sq1 = new Rectangle(10, 10);
Rectangle r2 = (Shape) new Square(6);
Rectangle r3 = (Rectangle) new Square(6);
Rectangle r4 = (Rectangle) new Circle(6);
Square sq2 = (Square) new Rectangle(6, 6);
CE, cannot instantiate an interface
Static: Triangle, Dynamic: Triangle
Static: Shape, Dynamic: Circle
Static: Rectangle, Dynamic: Square
CE, a Rectangle is not always a Square
CE, a Shape is not always a Rectangle
Static: Rectangle, Dynamic: Square
CE
Static: Square, Then RE
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter();
s2.getArea();
r1.getInscribedCircle();
((Square) r1).getInscribedCircle();
((Circle) r1).getDiameter();
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea();
r1.getInscribedCircle();
((Square) r1).getInscribedCircle();
((Circle) r1).getDiameter();
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
t1 has static type Triangle and it indeed has getPerimeter(), so compile time checking passes.
It has dynamic type Triangle, so the method getPerimeter() from the Triangle class is called.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle();
((Square) r1).getInscribedCircle();
((Circle) r1).getDiameter();
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
s2 has static type Shape and it indeed has getArea(), so compile time checking passes.
It has dynamic type Circle, so during runtime, so the overridden getArea() from the Circle class is called.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle();
((Circle) r1).getDiameter();
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
r1 has static type Rectangle, but it does not have the getInscribedCircle() method. The compiler stops you there.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle(); // Creating a circle
((Circle) r1).getDiameter();
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
The static type of r1 is Rectangle, and casting it to a Square is allowed at compile time. Now, the static type of the calling instance is Square, and it indeed has getInscribedCircle(), so compile time checking passes.
At runtime, r1 has dynamic type Square, so the method getInscribedCircle() from the Square class is called.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle(); // Creating a circle
((Circle) r1).getDiameter(); // CE
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
r1 has static type Rectangle, and casting it to a Circle is not allowed, because they are siblings.
The compiler stops you there.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle(); // Creating a circle
((Circle) r1).getDiameter(); // CE
Rectangle r5 = new Rectangle(3, 4);
((Square) r5).getArea();
sq1 has static type Square, and casting it to a Rectangle is allowed at compile time. The Rectangle class indeed has getPerimeter(), so compile time checks pass.
The dynamic type is Square, so the overridden method getPerimeter() from the Square class is called.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle(); // Creating a circle
((Circle) r1).getDiameter(); // CE
Rectangle r5 = new Rectangle(3, 4); // Nothing
((Square) r5).getArea();
This line doesn’t have any output and doesn’t throw any errors. It creates a new Rectangle object with length 3 and width 4.
CS 61BL Summer 2025
CS 61BL Summer 2024
4 Circles, Squares, and Triangles
t1.getPerimeter(); // Triangle's perimeter
s2.getArea(); // Circle's area
r1.getInscribedCircle(); // CE
((Square) r1).getInscribedCircle(); // Creating a circle
((Circle) r1).getDiameter(); // CE
Rectangle r5 = new Rectangle(3, 4); // Nothing
((Square) r5).getArea(); // RE
The static type of r5 is Rectangle, and casting it to a Square is allowed at compile time.
However, the dynamic type of r5 is still a Rectangle, so a runtime error occurs.
CS 61BL Summer 2025
CS 61BL Summer 2024
Content Review (Iterators, Comparators)
CS 61BL Summer 2025
CS 61BL Summer 2024
Subtype Polymorphism
Polymorphism in programming describes the ability for methods to work on a variety of types. This gives us more general code, or in other words, a single uniform interface that can work with many types.
Subtype polymorphism describes the fact that subtypes of a Class or Interface are also instances of that Class or Interface. Any method that takes in the parent type will take in an instance of the subtype.
CS 61BL Summer 2025
CS 61BL Summer 2024
Example: Comparators
Comparators are an example of how subtype polymorphism is commonly used.
public interface Comparator<T> {
int compare(T o1, T o2);
}
The Comparator interface’s compare function takes in two objects of the same type and outputs:
An object is determined to be “less than” or “greater than” or “equal to” another object based on how a class that implements Comparator fills in its own compare. This allows us to compare things that aren’t inherently numerical (ie. Dogs)
CS 61BL Summer 2025
CS 61BL Summer 2024
The Iterator & Iterable Interfaces
Iterators are objects that can be iterated through in Java (in some sort of loop).
public interface Iterator<T> {
boolean hasNext();
T next();
}
Iterables are objects that can produce an iterator.
public interface Iterable<T> {
Iterator<T> iterator();
}
You might have seen syntax like
for (String x : lstOfStrings) // Lists, Sets, Arrays are all Iterable!
which is shorthand for
for (Iterator<String> iter = lstOfStrings.iterator(); iter.hasNext();) {
String x = iter.next();
}
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q5)
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHRequestComparator implements Comparator<_____________________________> {
@Override
public int compare(___________________________ s1, ________________________ s2) {
// feel free to define variables here for readability if you'd like
if (_______________________________________) {
return -1;
} else if (_______________________________________) {
return 1;
} else if (_______________________________________) {
return -1;
} else if (_______________________________________) {
return 1;
}
return 0;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHRequestComparator implements Comparator<OHRequest> {
@Override
public int compare(OHRequest s1, OHRequest s2) {
// feel free to define variables here for readability if you'd like
if (_______________________________________) {
return -1;
} else if (_______________________________________) {
return 1;
} else if (_______________________________________) {
return -1;
} else if (_______________________________________) {
return 1;
}
return 0;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHRequestComparator implements Comparator<OHRequest> {
@Override
public int compare(OHRequest s1, OHRequest s2) {
// feel free to define variables here for readability if you'd like
if (s1.isSetup == true && s2.isSetup == false) {
return -1;
} else if (s1.isSetup == false && s2.isSetup == true) {
return 1;
} else if (_______________________________________) {
return -1;
} else if (_______________________________________) {
return 1;
}
return 0;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHRequestComparator implements Comparator<OHRequest> {
@Override
public int compare(OHRequest s1, OHRequest s2) {
// feel free to define variables here for readability if you'd like
if (s1.isSetup == true && s2.isSetup == false) {
return -1;
} else if (s1.isSetup == false && s2.isSetup == true) {
return 1;
} else if (s1.description.equals("setup") && !s2.description.equals(“setup”)) {
return -1;
} else if (!s1.description.equals("setup") && s2.description.equals(“setup”)) {
return 1;
}
return 0;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator _________________ {
OHRequest curr;
public OHIterator(OHRequest queue) {
_________________;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
}
@Override
public boolean hasNext() {
while (__________________) {
______________________________;
}
if (_______________________________) {
______________________________;
}
____________________________________;
}
@Override
public OHRequest next() {
if (___________________________) {
________________________;
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
_________________;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
}
@Override
public boolean hasNext() {
while (__________________) {
______________________________;
}
if (_______________________________) {
______________________________;
}
____________________________________;
}
@Override
public OHRequest next() {
if (___________________________) {
________________________;
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
curr = queue;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
@Override
public boolean hasNext() {
while (__________________) {
______________________________;
}
if (_______________________________) {
______________________________;
}
____________________________________;
}
@Override
public OHRequest next() {
if (___________________________) {
________________________;
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
curr = queue;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
@Override
public boolean hasNext() {
while (curr != null && !isGood(curr.Description)) {
curr = curr.next;
}
if (_______________________________) {
______________________________;
}
____________________________________;
}
@Override
public OHRequest next() {
if (___________________________) {
________________________;
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
curr = queue;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
@Override
public boolean hasNext() {
while (curr != null && !isGood(curr.Description)) {
curr = curr.next;
}
if (curr == null) {
return false;
}
return true;
}
@Override
public OHRequest next() {
if (___________________________) {
________________________;
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
curr = queue;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
@Override
public boolean hasNext() {
while (curr != null && !isGood(curr.Description)) {
curr = curr.next;
}
if (curr == null) {
return false;
}
return true;
}
@Override
public OHRequest next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
____________________________________;
____________________________________;
____________________________________;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHIterator implements Iterator<OHRequest> {
OHRequest curr;
public OHIterator(OHRequest queue) {
curr = queue;
}
public boolean isGood(String description) {
return description.length() >= 5;
}
@Override
public boolean hasNext() {
while (curr != null && !isGood(curr.Description)) {
curr = curr.next;
}
if (curr == null) {
return false;
}
return true;
}
@Override
public OHRequest next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
OHRequest currRequest = curr;
curr = curr.next;
return currRequest;
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHQueue _________________________________ {
private OHRequest queue;
public OHQueue(OHRequest queue) {
_________________________________;
}
@Override
______________ ______________ _________________ {
_________________________________;
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHQueue implements Iterable<OHRequest> {
private OHRequest queue;
public OHQueue(OHRequest queue) {
_________________________________;
}
@Override
______________ ______________ _________________ {
_________________________________;
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHQueue implements Iterable<OHRequest> {
private OHRequest queue;
public OHQueue(OHRequest queue) {
this.queue = queue;
}
@Override
______________ ______________ _________________ {
_________________________________;
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
5 OHQueue
public class OHQueue implements Iterable<OHRequest> {
private OHRequest queue;
public OHQueue(OHRequest queue) {
this.queue = queue;
}
@Override
public Iterator<OHRequest> iterator() {
return new OHIterator(queue);
}
}
}
CS 61BL Summer 2025
CS 61BL Summer 2024
Worksheet (Q6)
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
Why do we use a helper method here? Why can’t we just have the signature for gridify also have a pointer to the curr node, such that the user of the function passes in the sentinel each time?
CS 61BL Summer 2025
CS 61BL Summer 2024
6 Gridify Extra, Exam-Level Review Question from the Previous Week
Why do we use a helper method here? Why can’t we just have the signature for gridify also have a pointer to the curr node, such that the user of the function passes in the sentinel each time?
CS 61BL Summer 2025
CS 61BL Summer 2024
https://bit.ly/3ZRO8VM
CS 61BL Summer 2025
CS 61BL Summer 2024