Comp 1510 Lab 10
Comp 1510 Lab 10
1. Open Eclipse. Remember to keep all your projects in the same workspace. Think of a
workspace as a shelf, and each project is a binder of stuff on the shelf. If you need some
help organizing your workspace, we’d be happy to help!
Create a new Java project. Call it something like COMP 1510 Lab 10.
2. Complete the following tasks. Remember you can right-click the src package in the
Eclipse package explorer project to quickly create a new Java class or interface.
3. When you have completed the exercises, show them to your lab instructor. Be
prepared to answer some questions about your code and the choices you made.
Table of Contents
Let’s get started! 1
What will you DO in this lab? 1
1. Common Java interfaces 1
2. Let’s implement Comparable and use it to sort a list of Names 2
3. A simple robot that walks randomly 3
4. Application: simulating a drunk walker 5
5. Application: Collisions! 5
6. Telephone 6
7. You’re done! Show your lab instructor your work. 6
1 of 6
COMP 1510 Bruce Link, Keith Tang, Carly Orr, Arron Ferguson, Chris Thompson
INTERESTING FACTOID: a lambda expression can be used anywhere a functional interface can
be used. A functional interface is an interface with one method. The EventHandler interface is
an example of a functional interface.
4. We’ve also encountered the Comparable interface in an indirect way. The String
class implements the Comparable interface. In fact many classes from the Java
library implement the Comparable interface
docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html .
2 of 6
COMP 1510 Bruce Link, Keith Tang, Carly Orr, Arron Ferguson, Chris Thompson
integer, zero, or a positive integer if this object is less than, equal to, or greater than
the object specified as the parameter. We want to compare names or sort names
alphabetically, so we can use the Strings’s compareTo method. A good algorithm
could be something like this:
a. Compare the two last names. If they’re not the same, return the value
returned when you invoke the compareTo method on the last name instance
variable, passing the Name parameter’s last name as the parameter.
b. If the two last names are the same, compare the two middle names. If
they’re not the same, return the value returned when you invoke the
compareTo method on the middle name instance variable, passing the Name
parameter’s middle name as the parameter.
c. If the two middle names are the same, the we must compare the two first
names. Use the same method described above to compare the Strings.
8. Create a Driver class with a main method.
9. In the main method, create some names, and add them to an ArrayList<Name>.
10. Here’s the neat part. Make sure you have some Kleenex because you will surely spill
your coffee when you see how amazing this next bit is.
11. The ArrayList class has a sort method. Usually we pass an instance of a class that
implements the Comparator interface. We don’t have to do this though if we are
storing stuff in the ArrayList that implements Comparable.
12. Invoke the sort method on your full ArrayList<Name>, and then print out the results.
13. You may now wipe up your spilled coffee.
A random walk is basically a sequence of steps in some (possibly enclosed) space where the
direction of each step is random. The walk terminates when a maximum number of steps has
taken place or when the random walker steps outside of the space.
Random walks can be used to simulate things like the movement of molecules and economic
phenomena like stock prices.
We will assume our RandomWalker is walking on a two-dimension square grid. In fact, let’s
assume our simulations takes place on a square grid with the point (0,0) at the center. The
boundary of the square will be a single integer that represents the maximum x- and y-
coordinate for the current position on the square (so for a boundary value of 10, both the x-
and y-coordinates can vary from -10 to 10, inclusive). Each step will be one unit up, one unit
down, one unit to the left, or one unit to the right. (No diagonal movement.)
1. Create a new class called RandomWalker. It should have the following instance
variables:
i. The x coordinate of the current position
3 of 6
COMP 1510 Bruce Link, Keith Tang, Carly Orr, Arron Ferguson, Chris Thompson
4 of 6
COMP 1510 Bruce Link, Keith Tang, Carly Orr, Arron Ferguson, Chris Thompson
12. Update the takeStep method so that it modifies the maximumDistance instance variable
if necessary:
i. Start by adding a private support method to do this. The private support
method should be called private int max(int a, int b) and it should return the
maximum of a and b.
ii. Add code to takeStep to update maximumDistance. This can be done in a single
statement using the max method. The new value of maximumDistance will be
the maximum of either the current value of maximumDistance, or the current
distance to the origin. Note that if the current location of the RandomWalker is
(-3, 15) the distance to the origin is 15; if the current point is (-10, 7) the distance
to the origin is 10. Remember that Math.abs returns the absolute value of a
number.
13. Add an accessor method to RandomWalker for the maximumDistance.
14. In TestWalker, add statements to print out the maximum distance in the loop. Does it
work correctly?
1. Ask the user for the boundary and the number of steps
2. Ask the user for the number of drunks (simulations) to run
3. Use a for loop to instantiate a RandomWalker to represent the drunk and ask it to walk.
If it falls out of bounds early, increment a counter.
4. After the loop, print out the total number of tests and the number of falls.
5. Application: Collisions!
This next program will simulate two particles in a space and how many times they collide, i.e.,
occupy the same coordinates. The goal of the program is to determine and report the number
of collisions. Your Collisions program should:
5 of 6
COMP 1510 Bruce Link, Keith Tang, Carly Orr, Arron Ferguson, Chris Thompson
6. Telephone
It’s time for some JavaFX fun. Flex your programming fingers. You must create a JavaFX
program that displays the number pad for a phone. It should look like a phone number pad
(hint: use a GridPane). There should be a total of 12 buttons for the numbers 0 through 9, and
don’t forget the buttons for * and #. When a button is pressed, its value should be displayed on
a label which is positioned above the number pad. Consider the following:
1. How can you use loops to create the Buttons on the pad?
2. Look for areas of duplicated code. We like to minimize the amount of duplicated code.
Do you need a separate listener for each button, or can you use the ActionEvent’s
getSource method (check out Chapter 5’s RedOrBlue.java example). Do we even need
to use getSource?
3. Does it make sense to use lambdas here? What about a class that implements the
EventHandler?
6 of 6