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:
-
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:
- Call
JMUConsole.open()
once before the
program generates any output.
- Call
JMUConsole.print()
one or more times,
passing it a single argument (i.e., the thing to display
on the console).
- 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:
-
Run jGRASP. (How you do this will depend on your
operating system and how/where you installed jGRASP.)
-
"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.
-
Open
WhatAnEgo.java
by clicking on File-
Open, navigating to the appropriate directory/folder,
and selecting the appropriate file.
-
Click on View-Line Numbers (if line numbers
aren't already being displayed).
-
Click on to compile
WhatAnEgo.java
(i.e., to create
a file named WhatAnEgo.class
containing
equivalent byte codes).
-
What text is displayed in the "Compile Messages" area?
Something like:
----jGRASP exec: javac -g WhatAnEgo.java
----jGRASP: operation complete.
-
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.
-
Click on to run
this file.
-
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.
-
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.
4. Syntax Errors:
This part of the lab will help you understand syntax errors
and when they are generated.
-
On line 23, change
print
to prnt
.
-
Save the modified file.
-
Compile
WhatAnEgo.java
.
-
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
-
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.
-
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.
-
The next line contains the symbol that the compiler couldn't
recognize. What symbol was it?
-
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.
-
On line 23, change
prnt
back to print
.
-
Compile
WhatAnEgo.java
. (It should compile properly.)
-
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).
-
Change the right-side operand of the assignment statement
involving
size
to 0
and save the file.
-
Compile and execute
WhatAnEgo
.
-
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)
-
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
-
What statement is on that line? In other words, what statement
in
WhatAnEgo.java
caused the run-time error?
percentage = (1 - (rank / size)) * 100;
-
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
-
What do you think this message means?
This error means that the offending statement divided by 0
when the program was running.
-
Why did the program compile?
It didn't contain any syntax errors.
-
What kind of error was generated?
A run-time error.
-
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.
-
Change the program so that
550
is assigned to
rank
, save it, re-compile it, and re-execute it.
-
Is the output correct?
No, it should be about 50%.
-
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.
-
Why did the program compile?
It didn't contain any syntax errors.
-
Why didn't the program generate a run-time error?
Because it can run from start to finish.
-
What kind of error does the program contain?
A logic error.
-
What is the error?
percentage
, rank
and size
are integers (i.e., int
variables) and should be
real numbers (i.e., double
variables).
7. Style Errors:
This part of the lab will help you understand style errors that are
detected by static analysis tools like checkstyle.
-
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.
-
Click on Tools, pull down to Checkstyle,
and over to Check File.
-
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.
-
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.
-
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.
-
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
}
}
-
Click on Tools, pull down to Checkstyle,
and over to Check File.
-
What text is displayed in the "Compile Messages" area?
Something like:
----jGRASP exec:
Starting audit...
Audit done.
----jGRASP: operation complete.
-
What does this message tell you?
All of the style errors have been corrected.