Linked Lists
> Click or hit Control-Enter to run Example.main above
No Professor Of The Day Award For Me
(At least not today.)
Lecture Participation Reminder
Lecture participation scores will be up later today. Some of you will be unhappy.
Our participation tracking algorithm is very generous. But, to earn participation credit, you need:
-
To be in the right place. No points for attending the wrong lecture.
-
To be following along with the slides. No points for just being logged in.
-
Questions?
Cheating
I take academic integrity extremely seriously. Cheaters will be caught and punished.
We’ve begun checking submissions for plagiarism.
-
99% of you are fine. Thanks for being honest and working hard.
-
1% of you are not fine. You’ll be hearing from us.
Course Policies Questions?
We have good reasons for the choices we’ve made about how the course is set up. But there are always tradeoffs. I’m happy to discuss.
Remainder of the Semester
In happier news, here’s what we have on tap for the rest of the semester.
-
MP5: out Friday, due after Spring Break.
-
After break: MP6 and MP7, one week MPs on Android development and API usage.
-
2.5 week final project, done in pairs, due in the last lab.
-
CS 125 Fair: held on Reading Day, optional (but worth 1% extra credit), highlights chosen in lab.
Q6 Review: Terminology
class Parent { }
class Child extends Parent { }
In Java, any method that will accept an instance of Parent
will also accept an
instance of Child
. This is due to…
Q6 Review: Object Casting
public class Top {
public static void printOne(Top top) {
Bottom bottom = (Bottom) top;
bottom.print();
}
public void print() {
System.out.println("Top");
}
}
public class Bottom extends Top {
public void print() {
System.out.println("Bottom");
}
}
public class Example {
public static void main(String[] unused) {
Top top = new Top();
Top.printOne(top);
}
}
When the main
method runs, what will be printed?
> Click or hit Control-Enter to run Example.main above
Q6 Review: References
public class Car {
public int odometer;
public Car(int initialOdometer) {
odometer = initialOdometer;
}
public int increaseMileage() {
return odometer++;
}
}
public class Example {
public static void main(String[] unused) {
Car[] cars = new Car[10];
Car car = new Car(10);
for (int i = 0; i < cars.length; i++) {
cars[i] = car;
}
for (int i = 0; i < cars.length; i++) {
cars[i].increaseMileage();
}
System.out.println(cars[0].odometer);
}
}
When the main
method runs, what will be printed?
> Click or hit Control-Enter to run Example.main above
Q6 Review: Polymorphism
public class Shape {
public String toString() {
return "Shape";
}
}
public class FourSides extends Shape {
public String toString() {
return "FourSides";
}
}
public class Example {
public static void main(String[] unused) {
FourSides s = new Shape();
System.out.println(s);
}
}
When the main
method above runs, what will be printed?
> Click or hit Control-Enter to run Example.main above
Q6 Review: Garbage Collection
public class Example {
public static void main(String[] unused) {
String[] strings = new String[10];
for (int i = 0; ; i++) {
strings[i % 10] = new String();
}
}
}
After the code above runs for a while, how many valid String
objects will
exist in the system?
Linked Lists
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
this.value = setValue;
this.next = setNext;
}
}
Linked Lists
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
this.value = setValue;
this.next = setNext;
}
}
Item items = new Item(0, null);
Linked Lists
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
this.value = setValue;
this.next = setNext;
}
}
Item items = new Item(0, null);
items = new Item(8, items);
Linked Lists
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
this.value = setValue;
this.next = setNext;
}
}
Item items = new Item(0, null);
items = new Item(8, items);
items = new Item(5, items);
Linked Lists: Iteration
public class LinkedList {
private Item start;
}
public class Item {
public int value;
public Item next;
}
We can iterate through our LinkedList
using a for
loop.
> Click or hit Control-Enter to run Example.main above
Announcements
-
MP4 is due Friday.
-
We’ve added an anonymous feedback form to the course website. Use it to give us feedback!
-
Continue to communicate with the course staff about the strike as needed. We’re trying to keep everything up and running.
-
My office hours continue today at 11AM in the lounge outside of Siebel 0226.