Introduction to Computer Science - Lab 1 - Eclipse

(click for jGRASP Version)

Name_______________________________ Section ___________Score____________

Objectives:

The objectives of this lab are the following:

  1. To become familiar with the basics of Microsoft Windows XP
  2. To become familiar with the jGRASP (or Eclipse) Programming Environment and the Help System
  3. Learn about creating Java programs through the jGRASP (or Eclipse) IDE
  4. Edit, compile, and execute a simple Java program
  5. Investigate various error messages generated by the compiler
Background:

In the early days of programming, programs were written in machine language using paper and pencil. This task was very tedious and required many calculations and careful planning. The resulting program was a sequence of numbers that had to be transcribed into a machine readable format, perhaps on computer cards using a card punch machine. Errors were difficult to detect and had to be located manually. Small errors often required much time and effort to correct.

Modern programming environments have changed the way programming is done. The jGRASP and Eclipse programming environments provide an editor that supports text-processing operations required by modern programmers. It is integrated with tools that also support syntax checking, language help, and program testing and debugging. Today, most programming applications are managed using some type of software engineering tool to manage the development executables, libraries, test programs, and data sets. In the past, "Makefiles" were used to only compile and link only those source code files that were modified by a programmer. "Revision Control Systems" were used as a type of changes made for an application and to have several versions of an application readily available. Makefiles and revision control systems are still popular today, but there are many programming environments that use the concept of a "Project" to manage the development of programs. In the jGRASP and Eclipse environment, there is always an active project, yet simple programs can be created without worrying about the additional complexities afforded by the project concept.

The jGRASP and Eclipse help facility is always accessible by pressing F1. Help is available through a table of contents, an index, or through a search pane. Each help section includes keywords and related topics that may be easily explored with a click of the mouse. This type of help system is an example of the hypertext concept similar to that used by Netscape or Internet Explorer.

Experimental Procedure/ Analysis:

Task 1: In this exercise, you will learn how to create a simple Java application. This will include editing, compiling, and executing the program. To get started, you might want to take advantage of the Eclipse introductory tutorial. Start Eclipse and if the Welcome Page is not displayed, select Welcome from the Help Menu. Click the icon for the tutorial and pick the Build a Simple Java Application Tutorial.

Next you must work through the following steps:

  1. Start Eclipse on your workstation. At some point in this process, if you need to select a location for your workspace - create a folder named JavaProjects on your Z drive (or if on your own computer, pick an appropriate location).
  2. Select the Java Perspective if necessary (Under the Window menu, Open Perspective, Java)
  3. Start a new Java project (File menu, New, Project, select Java Project and click Next)
  4. Name the Project Add Test
  5. Click Finish (there is nothing to change on the next page of the wizard)
  6. Create a Java class file (File menu, New, Class)
  7. Name the class TestInteger (no spaces) and click Finish.
  8. Delete the entire contents of the template that Eclipse provides and paste the following in its place. You can copy and paste it or simply type it in. Be sure to pay close attention to upper and lower case letters and punctuation and spelling. Programming languages are very picky about such details.

    /**
       COPYRIGHT (C) 2004 Tim Margush. All Rights Reserved.
       ICS Lab 1 - Testing Integer Sizes.
       University of Akron
       @author (Your Name Here)
       @version 1.01 (date here)
    */
    public class TestInteger
    {
       public static void main(String[] args)
       {
          int Test = 100; //Set the Test value here

          System.out.print( "The value of test is " );
          System.out.println( test );
          System.out.print( "The value of test is " );
          System.out.println( test+1 );
       }
    }

  9. The Eclipse environment is always working behind the scenes to help you with the programming process. Evan as you type (or paste) the system is analyzing the program, looking for errors. You should see two lines marked with a light bulb and red X. These are lines where errors have been detected.
  10. Save the file (there is a disk icon to do this, or use Save under the File menu or Ctrl+S)
  11. Now your program will be automatically compiled (Eclipse uses the term Build instead of compile.) If you look under the Project Menu, there is a menu item named Build Automatically - it is checked if this option is on. The compiler also notes the errors and reports them in the Problems area at the bottom of the screen.
  12. You should get exactly two errors (they will be the same) - if not, carefully compare what you typed to what is printed above and recompile. Record the first error message that is found in the Problems area:

  13. _______________________________________________________________________
  14. If you double-click on an error message, the error message will be highlighted as well as the line of source code where the error occurred. What line is referenced by the second error message? (copy it here)

  15. _______________________________________________________________________
     
  16. In this case, the actual error occurs on a line preceding the point where the first error is detected. In the variable declaration, the variable name was mistyped. Correct the program by changing the uppercase 'T' to a lowercase 't'. Save your changes, and note that the Problems (and error notes) go away - This time the compile was successful.
  17. If you disable the Build Automatically, you can press a particular Ctrl key to Build manually. See if you can discover the shortcut to accomplish this task. Hint: look at the menus

    Build shortcut key: _____________________________
    Re-enable the automatic build if you disabled it.
  18. When the compile succeeds, a class file will be created. Use the Windows file explorer to look at the files in the project folder. This class file should be located in the same folder as the source file. What is the name of the class file? (You may need to change your Explorer settings to allow you to see extensuions on files - this is under Tools, Folder Options, View, Advanced Settings, uncheck Hide extensions for known file types and then click to apply this to all folders)

    _________________________________________________________________________
  19. Next we should execute (run) the program to see if it does what we intended. Under the Run menu, select Run As, and select Java Application. The output should appear in the bottom section of the screen in the Console Tab. What is the second line of output from the program?
     
     _______________________________________________________________________
  20. What happens if you change the name of the class from TestInteger to TestNumber and save the file? You might need to redisplay the Problems panel, althought the little light bulb can also provide valuable information if you click on it. What is the error message (problem)?

    ________________________________________________________________________
    What does this tell you about the class name and the file name in a Java class file?

    ________________________________________________________________________
  21. Correct the class name, then change the first occurrence of System to system. Save the file and record the error message:

    ________________________________________________________________________
  22. Fix the error you introduced by clicking the light bulb, and double-clicking the correct suggested fix.
  23. Next, remove the semicolon (;) from the end of the line where the variable test is declared. Save the file and record the error message:

    ________________________________________________________________________
  24. Fix the program before going on to the next section
Task 2: Exploring the Java documentation files.
  1. Use a web browser to locate the online Java API documentation (http://java.sun.com/j2se/1.5.0/docs/api/index.html).
  2. Click the Help link at the top of the page and then tell what API stands for:

    __________________________________________________________________
  3. Click the Index link and lookup the method: println(). (You will find several similar entries... click on the one for println(int) that is in the PrintStream class. According to the method description, what does this method do?

    _______________________________________________________________________(1 sentence)
  4. Click the link to the print(int) method and tell what method call is used by print to convert an integer to a String for printing:

    ___________________________________________________________________
  5. In the top left panel of the API documentation window, select the package named java.lang.
  6. In the left bottom panel, select the class named Integer.
  7. Documentation for this class appears in the panel to the right. Locate the description of the constant named MAX_VALUE and record its value here:

    ______________ 
  8. What is the significance of the exponent? (Hint: http://www.artima.com/underthehood/logic.html)

    _____________________________________________________________________________
  9. Use a calculator or other means to rewrite the above expression as a single integer: __________________
  10. In the program, change the literal value 100 to the largest number that can be stored in a variable of type int. Compile the program, execute it, and record the two values that are displayed:

    __________________ and ___________________
  11. Did a runtime error occur? _________________ (did you observe an error message?)

    Was the correct value for test+1 displayed? _____________

    Explain:

Finishing Up

Close the browser window and Eclipse. At this time you should move your project folder (and contents) to your network storage area (Z drive) for safe keeping. You can do this in the Lab using the file explorer. Remember to always copy the entire project (program) folder. 

You should always keep each programming project in a meaningfully named folder. If you are not in a Lab, you can use WinSCP to copy your files to your Linux account. 

After copying for safe keeping, be sure to erase your folder from the local file system.

Log Off before leaving the Lab. Remember to turn in your completed lab.

Conclusions:

In this lab we have learned the basics of Windows XP and how to create a simple Java program using the Eclipse IDE. We have also explored the Java API documentation so we can quickly get help on various topics. The on-line documentation is a necessary reference and it can help us in program development and in learning the Java language.