15-110 FALL 2009 (CORTINA)
HOMEWORK 6 - due Friday, October 30, 2009 in class
Please remember that homework must be handed in on time.
No late homework will be accepted.
Review the suggested readings of your textbook and
answer the following questions based on your textbook and the course
lecture material.
- (2 pts)
Consider the following method:
public static int mystery(int n) {
if (n == 0)
return 1;
return 2 * mystery(n-1);
}
- Explain why this method is recursive.
- What is the output of the following statement?
(Show your work to show
each call made to the mystery method.)
System.out.println(mystery(5));
- Describe the function this mystery method is computing.
- Write another method that computes the same function non-recursively
using a loop instead.
- (1.5 pts)
-
Complete the main method below that outputs the number of lines in the
file data.txt.
public static void main(String[] args) throws IOException {
Scanner filescan = new Scanner(new File("data.txt"));
// Complete this method:
}
- Why is throws IOException necessary here?
Use the following information and diagram for the remaining problems in
this homework assignment.
A simple pedometer stores the direction of its user as NORTH, EAST, SOUTH,
or WEST. In addition to the direction, the pedometer also stores the
total distance traveled in feet. When a pedometer is created, it is
initially referencing NORTH with a
distance traveled of 0 feet. The user of the pedometer can turn right 90
degrees, turn left 90 degrees, walk forward, or reset the unit. If the
user turns right or left, the direction changes based on the direction of
the turn. If the user walks forward, the distance traveled in feet
increases by the amount of feet walked. If the user resets the unit,
the direction is reset to NORTH and the distance traveled in feet is reset
to 0.
- (1.5 pts)
Consider a Java class named Pedometer that models the object
described above.
public class Pedometer {
// to be completed
}
-
Write variable definitions for the fields that are necessary to
represent a pedometer along with their data types as described
above.
-
Write a constructor for Pedometer that creates an object
of this class that
initially stores a direction of NORTH and a distance traveled of 0 feet.
-
Write simple accessors for each of the fields for a pedometer. Each
accessor should have no parameters and an appropriate return type.
- (2 pts)
Write the following mutator methods:
-
turnLeft - Changes the direction stored in the pedometer to the
direction that is 90 degrees counterclockwise from the current direction.
-
turnRight - Changes the direction stored in the pedometer to the
direction that is 90 degrees clockwise from the current direction.
-
walkForward - Changes the distance traveled in feet based on the
number of feet the user walks, given as a parameter. If the amount of feet
given is not positive, do not change the distance traveled and print a
warning message instead ("INVALID DISTANCE").
-
reset - Restores the pedometer to a direction of NORTH and a
distance traveled of 0 feet.
- (1 pt)
We also need some additional methods to complete our Java class.
-
Write a toString method for this pedometer that returns a string
containing the values of all of the fields of this object. Make sure you
use the correct signature for this method.
-
Write an equals method for this pedometer that requires a
parameter containing a reference to another object that is a another
pedometer. This method should return true
if this pedometer and the other pedometer have the same direction and same
distance traveled in feet, or false otherwise. Make sure you use the
correct signature for this method.
- (2 pts)
Write a main method that does the following:
- Create a pedometer.
- Using the pedometer you created,
walk forward a number of feet given by an integer entered at the
keyboard by the user.
(If the user enters a negative integer, this is ok
since the pedometer shouldn't change.)
- Using the pedometer you created,
turn left.
- Using the pedometer you created,
walk forward a number of feet given by an integer entered at the
keyboard by the user.
(If the user enters a negative number, this is ok
since the pedometer shouldn't change.)
- Using the pedometer you created,
turn right.
- If the pedometer has a total distance of more than 100 feet, then
reset the pedometer. Otherwise, turn right.
- Print the current direction on the pedometer.
(Note that this main method could be in the Pedometer class for
testing purposes, or it could be in another class to create an application
that uses one or more Pedometer objects.)