|
Learning objectives:After completing this project, you should: | |||
Be able to: Explain the following concepts:
|
Be able to: Do the following in our IDE (JCreator Pro and Tortoise CVS):
| |||
Items in red are learning objectives for this part of the project. |
Instructions: | Group work
| Final report
| Time limit
|
Question: What time is it now? (Later we will ask you how long you spent on Part 3 of JavaEyes.)
Programs can have two kinds of errors:
- Compile-time errors are errors that the compiler finds when you compile the program. These are also called syntax errors because they are errors in the syntax, or notation, of the program.
Programming languages require you to be precise. So, for example, if you type
Colr
or evencolor
whereColor
is required, that is a syntax error. Likewise, omitting an expression where it is required and referring to something that is not in the program are syntax errors.
- Run-time errors are errors that are not detected until you run the program. For example, the program might do the wrong thing or even crash in some circumstances. Run-time errors occur when you write code that is legal but does not result in the behavior that was specified.
This exercise focuses on compile-time errors, because those are what you will first encounter. But don't think that a program is correct merely because it compiles. It must compile and behave as specified to be correct.
A comment is an area in your code file that is deliberately ignored by the compiler. Programmers put comments in their code to help themselves and others understand their code. (More on this in Part 4 of JavaEyes.)
To comment out a portion of code means to change the code into a comment (so that the compiler then ignores it). This is sometimes useful as a error-finding technique (it simplifies the code), but today we'll use commenting-out simply as an easy way to introduce errors (so that you can gain experience with error messages).
In Java, you use the following scheme to comment your code:
/* This is a comment. */ // This is also a comment. /* This * is * also * a comment. */ // This is not a comment (and will produce an error).
Anything found within the delimiters /*
and */
is considered a comment.
This does not mean that you can "nest" comments!
In other words,
/* left /* inside */ right */would not be a legal comment. The compiler would consider
/* left /* inside */a comment, and it wouldn't know what to do with the final
*/
.
Any text which comes after //
on the same line
is also considered a comment.
Text which comes after //
on a subsequent line
is no longer considered commented (unless of course, it has been commented somewhere else).
Question: Write a two-line comment that states the color of your partner's eyes:
/*   */
notation.
//
notation.
If you have further questions about comments, please ask a course assistant.
Eye eye1;Change the word Eye to EYE; that is, change it to all upper-case letters like this:
EYE eye1;Compile again and see what happens.
It should not compile successfully this time. (Case matters!)
Question: When you changed Eye to EYE:
If you have any doubts about your answers to the above, simply ask a neighbor or assistant.
Deciphering compile-time error messages is an art that simply requires experience. The error message that you just received -- cannot resolve symbol -- is one of the most common. It can occur for many reasons, including:
//
) at the beginning of the same
line to comment it out, so that it looks like:
// EYE eye1;(Do not delete the line; you'll need it later.) Compile again and see what happens.
Again, it should not compile successfully. But now the compiler reports more than one error, at lines that you did not touch!
Question: When you commented out the line,
making it // private EYE eye1;
:
These examples show that:
Eye
to EYE
).
With practice, you will soon be able to use the (sometimes cryptic) compiler messages to locate the real source of compile-time errors.
EYE
back to Eye
),
and reassure yourself that the program again compiles successfully.
public JavaEyes() {and comment it out. Compile. Your error window should now display many error messages!
Take a look at them and see if you can understand some of them. Also try double-clicking on some of them to see where the compiler thinks you have made errors. It seems that many of these error messages don't relate directly to the error we actually committed!
To do so, run JavaEyes and move the mouse all around the gray frame. Eventually you should see an error message in the bottom panel that begins:
java.lang.ArithmeticException: / by zero
Question: Briefly describe the run-time error you found (the "other" one, not the "/ by zero").
One of the things we hope that you will take away from this class is that you will be a more productive (and happy!) software engineer if you learn to work in groups. If you have a question, ask your classmates! This helps both of you: it clears up your question, and reinforces another student's understanding of the material. The course assistants are also a good resource, and you should feel completely at ease with asking them for help. That's what they are there for, and you want them to feel needed, don't you?