Power Outage

Exercises

  1. In Java,
    1. What does the keyword final mean when it is used:
      1. As a variable modifier?
      2. As a method modifier?
      3. As a class modifier?
    2. What does the keyword extends mean?
    3. What does this refer to?
    4. What does super refer to?
    5. What is an interface?
    6. What is a superclass? What is a subclass?
  2. Study the source code for the partially complete program shown below.
    1. Describe the real-world problem that is modeled by the program.
    2. In what ways is the model extremely realistic?
    3. In what ways does the model approximate reality? How good are the approximations? How could they be improved?
    4. What are some interesting questions that could be answered using the program shown below or some variation of it that could not easily be answered without such a program?
  3. Finish the incomplete classes shown below. Do not change any of the code that is already written.
  4. For each of the classes shown below that does not have a main method, add a main method that could serve as the starting point of a program if the class was made public. In each of your main methods, include statements that invoke the other public methods of the class, including each of the public constructors. Pass interesting arguments to the methods when you invoke them. The goal is to convince yourself, by executing each of the main methods and observing the output produced (if any), that all of the public methods work as intended. If you are not sure how a method is supposed to work, then ask.
  5. Make sure you have completed the previous exercises before attempting this exercise. Study the main method of the PowerOutage class carefully.
    1. What do you observe when you run the program? Run the program several times.
    2. Explain why the value returned by oneHop.powerOut() is sometimes greater than, sometimes equal to, and sometimes less than the value returned by okPath.powerOut().
  6. Modify the implementation of PathFinder.findGoodPath so that the method returns a SignalPath object that is always at least as good as and is sometimes better than the object referred to by the variable in PowerOutage.main named oneHop in terms of the value returned by the objects’ powerOut methods. Larger .powerOut() values are better than smaller ones.
  7. Modify the implementation of PathFinder.findOptimalPath so that the method returns a SignalPath object that is as good as possible in terms of the value returned by the object’s .powerOut method. Explain how your implementation works in English; present your explanation in the form of comments that accompany your implementation.
  8. Implement the program shown below, including the missing parts, in JavaScript instead of Java.