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.

  1. (2 pts) Consider the following method:

    public static int mystery(int n) {
        if (n == 0) 
             return 1;
        return 2 * mystery(n-1);
    }
    

    1. Explain why this method is recursive.

    2. 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));

    3. Describe the function this mystery method is computing.

    4. Write another method that computes the same function non-recursively using a loop instead.

  2. (1.5 pts)

    1. 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:
          
      
      }
      

    2. 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. (1.5 pts) Consider a Java class named Pedometer that models the object described above.

    public class Pedometer {
    
       // to be completed
    
    }
    

    1. Write variable definitions for the fields that are necessary to represent a pedometer along with their data types as described above.

    2. 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.

    3. Write simple accessors for each of the fields for a pedometer. Each accessor should have no parameters and an appropriate return type.

  2. (2 pts) Write the following mutator methods:

    1. turnLeft - Changes the direction stored in the pedometer to the direction that is 90 degrees counterclockwise from the current direction.

    2. turnRight - Changes the direction stored in the pedometer to the direction that is 90 degrees clockwise from the current direction.

    3. 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").

    4. reset - Restores the pedometer to a direction of NORTH and a distance traveled of 0 feet.

  3. (1 pt) We also need some additional methods to complete our Java class.

    1. 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.

    2. 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.

  4. (2 pts) Write a main method that does the following:

    (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.)