Homework Assignment 2


This homework assignment will give you additional experience with writing basic Java programs and then some initial experience with inheritance and types.
  1. Rewrite the Salary.java question from homework assignment 1 with the following modifications:

    1. Create an instance variable in the Salary class in which you place the computed value for the average salary once all the salaries have been read (as opposed to a local variable in the Salary constructor).
    2. Create a method in Person for printing the person's information and true if the person's salary is above the group average and false otherwise. Your method will need to access the parent class's average salary instance variable. Your program should call this method when printing out employee's information at the end of your program. This part of the problem is supposed to show you that instances of nested classes can access instance variables in their parent object.
    3. The input will be read from standard input. There will be one person record per line. Hence the sample input from hw1 will now look like:
      nels 105000.80 
      mary 20000.50 
      frank 18153.33 
      nancy 91868.73 
      michelle 20858
      
      Use a Scanner object, not a Console object, to read input because the TAs may redirect stdin in order to test your program. You may assume that the input is error free.
    4. You should output the names in the order that they appear in the input. Hence your output for the above sample input should be:
      average salary = 51,176.27 
      
      nels            105,000.80 true
      mary             20,000.50 false
      frank            18,153.33 false
      nancy            91,868.73 true
      michelle         20,858.00 false
      
    5. You can either use your list code from question 7 to handle the variable-length input or you can use an ArrayList to handle the variable length-input.
    You must use my program design. I understand it might not be either the way you would like to organize your program or even the best way to design the program. However, I want to give you experience with using nested classes and with calling methods.

  2. The following problem will give you experience with Java's collection classes, Java's file I/O classes, and Java's Scanner class.

    Pet Detective is a game published by Lumosity that asks you to deliver pets to their homes within a specified number of moves. A screen shot of Pet Detective is shown below and you can view a video of a person playing the game here. Please note that you can skip over a pet without picking it up or skip over a home without visiting it.

    As you can see from the video, at each turn you move the car to either a pet, thus picking it up, or to a pet home, thus dropping it off. As you can also observe, the car has a capacity of 4 pets. Hence once you've loaded 4 pets into the car, you must deliver a pet to its home at the next turn before you can pick up another pet.

    This problem can be difficult to solve at the upper levels and one might want to write a program to try to find a solution to the puzzle (especially since Lumosity occasionally uses a puzzle that can't be solved in the required number of moves).

    Solving Strategy. I discussed this problem in lecture. If you are having problems implementing it after reading this description, then go to the video lecture. It starts talking about the problem at around the 50 minute mark.

    These puzzles can be expressed as an optimization problem. The puzzle solution is a Hamiltonian Circuit and hence the problem is NP-complete. Duane Mattern has managed to express this problem as an integer programming problem. However, a more efficient way to solve this problem is as a graph problem since you can take advantage of knowing the distances between nodes. You must solve this problem as a graph problem using the following formulation. You should represent the car, each pet, and each pet home as a vertex. You will then need to use a brute force, recursive search algorithm to find a solution and this search might take several minutes.

    You are free to investigate any search algorithm you want, but my solution involved always moving the car to the nearest unsearched vertex, subject to the constraint that if the car was full, the nearest vertex had to be a pet home, and then recursively continuing the search from that vertex. I would backtrack if the search failed to find a solution.

    Floyd Warshall Algorithm. You will need to use the Floyd Warshall algorithm presented in class to compute the shortest path between each pair of vertices before you start your exhaustive search.

    Input. The input will be a file whose name is given as a command line argument. The first line of your input will be the number of moves you can use to deliver your pets. The remaining lines of input will consist of vertex pairs and the distance between the pairs. For the above screen shot, the input might begin:

          29
          car marmot 1
          car armadillo 1
          fox_home cat_home 1
          fox_home yellow_dog 1
          cat_home marmot_home 1
          cat_home yellow_dog_home 2
          ...
        
    I make the following guarantees about the input:

    Output

    If the problem has a solution, then print out the vertices in the order in which they get visited. For example, if the car in the above screenshot makes it first moves to the armadillo, then the cardinal, then the cat, and then the armadillo home, then your output would start as:

    	  armadillo
    	  cardinal
    	  cat
    	  armadillo_home
    	  ...
    	

    If the puzzle cannot be solved in the given amount of moves, then print "No solution found".

    Data Structure Requirements: This is a homework assignment using Java's collection classes, so you must use the following data structures:

    You will probably need to use an array or ArrayList to handle your adjacency matrix. Since you don't know in advance either the number of pets or the maximum number of pets, it's probably easier to use an ArrayList. You should feel free to use other Java data structures as necessary.

    Parting Notes


    What to Submit

    Please submit the following two files:

    1. A jar file named Salary.jar that we can execute to test problem 1. Please include your source code in Salary.jar.
    2. A jar file named Petdet.jar that we can execute to test problem 2. Please include your source code in Petdet.jar.