JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using VSCode 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 lab that you completed earlier in the semester.
  1. Start VSCode (if it is not running).
  2. Create a directory/folder for this lab (e.g., named lab02) under the src directory.
  3. Copy TripRecord.java to the directory/folder that you just created. (Note: This version is different from the version that you might have used in an earlier lab.)
  4. Start VSCode.
  5. Click on explorer.png in the activity bar. (This will open the explorer view in the primary side bar.)
  6. Expand CS159, src, and the directory/folder you created for this lab.
  7. Open an editor containing TripRecord.java by double-clicking on the file with that name in the explorer view. yes
  8. Modify the package statement so that it is consistent with the directory/folder you created for this lab (e.g., package lab02;) and save the file.
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 VSCode?


    Under the src 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 that is compiled by VSCode kept?


    In the bin directory/folder.
    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. Suppose a third-party provider of .class files wants to organize/group them, how can they do it?


    By creating a .jar file (which is, essentially, a .zip file that contains one or more .class files organized in packages.
    Expand
  11. Where does VSCode keep "third-party" .jar files?


    In the lib directory.
    Expand
  12. Outside of VSCode, copy the file named trips.jar that you downloaded earlier into the lib directory for this course. It contains a file named TripBoard.class.
3. Adding a Source File to an Existing Project: This part of the lab will help you understand how to create a file and add source code to it.
  1. In the VSCode explorer, right-click on the directory/folder that you created for this lab (that is under the src directory/folder) and pull down to New File....
  2. Enter Tripping.java in the text field.
  3. Note that VSCode inserts the appropriate package statement and class declaration.
  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. You should get a syntax error that tells you that TripBoard cannot be resolved to a type because you have not told the compiler where to find the .class file. To resolve this error, add the following import statement below the package statement.
    import lab02.gui.TripBoard;

    VSCode looks in the lib directory/folder for .jar files, and the TripBoard class is in the package lab02.gui in trip.jar.

  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 rec. What code did you add?


    TripRecord rec;
    Expand
  15. Note: If TripRecord cannot be resolved to a type, then Tripping.java or TripRecord.java is not in the correct directory/folder.
  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 rec. What code did you add?


    rec = 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 rec to board using this method. What code did you add?


    board.addTripRecord(rec);
    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 rec. What code did you add?


    rec = 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 rec with a different TripRecord object.
    Expand
  22. Add rec to board using this method. What code did you add?


    board.addTripRecord(rec);
    Expand
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 VSCode.
  1. Click on run.png (near the top right of the VSCode window). What happens?


    The "Terminal" tab appears. It shows what command was used to execute the Java program and displays the output of that program. The output 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. Copy the command that was used to run the Java program (it ends in lab02.Tripping) and paste it into last line of the Terminal window. Then, type a space and the command line argument SafeRides and press Return. 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
  6. Close the window that just appeared to terminate the program.
  7. In the Terminal window, press the up-arrow key. What happens?


    The previous command appears. (Boy, that can save a lot of typing!)
    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 Edit+Toggle Line Comments. What happens?


    Each line is now a comment.
    Expand
  3. Click on Edit+Toggle Line Comments again. What happens?


    The lines are un-commented.
    Expand

Copyright 2024