cs230 Lecture Notes Week 8 Tuesday Circular buffer --------------- For now we are implementors. In two assignments we are going to use the queue implementation. Ideally, we should write and debug it now, and then in two weeks we can just use it as if it were built in. Sorry about the confusion in the book: everywhere the variable last appears, replace it with next. The instance vars of the Queue are first: the head of the queue next: the next available space in the queue Special case, if first == next, the queue is empty. What happens when first reaches the end of the array? Does that mean the queue is full? Not necessarily -- we can wrap around to the beginning. Modulus magic ------------- The modulus operator performs clock arithmetic. (Also useful for checking divisibility and extracting the rightmost digit of a number.) Circular buffer resumed ----------------------- So how do we know when the buffer is full? What do we do when that happens? Process objects one more time ----------------------------- Look at Homework 5 solutions Balance = client code = Test or Calculator Checker = process object = Evaluator Goals: 1) hide implementation details from client 2) all methods in Evaluator should be able to access the state Solution: 1) use instance variables to store state Accidental benefit: we can have multiple processes going on at the same time. Client view of a process object: 1) create it by invoking new 2) drive the process by invoking methods Examples: StringTokenizer: initialize it with a string, invoke hasMoreTokens and nextToken Checker: initialize with nothing, invoke checkLine and endOfFile Evaluator: initialize with the expression, invoke evaluate (all other methods can be private, since the client doesn't have to invoke them)