Java Eyes

An introduction to your
Integrated Development Environment
(IDE)

Part 3: Compile-time and run-time errors

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should:
Be able to: Explain the following concepts:
  • Integrated Development Environment (IDE)
    • Why using an IDE is valuable
  • Compiling and executing a program
  • Compile-time errors and run-time errors
  • Documentation and Javadoc doc-comments
  • Version control
    • Why version control is valuable
Be able to: Do the following in our IDE (JCreator Pro and Tortoise CVS):
  • Checkout and commit a project in the CVS version-control system
  • Compile and execute a program
  • Navigate the IDE
  • Edit a program
  • Use JDK Help to learn about a class
  • Identify the apparent source of compile-time errors
  • Provide Javadoc doc-comments per the course's documentation standard
Items in red are learning objectives for this part of the project.

Instructions:

Group work

  • Continue working with your partner from JavaEyes, Part 2.
  • But switch roles so that the other person does the JCreator typing in this part.

Final report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your final report.

Time limit

  • Most students complete this exercise in 30 to 60 minutes.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.

Question: What time is it now? (Later we will ask you how long you spent on Part 3 of JavaEyes.)

Kinds of Errors

Programs can have two kinds of errors:

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.

Fixing Compile-Time Errors

  1. What happens when things go wrong with your project? To find out, you're going to comment out a portion of the code, in order to see what kinds of compile-time errors you can get.

    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:

    If you have further questions about comments, please ask a course assistant.

  2. Open JCreator so that you are back in your JavaEyes project.

  3. Compile and run JavaEyes to confirm that you are starting with an uncorrupted copy.

  4. Find the line in the JavaEyes.java file that says:
         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.

  5. Double-click on the line that contains the error message (or you can use the F4 key as a shortcut to the next error).

    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:

  6. Now insert two slash characters (//) 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:

    With practice, you will soon be able to use the (sometimes cryptic) compiler messages to locate the real source of compile-time errors.

  7. Before you repair the error, run the application again.

  8. Take out the backslashes you inserted, change the spelling from EYE back to Eye), and reassure yourself that the program again compiles successfully.

  9. Now find the line that says:
         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!

  10. Fix the code you just broke, and recompile to make sure everything works again.

Fixing Run-Time Errors

  1. The previous section showed you compile-time errors. Now let's look at run-time errors.

    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
    

  2. There is at least one other run-time error, it seems to me. See if you can spot it:

    Question: Briefly describe the run-time error you found (the "other" one, not the "/ by zero").

Getting Help

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?

Summary and Finishing Up

  1. Question: Programs can have two kinds of error. What are they?

  2. Question: When you get a compile-time error message, how do you go to the line that the compiler thinks is the source of the error?

  3. Question: Does the compiler identify the correct source of the error: never? sometimes? always?

  4. Compare your answers with what others say. Keep asking other people (or assistants!) until you are confident that you know the right answers.

  5. Question: How much time did you spend on Part 3 of JavaEyes?