JMU JMU - Department of Computer Science
Help Tools
Lab: Experimenting with jGRASP


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....)

1. Background on Console Output: The JMUConsole class (used in this lab) is an easy way to send output to the console. It is used as follows:
  1. Call JMUConsole.open() once before the program generates any output.
  2. Call JMUConsole.print() one or more times, passing it a single argument (i.e., the thing to display on the console).
  3. Call JMUConsole.close() once before the program terminates.

For those people who know how to use them, it also has static printf() and println() methods.

2. Starting jGRASP: There are many different integrated development environments (IDEs). For this lab you will be using a rudimentary IDE called jGRASP that is oriented towards beginning programmers. To get started:
  1. Run jGRASPjgrasp.gif. (How you do this will depend on your operating system and how/where you installed jGRASP.)
  2. "Clean up" jGRASP by clicking on File, pulling down to Close All, and over to Windows.
3. Compiling and Running Programs: This part of the lab will help you gain the skills you need to compile and run/execute programs in an IDE.
  1. Open WhatAnEgo.java by clicking on File- Open, navigating to the appropriate directory/folder, and selecting the appropriate file.
  2. Click on View-Line Numbers (if line numbers aren't already being displayed).
  3. Click on jgrasp-compile.gif to compile WhatAnEgo.java (i.e., to create a file named WhatAnEgo.class containing equivalent byte codes).
  4. What text is displayed in the "Compile Messages" area?


    Something like:
     ----jGRASP exec: javac -g WhatAnEgo.java
    
    
     ----jGRASP: operation complete.
        
    Expand
  5. What information is this text conveying?


    It's indicating that jGRASP compiled the file named WhatAnEgo.java using the javac command.

    Note: You could have done the same thing in a command shell.

    Expand
  6. Click on jgrasp-run.gif to run this file.
  7. What text is displayed in the "Run I/O" area?


    Something like:
    ----jGRASP exec: java WhatAnEgo Bernstein
    
    Prof. Bernstein is ranked 100 percent.
    Hes' great!
    
    ----jGRASP: operation complete.
        
    Expand
  8. What information is this text conveying?


    First, it's indicating that jGRASP executed the program WhatAnEgo.class using the java command.

    It then shows the output that is generated by the program.

    Note: You could have done the same thing in a command shell.

    Expand
4. Syntax Errors: This part of the lab will help you understand syntax errors and when they are generated.
  1. On line 23, change print to prnt.
  2. Save the modified file.
  3. Compile WhatAnEgo.java.
  4. What is displayed in the "Compile Messages" area?


    Something like:
    WhatAnEgo.java:23: error: cannot find symbol
            JMUConsole.prnt("Prof. Bernstein is ranked ");
                      ^
      symbol:   method prnt(String)
      location: class JMUConsole
    1 error
        
    Expand
  5. This message is telling you that WhatAnEgo.java could not be compiled because it contains a syntax error. There is a number after the text WhatAnEgo.java and between two colons. What do you think this number indicates?


    The line number containing the syntax error.
    Expand
  6. There is an error message after the colons that says something like cannot find symbol. What do you think this means?


    The compiler could not recognize a symbol.
    Expand
  7. The next line contains the symbol that the compiler couldn't recognize. What symbol was it?


        prnt(java.lang.String)
        
    Expand
  8. This message meanse that you used a function named prnt that is passed a String and that the compiler does not think there is such a function. The next line contains the statement containing the problem. Under that is a line with a ^ (called a caret). What is the caret pointing at and why?


    It is pointing at the last character that does not contain a syntax error. In this case, JMUConsole is fine and that the problem is after that. This means that the compiler does not recognize the method prnt() since there is no such method in the JMUConsole class.
    Expand
  9. On line 23, change prnt back to print.
  10. Compile WhatAnEgo.java. (It should compile properly.)
  11. Make some other changes that cause syntax errors, re-compile WhatAnEgo.java, and make sure you can interpret the error messages. When you are done, change everything back so that the program compiles correctly.
5. Run-Time Errors: This part of the lab will help you understand the difference between compile-time errors and run-time errors (and when they occur).
  1. Change the right-side operand of the assignment statement involving size to 0 and save the file.
  2. Compile and execute WhatAnEgo.
  3. What is displayed in the "Run I/O" area?


    Something like:
    Exception in thread "main" java.lang.ArithmeticException: / by zero
            at WhatAnEgo.main(WhatAnEgo.java:20)
    
        
    Expand
  4. Looking at the error message, what line caused the run-time error?


    It says that the run-time error was caused by line 18 of WhatAnEgo.java
    Expand
  5. What statement is on that line? In other words, what statement in WhatAnEgo.java caused the run-time error?


            percentage = (1 - (rank / size)) * 100;    
         
    Expand
  6. The previous line in the "Run I/O" area describes the error (also called an exception). What was the error?


    java.lang.ArithmeticException: / by zero
        
    Expand
  7. What do you think this message means?


    This error means that the offending statement divided by 0 when the program was running.
    Expand
  8. Why did the program compile?


    It didn't contain any syntax errors.
    Expand
  9. What kind of error was generated?


    A run-time error.
    Expand
  10. Change the assignment statement so that it again uses 1100 and save the file.
6. Logic Errors: This part of the lab will help you understand the difference between syntax errors and logic errors.
  1. Change the program so that 550 is assigned to rank, save it, re-compile it, and re-execute it.
  2. Is the output correct?


    No, it should be about 50%.
    Expand
  3. So, is the program correct? Was it correct before?


    No, it is not correct and was not correct before. We just got lucky for one test point.
    Expand
  4. Why did the program compile?


    It didn't contain any syntax errors.
    Expand
  5. Why didn't the program generate a run-time error?


    Because it can run from start to finish.
    Expand
  6. What kind of error does the program contain?


    A logic error.
    Expand
  7. What is the error?


    percentage, rank and size are integers (i.e., int variables) and should be real numbers (i.e., double variables).
    Expand
7. Style Errors: This part of the lab will help you understand style errors that are detected by static analysis tools like checkstyle.
  1. If you're using a lab computer (sorry, you may have to do this every time you login), go to the course "Help" page on "Installing the CS149 Development Environment", download Checkstyle and the CS149 Style Checks, and follow the instructions for installing checkstyle. If you are using your own computer, you should already have done this.
  2. Click on Tools, pull down to Checkstyle, and over to Check File.
  3. What text is displayed in the "Compile Messages" area?


    Something like:
     ----jGRASP exec: 
    Starting audit...
    [ERROR] WhatAnEgo.java:5:1: Type Javadoc comment is missing an @author tag. [JavadocType]
    [ERROR] WhatAnEgo.java:5:1: Type Javadoc comment is missing an @version tag. [JavadocType]
    [ERROR] WhatAnEgo.java:6:1: '{' at column 1 should be on the previous line. [LeftCurly]
    [ERROR] WhatAnEgo.java:7: First sentence should end with a period. [JavadocStyle]
    [ERROR] WhatAnEgo.java:13:4: '{' at column 4 should be on the previous line. [LeftCurly]
    Audit done.
    Checkstyle ends with 5 errors.
    
     ----jGRASP: operation complete.
        
    Expand
  4. What does this message tell you?


    There are several style errors.

    The block comment at the top of the class is missing an @author tag and a @version tag.

    The left brace on line 6 should be at the end of line 5.

    In line 7, the first line of the comment should end with a period.

    The left brace on line 13 should be at the end of line 12.

    Expand
  5. Are these syntax errors?


    No, the program compiles fine (i.e., it is consistent with the grammar of Java). However, it does not conform to the course style guide.
    Expand
  6. Fix these style defects. When you're done, the class should look something like the following:
    /**
     * A very simple Java program that does nothing but generate
     * some output.
     *
     * @author  Prof. David Bernstein, James Madison University
     * @version 2.0
     */
    public class WhatAnEgo {
    
        /**
         * The entry point of the application.
         *
         * @param args  The command-line arguments. args[0] should be a name
         */
        public static void main(String[] args) {
            int percentage;
            int rank;
            int size;
            
            size = 1100;
            rank = 1;
            percentage = (1 - (rank / size)) * 100;    
    
            JMUConsole.open();        
            JMUConsole.print("Prof. Bernstein is ranked ");
            JMUConsole.print(percentage);
            JMUConsole.print(" percent.\n");
            JMUConsole.print("He's great!\n");
            JMUConsole.close();
            
            // Done
        }
    }
    
  7. Click on Tools, pull down to Checkstyle, and over to Check File.
  8. What text is displayed in the "Compile Messages" area?


    Something like:
     ----jGRASP exec: 
    Starting audit...
    Audit done.
    
     ----jGRASP: operation complete.
        
    Expand
  9. What does this message tell you?


    All of the style errors have been corrected.
    Expand

Copyright 2020