JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using Command Line Arguments in jGRASP


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 and then selecting Save as... or Save link as....)

1. Review: This part of the lab will help you review some concepts/terms that you should already know.
  1. What are the command line arguments called in WhatAnEgo.java? In other words, what formal parameter of the main() method will hold the command line arguments?


    args
    Expand
  2. What is the type of the formal parameter that holds the command line arguments?


    String[] (i.e., an array of String objects)
    Expand
  3. How do you access an element of an array?


    Using the [] operator.
    Expand
  4. How do you access element 0 of the array named args?


    args[0]
    Expand
  5. The Text class has a method with the following signature:
        public static double toNonnegativeDouble(String s)
        

    that converts a String representation of a non-negative double contained in the parameter named s to a non-negative double value and returns it. In the event that s does not represent a non-negative double it will return the value -1.0 instead.

  6. How would you invoke this method, passing it the String literal "5.9"?


    Text.toNonnegativeDouble("5.9")
    Expand
  7. How would you invoke this method, passing it the 0th element of a String[] named args?


    Text.toNonnegativeDouble(args[0])
    Expand
  8. Change the declaration of percentage, rank, and size so that they are now double values. What do those statements look like now?


    double percentage;
    double rank;
    double size;
    Expand
2. Processing Command Line Arguments: This part of the lab will help you gain the skills you need to process command line arguments.
  1. Change the initialization of the variable named size so that, instead of being assigned 1100, it is assigned the double value of the 0th element of the command line arguments. What is the modified assignment statement?


    size = Text.toNonnegativeDouble(args[0]);
    Expand
  2. Change the initialization of the variable named size so that, instead of being assigned 1, it is assigned the double value of the 1st element of the command line arguments. What is the modified assignment statement?


    rank = Text.toNonnegativeDouble(args[1]);
    Expand
  3. Compile the class. You should not get any syntax errors. If you do, correct them. (Don't worry about any style defects.)
3. Entering Command Line Arguments in jGRASP: This part of the lab will help you gain the skills you need to enter command line arguments using jGRASP.
  1. Execute the program. What error message did you get?


    ArrayIndexOutOfBoundsException in line 18.
    Expand
  2. What element of what array is being accessed in this line?


    Element 0 of the array args (i.e., args[0]).
    Expand
  3. What is this message telling you?


    There is no such element of the args array.
    Expand
  4. Why is this the case?


    I didn't provide any command line arguments!
    Expand
  5. Click on Build-Run Arguments. What happened?


    A text field appeared below the menu bar.
    Expand
  6. Enter 1100.0 1.0 in the "Run Arguments" text field.
  7. Execute the program. Did you get the correct output?


    I did. He really is great! I'm so glad I'm taking CS149 with him.
    Expand
  8. Try running the program with different command line arguments to make sure you understand the process.
  9. Do you need to re-compile the program when you change the command line arguments?


    No, none of the code has changed. The only thing that has changed is the information that will be provided to the program when it is executed.
    Expand

Copyright 2020