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:
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.)
1. Starting DrJava:
There are many different text editors. and most programmers have their
personal favorite. For this lab you will be using the text editor
in the integrated development environment (IDE) call DrJava.
To get started:
Run DrJava (by clicking or double-clicking on the
icon on your desktop).
"Clean up" DrJava by clicking on File and pulling
down to Close All.
Notice that the title bar includes the text "(Untitled)". This means that
a document is open (even though it has nothing in it) and that it does
not yet have a title/name.
2. Text Editing Functionality for Programmers:
This part of the lab will help you gain skills with text editing
functionality that is especially useful for programmers.
Open PictureFrame.java by clicking on
Open, selecting the appropriate
file and directory, and clicking on Open.
What text appears in the title bar of the window that opened.
The name of the file (among other things).
In case you couldn't guess, the file
named PictureFrame.java is written in Java. For
readability, all of the words return should line up
(i.e., should start in the same column). Find all of the occurences
of the word return and note their column numbers.
Do they line up?
No.
Click on Edit+Select All.
What happened?
All of the lines are highlighted.
Click on Edit+Indent Lines (or
press Tab.
Do all of the words return line up now?
Yes.
The word public is a reserved word in Java so, DrJava
presents it in a special color.
What color?
On my machine, blue.
Go to any instance of the word public
and delete the b.
What color is it now?
On my machine, black. Hence, it must be a typo.
What changed in the title bar?
There is now an asterisk (i.e., '*').
What do you think the asterisk in the title bar indicates
(when it is there)?
The file has been changed since it was last saved.
Fix the typo you just created (i.e., change it back
to public).
Close PictureFrame.java.
Open WhatAnEgo.java.
Click on Edit+Preferences, select
"Display Options", and select "Show All Line Numbers" (if it isn't
already).
What happened?
Line numbers appeared on the left side. They are not part of the
text.
All of the text between a /* and a */
is a comment in Java (and many other programming languages).
DrJava presents comments in a special color.
What color?
On my machine, green.
How else can you include a comment in a Java program?
Everything on a line after a double-slashes is a comment.
You can tell because it is in green in DrJava.
String literals are enclosed in quotes in Java
(and many other programming languages).
DrJava presents String literals in a special color.
What color?
On my machine, dull red.
Turn on line numbers (if they aren't on already).
Delete the quotation mark near the end of line 21 (i.e., the second
quotation mark on the line).
What happened?
Everything after the quotation mark on that line is red, indicating that
DrJava thinks all of those characters are part of the
String literal.
Click on Edit and pull down to Undo
(to restore the quotation mark).
Delete the quotation mark near the beginning of line 21 (i.e., the first
quotation mark on the line).
What happened?
Prof. changes to blue and everything after the
quotation mark on that line is red, indicating that
everything is messed up.
Undo your last change. (At this point the file should be in
its original state. If you're not sure that it is, close the file
without saving it and re-open it.)
Click on column 1 of line 1.
What happens if you click to the right of a close curly bracket?
Everything between it and the corresponding open
curly bracket is highlighted.
Click on column 1 of line 1.
What happens if you click to the right of an open curly bracket?
Everything between it and the corresponding close
curly bracket is highlighted.
Click on column 1 of line 1.
What happens if you click to the right of a close parenthesis?
Everything between it and the corresponding open
parenthesis is highlighted.
Click on column 1 of line 1.
What happens if you click to the right of an open parenthesis?
Everything between it and the corresponding close
parenthesis is highlighted.
Click on column 1 of line 1.
What happens if you click to the right of a close square bracket?
Everything between it and the corresponding open
square bracket is highlighted.
Click on column 1 of line 1.
What happens if you click to the right of an open square bracket?
Everything between it and the corresponding close
square bracket is highlighted.
3. Integrated Development Environments (IDEs):
This part of the lab will help you gain skills with
an IDE.
Close all of the files that are currently open and re-open
WhatAnEgo.java.
Click on Compile to compile
WhatAnEgo.java (i.e., to create
a file named WhatAnEgo.class containing
equivalent byte codes).
What text is displayed in the "Compiler Output" area?
Something like:
Compilation completed.
What information is this text conveying?
It's indicating that DrJava compiled the file named WhatAnEgo.java
(using the javac command).
Note: You could have done the same thing in a command shell.
In the "Interactions" area, type java WhatAnEgo
and press Enter.
What happened?
The program executed and generated output like:
Prof. Bernstein is ranked 100 percent.
He's great!
You've decided that the program isn't doing what it should and you're
thinking of deleting lines 21 and 22. However, you're not sure and
you don't want to re-type them if you don't have to. So, click on the
left side of that line (not on the 21) and, keeping the mouse-button
pressed, drag down to the next line.
What happened?
The lines are highlighted to indicate that they are selected.
Click on Edit and pull down to Comment Lines.
What happened?
DrJava made the lines comments (so they turned green on my machine).
Compile and run this version of the program.
What text is displayed in the "Interactions" and/or "Console" area?
Something like:
He's great!
You were wrong, the program was correct before. So,
select the lines that you just "commented out" (if they are not
still selected), click on Edit and pull down
to Uncomment Lines.
What happened?
DrJava removed the comment indicators.
4. Syntax Errors:
This part of the lab will help you gain the skills you need
to understand syntax errors that are generated by the compiler.
If it isn't activated already, activate line numbering.
On line 21, change print to prnt.
Compile WhatAnEgo.java.
What is displayed in the "Compiler Output" area?
Something like:
1 error found:
File: E:\users\bernstdh\WhatAnEgo.java [line: 21]
Error: The method prnt(java.lang.String) is undefined for the type JMUConsole
This message is telling you that WhatAnEgo.java could not
be compiled because it contains a syntax error.
There is a number in the message in the "Compiler Output" area. What do you think this number indicates?
The line number containing the syntax error.
There is an error message in the "Compiler Output" area. What do you think this means?
The compiler could not recognize the symbol prnt(java.lang.String).
This message means that you used a method named prnt that is
passed a String and that the compiler does not think there
is such a function.
Line 21 is highlighted (in yellow on my machine). Where is the cursor?
It is at the last character that does not contain
a syntax error. In this case, System.out. is
fine and the syntax error is after that.
On line 21, 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.
Change the right-side operand of the assignment statement
involving size to 0.
Compile and execute WhatAnEgo.
What is displayed in the "Interactions" area?
Something like:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at WhatAnEgo.main(WhatAnEgo.java:18)
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 message in the "Interactions" 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.
Change the assignment statement so that it again uses 1100.
6. Run-Time Errors:
This part of the lab will help you understand the difference
between compile-time errors and run-time errors.
Change the right-side operand of the assignment statement
involving size to 0.
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:18)
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.
7. Logic Errors:
Change the program so that 550 is assigned to
rank, then re-compile 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 and should be real numbers.
Change the declarations so that the variables are
all of type double and that the literals
are all of type double.
Compile the program.
Are there any syntax errors?
No, the program compiles.
Run the program.
Are there any run-time errors?
Yes, something like.
Prof. Bernstein is ranked Exception in thread "main" java.util.IllegalFormatConv
ersionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:430
2)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at JMUConsole.printf(JMUConsole.java:374)
at WhatAnEgo.main(WhatAnEgo.java:22)
If you look carefully at the message you'll see that it's about
the call to printf() and, in particular, that the wrong
format is being used.
Change the format string so that, instead of %d it uses
%5.1f.
Compile and run the program. It should now be error free.
8. More Advanced Text Editing:
This part of the lab will help you gain some more advanced text editing
skills (that will prove beneficial throughout the semester).
Open the file named PictureFrame.java and correct the indenting
(if it isn't already).
Click on the text of line 10 and then click on
Tools+Toggle Bookmark.
What happened in the "Code" area?
Line 10 is now underlined (in green on my machine).
What happened below the "Code" area?
A "Bookmarks" area was created and line 10 is listed as a bookmark.
Create bookmarks for lines 27, 47, and 57.
Click on Next or Previous in the
"Bookmarks" area.
What happened?
The cursor moved between the bookmarks.
Double-click on line 10 in the "Bookmarks" area.
What happened?
The cursor moved to line 10.
Click on Tools+Toggle Bookmarks.
What happened?
The bookmark for line 10 was removed.
What does "toggle" mean in this context?
Switch back and forth between two possible values.
9. The Keyboard and the Mouse:
This part of the lab will help you determine whether you are more
productive using the keyboard or the mouse.
Using the mouse only, scroll to the top of the file, select the
entire file, and cut the selected text.
Undo the cut.
In the instructions/questions that follow, Ctrl denotes the
[Ctrl] key on MS Windows keyboards and the [command] on Mac keyboards.
Using the keyboard, type Ctrl-Home.
Then hold the Shift and type Ctrl-End,
Ctrl-X.
Which approach was faster.
For me, the keyboard was much faster.
Undo the cut.
Using the keyboard, type Ctrl-a,
Ctrl-X.
Which approach was faster.
There's no comparison!
Undo the cut.
Using nothing but the mouse, scroll to the top of the
file, select the entire file, click on Edit and
pull down to Indent Line(s).
Press Ctrl+A and then press Tab.
Which approach was faster.
For me, the keyboard was much faster.
Other than the time required to execute the command itself,
write down another advantage of using keyboard shortcuts rather than
the mouse.
It is much easier to start typing again since your hands are still in
the right place.
Using the keyboard, press Ctrl+G, 10.
Now, press Ctrl+Right.
What happened?
The cursor moved to the right one word.
What most people call keyboard shortcuts or accelerator keys, programmers
call key bindings. In DrJava, some key bindings are set by default
and others are not set, but most can be changed. To see the key bindings,
click on Edit+Preferences
and then select Key Bindings.
What key binding is used to delete from the cursor to the end of the line?
(Hint: It's call "Clear Line".)
Ctrl+k.
What key binding is used to select from the cursor to the end of the line?
Shift+End.
What key binding is used to select from the cursor to the beginning
of the line?
Shift+Home.
What key binding is used to select from the cursor to the end
of the word?
Ctrl+Shift+Right.
What key binding is used to select from the cursor to the beginning
of the word?