1 |
h11 |
CS56 M16 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 9am or 10:30am |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h11: More Livecoded Parser Review, and the Factory and State Patterns
ready? | assigned | due | points |
---|---|---|---|
true | Fri 07/15 09:00AM | Wed 07/20 09:30AM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)
Part of this homework concerns the parser which was livecoded in class, which is available online here: https://github.com/UCSB-CS56-M16/cs56-livecoded-parser.
The rest concerns HFDP Chapter 4 and HFDP Chapter 10. Please read those chapters, then answer these questions.
- (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (either 9am or 10:30am) in the space indicated. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
-
(12 pts) The code in the box to the right does not use final. Circle variable definitions which could be marked with final. Each correctly identified variable is worth three points.
public class Foo { public boolean b; public Foo(boolean b) { this.b = b; } public static void bar(boolean b) { Foo f = new Foo(b); f.b = !b; } }
-
Methods in the livecoded parser keep track of the current position in the input by passing around an int parameter.
That is, methods take an int position to start from and return an updated int position reflecting where to resume parsing the input from.
An alternative approach here is to have a single int instance variable which keeps track of the current position in the input. As parsing proceeds, this variable can be updated to reflect position changes.
- (10 pts) Name one advantage of the current approach, based on passing around the int parameter.
- (10 pts) Name one advantage of the alternative approach, based on mutating a single int instance variable.
-
(10 pts) A Java constructor, invoked with
new
, and a method of a Factory object (one created using the Factory design pattern) can both be used to create instances of new objects. What is the situation in which a Factory object is preferred to just instantiating a class withnew
? That is, what problem does the factory design pattern solve? -
(10 pts) Explain how the Factory design pattern and the Abstract Factory design pattern are different.
-
In HFDP chapter 10, the running example with the gumball machine initially uses an int to keep track of what state the machine is in, as opposed to using the state pattern.
- (8 pts) Name two advantages of applying the state pattern to this example
- (5 pts) Name one disadvantage of applying the state pattern to this example
-
(25 pts) The provided code for the full parser you are working with in lab05 is available here:
https://github.com/UCSB-CS56-M16/cs56-parsing-assignment. This code uses a variant of the state pattern in tokenization, specifically through theTokenizerState
interface.In the space below, draw out the finite state diagram corresponding to how this tokenizer works. States should be labeled with the name of the class which implements the state. Transitions between states should be labeled with the condition under which the transition occurs, which can be derived by looking at the implementations of the
nextState
method. For example, a possible transition is “curChar
is a digit”, or “curChar
is ‘q’”. Your diagram should show all possible transitions, including transitions which start and end in the same state (i.e., self-loops). For guidance on how this diagram should look overall, you may want to consult the diagram for the gumball machine near the end of chapter 10 in HFDP.