JMU
Lab: Skills - Using the DrJava Editor


http://imgs.xkcd.com/comics/real_programmers.png
(Courtesy of xkcd)

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.)

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:
  1. Run DrJava (by clicking or double-clicking on the drjava.gif icon on your desktop).
  2. "Clean up" DrJava by clicking on File and pulling down to Close All.
  3. 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.
  1. Open PictureFrame.java by clicking on Open, selecting the appropriate file and directory, and clicking on Open.
  2. What text appears in the title bar of the window that opened.


    The name of the file (among other things).
    Expand
  3. 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.
  4. Do they line up?


    No.
    Expand
  5. Click on Edit+Select All.
  6. What happened?


    All of the lines are highlighted.
    Expand
  7. Click on Edit+Indent Lines (or press Tab.
  8. Do all of the words return line up now?


    Yes.
    Expand
  9. The word public is a reserved word in Java so, DrJava presents it in a special color.
  10. What color?


    On my machine, blue.
    Expand
  11. Go to any instance of the word public and delete the b.
  12. What color is it now?


    On my machine, black. Hence, it must be a typo.
    Expand
  13. What changed in the title bar?


    There is now an asterisk (i.e., '*').
    Expand
  14. 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.
    Expand
  15. Fix the typo you just created (i.e., change it back to public).
  16. Close PictureFrame.java.
  17. Open WhatAnEgo.java.
  18. Click on Edit+Preferences, select "Display Options", and select "Show All Line Numbers" (if it isn't already).
  19. What happened?


    Line numbers appeared on the left side. They are not part of the text.
    Expand
  20. 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.
  21. What color?


    On my machine, green.
    Expand
  22. 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.
    Expand
  23. String literals are enclosed in quotes in Java (and many other programming languages). DrJava presents String literals in a special color.
  24. What color?


    On my machine, dull red.
    Expand
  25. Turn on line numbers (if they aren't on already).
  26. Delete the quotation mark near the end of line 21 (i.e., the second quotation mark on the line).
  27. 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.
    Expand
  28. Click on Edit and pull down to Undo (to restore the quotation mark).
  29. Delete the quotation mark near the beginning of line 21 (i.e., the first quotation mark on the line).
  30. What happened?


    Prof. changes to blue and everything after the quotation mark on that line is red, indicating that everything is messed up.
    Expand
  31. 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.)
  32. Click on column 1 of line 1.
  33. What happens if you click to the right of a close curly bracket?

    Everything between it and the corresponding open curly bracket is highlighted.
    Expand
  34. Click on column 1 of line 1.
  35. What happens if you click to the right of an open curly bracket?

    Everything between it and the corresponding close curly bracket is highlighted.
    Expand
  36. Click on column 1 of line 1.
  37. What happens if you click to the right of a close parenthesis?

    Everything between it and the corresponding open parenthesis is highlighted.
    Expand
  38. Click on column 1 of line 1.
  39. What happens if you click to the right of an open parenthesis?

    Everything between it and the corresponding close parenthesis is highlighted.
    Expand
  40. Click on column 1 of line 1.
  41. What happens if you click to the right of a close square bracket?

    Everything between it and the corresponding open square bracket is highlighted.
    Expand
  42. Click on column 1 of line 1.
  43. What happens if you click to the right of an open square bracket?

    Everything between it and the corresponding close square bracket is highlighted.
    Expand
3. Integrated Development Environments (IDEs): This part of the lab will help you gain skills with an IDE.
  1. Close all of the files that are currently open and re-open WhatAnEgo.java.
  2. Click on Compile to compile WhatAnEgo.java (i.e., to create a file named WhatAnEgo.class containing equivalent byte codes).
  3. What text is displayed in the "Compiler Output" area?


    Something like:
    Compilation completed.
        
    Expand
  4. 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.

    Expand
  5. In the "Interactions" area, type java WhatAnEgo and press Enter.
  6. What happened?


    The program executed and generated output like:
    Prof. Bernstein is ranked 100 percent.
    He's great!
        
    Expand
  7. 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.
  8. What happened?


    The lines are highlighted to indicate that they are selected.
    Expand
  9. Click on Edit and pull down to Comment Lines.
  10. What happened?


    DrJava made the lines comments (so they turned green on my machine).
    Expand
  11. Compile and run this version of the program.
  12. What text is displayed in the "Interactions" and/or "Console" area?


    Something like:
    He's great!
        
    Expand
  13. 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.
  14. What happened?


    DrJava removed the comment indicators.
    Expand
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.
  1. If it isn't activated already, activate line numbering.
  2. On line 21, change print to prnt.
  3. Compile WhatAnEgo.java.
  4. 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
        
    Expand
  5. This message is telling you that WhatAnEgo.java could not be compiled because it contains a syntax error.
  6. 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.
    Expand
  7. 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).
    Expand
  8. 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.
  9. 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.
    Expand
  10. On line 21, change prnt back to print.
  11. Compile WhatAnEgo.java. (It should compile properly.)
  12. 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.
  1. Change the right-side operand of the assignment statement involving size to 0.
  2. Compile and execute WhatAnEgo.
  3. What is displayed in the "Interactions" area?


    Something like:
    Exception in thread "main" java.lang.ArithmeticException: / by zero
            at WhatAnEgo.main(WhatAnEgo.java:18)
        
    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 message in the "Interactions" 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. 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.
  1. Change the right-side operand of the assignment statement involving size to 0.
  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:18)
    
        
    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.
7. Logic Errors:
  1. Change the program so that 550 is assigned to rank, then re-compile 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 and should be real numbers.
    Expand
  8. Change the declarations so that the variables are all of type double and that the literals are all of type double.
  9. Compile the program.
  10. Are there any syntax errors?


    No, the program compiles.
    Expand
  11. Run the program.
  12. 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.

    Expand
  13. Change the format string so that, instead of %d it uses %5.1f.
  14. 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).
  1. Open the file named PictureFrame.java and correct the indenting (if it isn't already).
  2. Click on the text of line 10 and then click on Tools+Toggle Bookmark.
  3. What happened in the "Code" area?


    Line 10 is now underlined (in green on my machine).
    Expand
  4. What happened below the "Code" area?


    A "Bookmarks" area was created and line 10 is listed as a bookmark.
    Expand
  5. Create bookmarks for lines 27, 47, and 57.
  6. Click on Next or Previous in the "Bookmarks" area.
  7. What happened?


    The cursor moved between the bookmarks.
    Expand
  8. Double-click on line 10 in the "Bookmarks" area.
  9. What happened?


    The cursor moved to line 10.
    Expand
  10. Click on Tools+Toggle Bookmarks.
  11. What happened?


    The bookmark for line 10 was removed.
    Expand
  12. What does "toggle" mean in this context?


    Switch back and forth between two possible values.
    Expand
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.
  1. Using the mouse only, scroll to the top of the file, select the entire file, and cut the selected text.
  2. Undo the cut.
  3. In the instructions/questions that follow, Ctrl denotes the [Ctrl] key on MS Windows keyboards and the [command] on Mac keyboards.
  4. Using the keyboard, type Ctrl-Home. Then hold the Shift and type Ctrl-End, Ctrl-X.
  5. Which approach was faster.


    For me, the keyboard was much faster.
    Expand
  6. Undo the cut.
  7. Using the keyboard, type Ctrl-a, Ctrl-X.
  8. Which approach was faster.


    There's no comparison!
    Expand
  9. Undo the cut.
  10. 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).
  11. Press Ctrl+A and then press Tab.
  12. Which approach was faster.


    For me, the keyboard was much faster.
    Expand
  13. 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.
    Expand
  14. Using the keyboard, press Ctrl+G, 10. Now, press Ctrl+Right.
  15. What happened?


    The cursor moved to the right one word.
    Expand
  16. 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.
  17. What key binding is used to delete from the cursor to the end of the line? (Hint: It's call "Clear Line".)


    Ctrl+k.
    Expand
  18. What key binding is used to select from the cursor to the end of the line?


    Shift+End.
    Expand
  19. What key binding is used to select from the cursor to the beginning of the line?


    Shift+Home.
    Expand
  20. What key binding is used to select from the cursor to the end of the word?


    Ctrl+Shift+Right.
    Expand
  21. What key binding is used to select from the cursor to the beginning of the word?


    Ctrl+Shift+Left.
    Expand

Copyright 2018