CS计算机代考程序代写 Java CS 61B Iterators and Iterables Spring 2021 Discussion 5: February 15, 2021

CS 61B Iterators and Iterables Spring 2021 Discussion 5: February 15, 2021
1
1.
2.
3.
Iterators Warmup
If we were to define a class that implements the interface Iterable, what method(s) would this class need to define? Write the function signature(s) below.
If we were to define a class that implements the interface Iterator, what method(s) would this class need to define? Write the function signature(s) below.
What¡¯s one difference between Iterator and Iterable?

1 2 3 4 5 6 7 8 9
10 11
2 Iterators and Iterables 2 OHQueue
The goal for this question is to create an iterable Office Hours queue. We¡¯ll do so step by step.
The code below for OHRequest represents a single request. Like an IntNode, it has a reference to the next
request. description and name contain the description of the bug and name of the person on the queue.
public class OHRequest { public String description; public String name;
public OHRequest next;
public OHRequest(String description, String name, OHRequest next) { this.description = description;
this.name = name;
this.next = next;
} }
First, let¡¯s define an iterator. Create a class OHIterator that implements an iterator over OHRequest objects that only returns requests with good descriptions. Our OHIterator¡¯s constructor will take in an OHRequest object that represents the first OHRequest object on the queue. We¡¯ve provided a function, isGood, that accepts a description and says if the description is good or not. If we run out of office hour requests, we should throw a NoSuchElementException when our iterator tries to get another request.
import java.util.Iterator;
public class OHIterator __________________________________________ {
OHRequest curr;
public OHIterator(OHRequest queue) { }
public boolean isGood(String description) {
return description != null && description.length() > 5;
}
}

Now, define a class OHQueue. We want our OHQueue to be iterable, so that we can process OHRequest objects with good descriptions. Our constructor will take in an OHRequest object representing the first request on the queue.
import java.util.Iterator;
public class OHQueue __________________________________ {
public OHQueue (OHRequest queue) { }
}
Fill in the main method below so that you make a new OHQueue object and print the names of people
with good descriptions. Note : the main method is part of the OHQueue class. public class OHQueue … {
….
public static void main(String [] args) {
OHRequest s5 = new OHRequest(“I deleted all of my files”, “Allyson”, null); OHRequest s4 = new OHRequest(“conceptual: what is Java”, “Omar”, s5);
OHRequest s3 = new OHRequest(“git: I never did lab 1”, “Connor”, s4);
OHRequest s2 = new OHRequest(“help”, “Hug”, s3);
OHRequest s1 = new OHRequest(“no I haven’t tried stepping through”, “Itai”, s2);
for (_____________ : ________________) {
} }
Iterators and Iterables 3

4 Iterators and Iterables 3 Thank u, next
Now that we have our OHQueue from problem 2, we¡¯d like to add some functionality. We¡¯ve noticed a bug in our office hours system: whenever a ticket¡¯s description contains the words ¡°thank u¡±, that ticket is put on the queue twice. To combat this, we¡¯d like to define a new iterator, TYIterator.
If the current item¡¯s description contains the words ¡°thank u,¡± it should skip the next item on the queue, because we know the next item is an accidental duplicate from our buggy system. As an example, if there were 4 OHRequest objects on the queue with descriptions [“thank u”, “thank u”, “im bored”, “help me”], calls to next() should return the 0th, 2nd, and 3rd OHRequest objects on the queue. Note: we are still enforcing good descriptions on the queue as well!
Hint – To check if a description contains the words ¡°thank u¡±, you can use:
curr.description.contains(“thank u”)
public class TYIterator extends ______________________________________ {
public TYIterator(OHRequest queue) { }
}

4 Senior Class Extra
For each line in the main method of our testPeople class, if something is printed, write it next to the line. If the line results in an error, write next it whether it is a compile time error or runtime error, and then proceed as if that line were not there.
public class Person { public String name; public int age;
1
2
3
4
5
6
7 8} 9
public Person(String name, int age) { this.name = name;
this.age = age;
10 public void greet(Person other) {System.out.println(“Hello, ” + other.name);}
11 }
12 13
14 public class Grandma extends Person { 15
16 public Grandma(String name, int age) {
17 super(name, age);
18 }
19
20 @Override
21 public void greet(Person other) {System.out.println(“Hello, young whippersnapper”);}
22
23 public void greet(Grandma other) {System.out.println(“How was bingo, ” + other.name + “?”);}
24 }
25
26 public class testPeople {
27 public static void main(String[] args) {
28 Person n = new Person(“Neil”, 12);
29 Person a = new Grandma(“Ada”, 60);
30 Grandma v = new Grandma(“Vidya”, 80);
31 Grandma al = new Person(“Alex”, 70);
32 n.greet(a);
33 n.greet(v);
34 v.greet(a);
35 v.greet((Grandma) a);
36 a.greet(n);
37 a.greet(v);
38 ((Grandma) a).greet(v);
39 ((Grandma) n).greet(v);
40 }
41 }
Iterators and Iterables 5