JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using Eclipse to Execute Code


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 (e.g., the course downloads 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. Getting Started: For this lab, you are going to add to a project that created earlier in the semester.
  1. Start Eclipse (if it is not running).
  2. If necessary, "clean up" Eclipse by clicking on File and pulling down to Close All.
  3. Expand the project named eclipseskills, expand the src directory/folder in that project, and expand the default package in that directory/folder. It should contain the source code for TripRecord.java. If not, you should complete the lab on using Eclipse to check syntax and style.
2. Adding a Compiled Class to an Existing Project: This part of the lab will help you understand source code, compiled code, and how to add a compiled class to an existing project.
  1. A .java file is said to contain "source code". What is "source code"?


    Human-readable instructions written in the Java programming language.
    Expand
  2. Where is source code kept in an Eclipse project?


    In the src directory/folder that is inside of the project directory/folder.
    Expand
  3. When a .java file is compiled, the resulting "object code" is put in a .class file. What is "object code"?


    It is, essentially, machine-readable instructions. (Actually, it is processor-independent, machine-like instructions that still need to be converted to processor-specific, machine instructions, but the distinction is unimportant for our purposes.)
    Expand
  4. Where is object code kept in an Eclipse project?


    If you setup the project correctly, in the bin directory/folder that is inside of the project directory/folder. Eclipse does not show this directory/folder, but you can see it in a file explorer or finder (depending on your operating system).
    Expand
  5. Are programmers ever given .java files that they need to include in a project?


    Yes, frequently.
    Expand
  6. Can programmers view and edit .java files that have been given to them?


    Yes, they are, after all, human-readable.
    Expand
  7. Are programmers ever given .class files that they need to include in a project?


    Yes, frequently.
    Expand
  8. Can programmers view and edit .class files that have been given to them?


    No.
    Expand
  9. Why might a programmer be given a .class file rather than a .java file?


    So that they can't view them (e.g., they might be proprietary) or edit them (e.g., so that they can't be broken or changed).
    Expand
  10. Following the instructions on the Departments Wiki, add the external directory/folder containing the .class file you downloaded above into the eclipseskills project. (Note: You can leave the .class file in the downloads folder/directory that you created for this course.)
  11. It would have been easier for you if the instructions in the previous step were included in the lab, and not on the Department Wiki. Why are these instructions on the Department Wiki instead?


    You may need to use them again and including them in the Department Wiki makes them easier to find. Indeed, that's the first place you should always look for help.
    Expand
  12. Whay aren't the instructions both on the Wiki and in the lab?


    Computer scientists hate duplication since it can lead to inconsistencies when changes are made. This is true of code, design documents, tests, etc.
    Expand
3. Adding a Source File to an Existing Project: This part of the lab will help you understand how to add a class containing source code to an existing project.
  1. Right-click on the default package, pull down to New, and across to Class.
  2. Enter Tripping in the "Name" field. (Make sure that the "Package" field is blank. It should be if you followed the previous instruction carefully. If not, erase the contents of the "Package" field.)
  3. Click on Finish.
  4. To conform to the course styleguide, add the following block comment immediately above the declaration of the class.
    /**
     * The main class for the Tripping application that displays
     * information about trips that have been taken by a fleet
     * of passenger vehicles.
     */
    
  5. To conform to the course style guide, add an @author and @version line to this block comment. What lines did you add?


     * @author NAME HERE, James Madison University
     * @version 1.0
    
    Expand
  6. To conform to the course style guide, add the required Honor Code statement. What lines did you add?


     *
     * This work complies with the JMU Honor Code.
     *
    
    Expand
  7. Add an empty main() method with a formal parameter named args to the Tripping class. What lines did you add?


          
       public static void main(String[] args) {
    
       }
    
    Expand
  8. To conform to the course styleguide, add the following block comment immediately above the main() method.
        /**
         * The entry point of the application.
         * 
         * @param args The command-line arguments (which are ignored)
         */
    
  9. Is args an instance attribute, a class attribute, a formal parameter, or a local variable?


    A formal parameter (because it is declared inside the declaration of a method).
    Expand
  10. What "kind of thing" is args?


    An array of String objects
    Expand
  11. Add a statement to the main() method that declares a TripBoard object named board. What code did you add?


    TripBoard board;
    Expand
  12. Note: If TripBoard cannot be resolved to a type, then you didn't include the .class file correctly.
  13. Is board an instance attribute, a class attribute, a formal parameter, or a local variable?


    A local variable (because it is declared inside the body of a method).
    Expand
  14. Add a statement to the main() method that declares a TripRecord object named record. What code did you add?


    TripRecord record;
    Expand
  15. Note: If TripRecord cannot be resolved to a type, then Tripping.java or TripRecord.java is not in the correct project and package.
  16. The TripBoard class has a constructor that is passed a String containing the company name. Add a statement that constructs a TripBoard object (using command-line argument 0 for the company name) and assigns it to the variable named board. What code did you add?


    board = new TripBoard(args[0]);
    Expand
  17. Add a statement that constructs a TripRecord object (with 3 passengers, 5.9 hours, and 355.0 miles), and assigns it to the variable named record. What code did you add?


    record = new TripRecord(3, 5.9, 355.0);
    Expand
  18. The TripBoard class has a method named addTripRecord() that has a single formal parameter of type TripRecord and that doesn't return anything. Add record to board using this method. What code did you add?


    board.addTripRecord(record);
    Expand
  19. What is the . in the statement you just added?


    The membership operator.
    Expand
  20. Add a statement that constructs a TripRecord object (with 2 passengers, 2.1 hours, and 143.1 miles), and assigns it to the variable named record. What code did you add?


    record = new TripRecord(2, 2.1, 143.1);
    Expand
  21. What does this assignment statement do?


    It replaces the TripRecord object that used to be in record with a different TripRecord object.
    Expand
  22. Add record to board using this method. What code did you add?


    board.addTripRecord(record);
    Expand
  23. Run Checkstyle (if it is not activated for this project), and correct all of the style defects it identifies.
4. Integrated Development Environments (IDEs): An Integrated Development Environment is more than just an editor. One of the things an IDE provides is the ability to execute code. This part of the lab will help you gain skills with running code in Eclipse.
  1. Click on run.png. What happens?


    The "Console" tab appears and indicates that an ArrayIndexOutOfBoundsException was thrown.
    Expand
  2. Describe the purpose of the formal parameter args in the main() method.


    It contains the command line arguments that were passed to the program when it was run.
    Expand
  3. What command line arguments did you pass to main()?


    I didn't pass any!
    Expand
  4. Why was the exception thrown?


    Because there isn't an element 0 of the args array because I didn't pass any command line arguments.
    Expand
  5. Click on Run+Run Configurations.... What happens?


    A dialog box appears.
    Expand
  6. Select the run configuration for "Tripping" (if there is more than one). Then, click on the "Arguments" tab of the dialog box and enter SafeRides in the "Program arguments" field. Then click Run. What happens.


    The program runs and displays information about the two trips for SafeRides. (I can't believe that they were willing to drive so far!!)
    Expand
5. Temporarily Modifying Code: When debugging, it is common to add and remove code temporarily. This part of the lab will help you get better at doing so.
  1. You've decided that the program isn't doing what it should and you're thinking of deleting code that constructs and adds the first TripRecord. However, you're not sure and you don't want to re-type them if you don't have to. So, click and drag to select those lines. What happens?


    The lines are highlighted.
    Expand
  2. Click on Source+Toggle Comment. What happens?


    Each line is now a comment.
    Expand
  3. Click on Source+Toggle Comment again. What happens?


    The lines are un-commented.
    Expand

Copyright 2022