Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting
and add them to your project/directory.
Remember to put the data files in the appropriate place (e.g., in the
project directory in Eclipse).
robotics.jar contains
both Direction.class
and Robot.class
, which are
in the robotics
package;
ArrayColorFinder.java
.
colors
?
colors[0]
?
names
?
colors
?
Color
objects have been instantiated?
Color
objects?
Color
objects to elements
of the array colors
?
names
and colors
?
Driver
.
What output is generated when you enter "snow"?
List
interface and the ArrayList
class.
ListColorFinder
and copy the code from ArrayColorFinder
into it
(changing the name of the constructor appropriately).
colors
and names
so that they are
both List
objects. (Do not parameterize
them!) What changes did you make?
import
something in order
to use the List
class. What line do you have
to add to the top of your class as a result?
colors
and names
objects so that they are now ArrayList
objects.
What changes did you make?
List
objects but
instantiate them as ArrayList
objects>?
List
objects rather than ArrayList
objects>?
List
objects
rather than arrays? (Hint: It should be immediately apparent from the change
you just made.)
for
loop that
store the names and colors. (Hint: Look at the documentation for
the ArrayList
class.)
What changes did you make?
for
in getColor()
. (Hint: Look at the documentation for
the ArrayList
class.) What changes
did you make?
ListColorFinder
class.
What error messages were generated?
get()
method calls.
What changes did you make?
ListColorFinder
class. The compiler
still generates at least one "Note/Warning" when you compile the
class. What "Note/Warning" is generated?
colors
so that it is
a List
of Color
objects. What
change did you make?
names
so
that it is a List
of String
objects.
What change did you make?
colors
and names
objects.
What changes did you make?
getColor()
method so that it no longer
casts the objects returned by the calls to
get()
.
What changes did you make?
Map
interface and some of the
benefits of maps. Since you understand type-safety now, there's no reason
to start with a version that isn't type-safe.
MapColorFinder
and copy the code from ArrayColorFinder
into it
(changing the name of the constructor appropriately).
names
object.
colors
object so that it is
now a Map
with a key of type String
and a value of type Color
. (This is often described as
a Map
from String
to Color
.)
What code did you change?
colors
object as a HashMap
from String
to Color
.
What code did you add?
key
and value
with a call to
the appropriate method in the HashMap
class.
What change did you make?
getColor()
method in the
ColorFinder
class so that it now uses the
HashMap
named colors
. (Hint: Look at
the documentation for the HashMap
class.)
What code did you add?
Map
objects as compared to
List
objects. (Hint: It should be
immediately apparent from the change you just made.)
Bender
and Driver1
classes.
Robot
class includes the following methods:
protected boolean canMove(Direction d)
protected void move(Direction d)
protected boolean haveBeen(Direction d)
Complete the execute()
method in
the Bender
class using the above methods from
the Robot
class in such a way that it is consistent
with the comments describing it. (Note: You need not use
the haveBeen()
method.) Do not use
an Iterator
or a for-each
loop.
What code did you add?
Driver1
.
execute()
method so that it uses
a for-each
loop. What is your execute()
method now?
Driver1
.
execute()
method so that it uses an
Iterator
.
What is your execute()
method now?
Driver1
.
Driver2
class and the
data file box.txt
.
Bender
class and complete it. (For this question you should
assume that there are four directions: Direction.FORWARD
,
Direction.RIGHT
, Direction.BACKWARD
, and
Direction.LEFT
.)
* Read instructions from a file * * @param name The name of the file * @return An ArrayList of Direction objects */ public List<Direction> read(String name) { }What code did you add?
Driver2
.
Direction
class actually includes
Direction.CONTINUE
. Add a line to the read()
method that accounts for this Direction
.
What code did you add?
execute()
method (the version that uses
the Iterator pattern) so that it handles
Direction.CONTINUE
. Specifically, when a
Direction.CONTINUE
is encountered the execute()
method must get the next element in the method
collection and then
repeatedly move the Robot
in that direction until
it can no longer do so.
What code did you add?
Driver3
.
Bender
class:
private Map<String, List<Direction>> library;
Make sure you understand this declaration.
library
attribute in the constructor
of the Bender
class.
What code did you add?
Bender
class and complete it:
/** * Add a method to the existing library * * @param method The method to add */ public void add(String name, List<Direction> method) { }What code did you add?
Bender
class and complete it:
/** * Execute a named set of instructions from the library. * Specifically, retrieve a named set of instructions from * the library and then call the other version of * execute(). * * @param name The name of the method */ public void execute(String name) { }
What code did you add?
Driver3
.
Copyright 2024