Back to 111 Main Page

Mobius strip

Assignment 13

Task

Practice reading and debugging strange code.

Scenario

When you come into your job at Exitech one Monday morning, the office smells of rotten eggs and burnt rubber. Your boss corners you and tells you the following:

Great, you're finally in. Look, Freddy and his cubicle were immolated Friday night. Yeah, pretty crazy, I know. I guess Pam was working late too and she saw it--said it was "a great unholy green fire". Yeah, I've heard all the water-cooler talk that he was messing around with "the occult" and "forbidden knowledge" and such nonsense, but don't believe a word of that foolishness! The man was known to play with matches in his cubicle.

But look, the point of all this is that now we're behind on the WordSmith account. We're supposed to have that program to them today! Freddy was normally a diligent worker, but he seemed sort of... well, distracted last week. But he assured me he'd get it done Friday night. And now look--we have this mess to clean up and no program!

I'll have Pam forward you the last copy of Freddy's work that we have on the backup server. He had three meetings with WordSmith hashing out the output requirements. I guess he wrote them up in some documentation format or some such thing--so don't mess that up. Just get his program working, ASAP! Oh, and don't bother about that whole bug report thing this time--we're in a hurry on this one.

The boss strolls off. Ugh. So begins another Monday at Exitech.

Steps

The code: PalindromeChecker.java

First, look though the code and determine what basic problem it is trying to solve. Rename the file (and class inside) to UsernameA13.java. Add your name to the javadocs with another @author tag.

Now work on getting the program to compile and run correctly. You do not need to produce a bug report, and so you do not need to preserve the line numbering. It's your code now: add, change, delete, and reformat as you see fit to get the program to work correctly. While you may find that proper indenting and better variable names would make the code more understandable (and so worth doing), you are not required to fix Freddy's coding standards.

Hints

  • Remember that when you rename a class, you'll also need to change any internal references to the class--such as in static method calls.
  • With compile errors, start with the first one listed. Frequently, later compile errors result from the compiler getting confused by an earlier error.
  • With runtime errors, remember that the output from an exception or runtime crash is a dump of the runtime stack. Look at it to see what code line it crashed on and while calling which method. Usually, this is not the place to the fix the bug though. Instead, the problem is often due to some variable not having a valid or correct value, as set (or mangled) earlier in the code.
  • With both runtime and logic errors, don't just try to stare your way through the code. You'll need to trace though the code line-by-line with specific input, tracking what values each variable contains and the results of each test. Often, it's helpful during this process to print something out to see or double-check what data Java is really working with. For example, in this program, p.charAt(h) and p.charAt(t) are often examined and eventually compared. If you're not getting the results you expect, try printing out what is being compared each time through the loop, as with this temporary line:
    System.out.println(p.charAt(h) + "=?=" + p.charAt(t));
  • The errors are generally little things, such as: a single character added or missing; a single word or variable name that is incorrect or misspelled; a +1 or -1 missing somewhere; a ++ that should be a -- (or vice versa); etc. You shouldn't need to add any lines of code to get the program to work (though you may if you want to).
  • If all else fails, you could always rewrite the code from scratch to do what it's supposed to do.

What to Submit

Upload your UsernameA13.java file to Tamarin.

Grading [3 points]

1.5 - Compiles
Your uploaded program compiles successfully. (You are not required to follow coding standards for this assignment.)
1.0 - Program runs
No runtime crashes.
0.5 - Correct output.
Output is correct for various input.

FAQs

Tips
Compiling is usually the easy part; it's runtime and logic errors that are hard.
  • If your program is falsely identifying palindromes that really are not (or vice versa), use the println statement given above in Hints to see what two characters are being compared to each other every time through the loop. (This is the single most valuable bit of advice on this page: If the end result of your program is wrong, print out what it's doing along the way so you can see what is really happening.)
  • If you're getting runtime crashes because one of your indexes is off: figure out, based on the algorithm, what the index should be in that situation (ie, for the length of the string you just entered). Then look for why the index would have a value of one more or less than that.
  • Pay attention to every brace and semi-colon. Do you really have the control structures that you think you do?
  • Hint: All the runtime and logical errors (if any) are to be found in the isPalindrome method.
  • Try not to introduce your own bugs on top of those already in the file. In short, don't make changes unless you know why they are needed.
  • If you can't make sense of what the isPalindrome method is doing, think about how you would have written it if you had to solve the same problem.
  • Feel free to delete the irrelevant //comments if you want to.


~ztomasze Index : TA Details: ICS111: A13
http://www2.hawaii.edu/~ztomasze
Last Edited: 03 Mar 2009
©2009 by Z. Tomaszewski.