COMP 110-003 Fall 2008

In-class exercise

October 10, 2008

Description

The purpose of this exercise is to give you some more practice with a bunch of things we have learned so far, up to and including how to write an equals method.

Download ExerciseOct10.java and InsecureCreditCard.java. Make sure they are both in the same folder on your computer. Read through both files and try to understand what is happening. If you try to compile ExerciseOct10.java, you will see that it has several syntax errors. Your goal in this exercise is to correct the syntax errors, and then to correct logic errors in the code.

Each time you find an error in the code, write down (on paper) what you found. You can be brief in what you write. Turn this paper in at the end of class, even if you have not finished the whole exercise. If you finish early, let me know and show me what you have done.

  1. Start off by correcting one syntax error at a time. For example, when you first compile the program, start by correcting the very first error: the keyword class is misspelled at the top of the program. Fix that error, and then recompile to find out what the next error is. Recompile again, and fix the next error, until there are no more syntax errors.

  2. Now there are a few logic errors in the code. Try running the program to see what happens. The nested for loops are supposed to iterate over several years and months to find a matching credit card expiration date (which is supposed to be 8/2013). However, a match is not being found. Why not? Write down any logic errors you find. Again, fix one logic error at a time and retest the program.

    Hint: you will also need to change the equals method inside InsecureCreditCard.java to get the program to do what it is supposed to do. Read the description of the equals method in InsecureCreditCard.java, and fill in the code for the method accordingly. See pp. 275-279 (4th edition) or pp. 302-307 (5th edition) for information on writing an equals method.

This exercise will not be graded. We will discuss it later. You are welcome to work in groups, but make sure you understand the concepts. Ask questions if you are having trouble. Make sure you hand in your sheet of paper listing the errors you found in the program.