JMU
Lab: Gaining Experience with Functions and Separate Compilation


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Review: This part of the lab will give you experience with some things you should already know and understand.
  1. Read and understand SpeedPrinter.java.
  2. The line public static final double MILES_PER_KILOMETER = 0.621371; declares and initializes a:
    Function that is passed a String
    Variable that can hold a double
    Constant that holds a double
    All of the above
    None of the above
  3. kph is a:
    Function that is passed a String
    Variable that can hold a double
    Constant that holds a double
    All of the above
    None of the above
  4. toNonnegativeDouble() is a:
    Function that is passed a String
    Variable that can hold a double
    Constant that holds a double
    All of the above
    None of the above
  5. / is a:
    Function that is passed a String
    Variable that can hold a double
    Binary arithmetic operator
    All of the above
    None of the above
  6. Which line contains the declaration of the SpeedPrinter class?
    5
    6
    15
    25
    None of the above
  7. Which line indicates the start of the body of the SpeedPrinter class?
    5
    6
    15
    25
    None of the above
  8. Which line indicates the end of the body of the SpeedPrinter class?
    5
    6
    15
    25
    None of the above
  9. In jGRASP, what happens if you hover the mouse cursor over an open or close curly bracket?

  10. Which line contains the declaration of the main() function?
    5
    15
    16
    25
    None of the above
  11. Which line indicates the start of the body of the main() function?
    5
    15
    16
    25
    None of the above
  12. Which line indicates the end of the body of the main() function?
    5
    6
    15
    25
    None of the above
  13. Compile SpeedPrinter.java.
  14. Execute SpeedPrinter with a single command line argument of 100.0
  15. What value is printed?

  16. Do you need to compile SpeedPrinter.java before you can execute it again?
    Yes
    No
    Maybe
    All of the above
    None of the above
  17. Execute SpeedPrinter with two command line arguments, 60.0 and 70.0
  18. What value is printed?

  19. Why aren't two values printed?

  20. Change the body of the main() method so that it contains the following code:
           double         kph;
           double         mph;
    
           mph = Text.toNonnegativeDouble(args[0]);
           kph = mph / MILES_PER_KILOMETER;       
    
           System.out.println(kph);       
    
    
           mph = Text.toNonnegativeDouble(args[1]);
           kph = mph / MILES_PER_KILOMETER;       
    
           System.out.println(kph);       
        
  21. Save SpeedPrinter.java.
  22. Why do you now need to compile SpeedPrinter.java (even though you compiled it earlier)?
    Because the source code changed
    Because the .class file is now out of date
    Because the new changes won't be executed if I don't
    All of the above
    None of the above
  23. Compile SpeedPrinter.java.
  24. Execute SpeedPrinter with two command line arguments, 60.0 and 70.0
  25. What values are printed?

2. Functions and Modularity: This part of the lab will give you experience with creating a function and making algorithms more modular.
  1. What is bad about the new version of SpeedPrinter.java?
    Code duplication
    Code duplication
    Code duplication
    Code duplication
    Code duplication
  2. What is one important way to reduce code duplication?
    Move the duplicate code to a function
    Delete the duplicate code
    Ignore the duplicate code
    All of the above
    None of the above
  3. Add the following function to the SpeedPrinter class (in an appropriate spot):
        /**
         * A function for converting a speed in mi/hr to
         * a speed in km/hr and printing the result
         *
         * @param   text   A String representation of the speed in mi/hr
         */
        public static void printKPH(String text)
        {
           double         kph;
           double         mph;
           
           mph = Text.toNonnegativeDouble(text);
           kph = mph / MILES_PER_KILOMETER;       
           System.out.println(kph);
        }
        
  4. Why is printKPH() declared to be void?
    It doesn't do anything
    It has no parameters
    It doesn't return anything
    All of the above
    None of the above
  5. What is passed to printKPH()?
    A String
    Nothing, it has no parameters
    A double value
    All of the above
    None of the above
  6. Remembering your answer to the questions above about the start and end of the body of main(), replace the body of the main() method with the following:
           SpeedPrinter.printKPH(args[0]);
           SpeedPrinter.printKPH(args[1]);
        
  7. Compile SpeedPrinter.java.
  8. Execute SpeedPrinter with two command line arguments, 60.0 and 70.0
  9. What values are printed?

  10. Delete the class name and the period from the calls to printKPH(). That is, change those two lines to the following:
           printKPH(args[0]);
           printKPH(args[1]);
        
  11. Compile SpeedPrinter.java. Note that there are no errors.
  12. Why are there no errors? In other words, what does this tell you about calling a function that is in the same class?

  13. It is important to be able to trace the execution of a program line-by-line. Given the following implementation of the SpeedPrinter class, trace the execution when it is run with a single command-line argument.
    public class SpeedPrinter
    {
        static final double MILES_PER_KILOMETER = 0.621371;
    
        
        public static void printKPH(String text)     // 1
        {
           double         kph;
           double         mph;
           
           mph = Text.toNonnegativeDouble(text);     // 2
           kph = mph / MILES_PER_KILOMETER;          // 3
           System.out.println(kph);                  // 4
        }
    
    
        public static void main(String[] args)       // 5
        {
           printKPH(args[0]);                        // 6
           printKPH(args[1]);                        // 7
        }
    }
    

    Specifically, list (the numbers of) all of the statements as they will be executed. (Hint: The main() function is called the entry-point.)


3. Separate Compilation: This part of the lab will give you experience with creating a utility class that contains a function that might be used in multiple applications and compiling different files.
  1. Read and understand SpeedSetter.java
  2. Why is toKPH() declared to be a double?
    It is passed a double value
    It returns a double value
    It has two parameters
    All of the above
    None of the above
  3. Note that the call to toKPH() in main() does not include the class name.
  4. Based on what you learned earlier, why will SpeedSetter compile without errors in spite of this?
    toKPH() is in the same class as main()
    toKPH() has a descriptive name
    toKPH() doesn't do anything
    All of the above
    None of the above
  5. Compile SpeedSetter.java
  6. Create a new file named Converter.java that contains the following:
    /**
     * A utility class that can be used to convert values of
     * various kinds
     */
    public class Converter
    {
    
    
    
    }
        
  7. Cut the line declaring and initializing the constant in SpeedSetter.java and paste it in the first blank line in the Converter class.
  8. Cut the function named toKPH() (and its comments) in SpeedSetter.java and paste it in the last blank line in the Converter class.
  9. Compile Converter.java (It should compile without errors.)
  10. Compile SpeedSetter.java
  11. What error was generated?

  12. Why was this error generated?

  13. In SpeedSetter.java, change the call to toKPH() to the following:
           kph = Converter.toKPH(mph);       
        
  14. Compile SpeedSetter.java (It should now compile without any errors.)
4. More on Using Functions in Other Classes: This part of the lab will give you more experience with using functions in other classes.
  1. The Dashboard class contains a function named setSpeed() that is passed a double containing a speed in km/hr and displays it on a speedometer.
  2. Modify SpeedSetter.java so that, instead of printing the value in kph it passes it to the setSpeed() function in the Dashboard class.
  3. What line(s) did you delete?
    mph = Text.toNonnegativeDouble(args[0]);
    kph = Converter.toKPH(mph);
    System.out.println(kph);
    All of the above
    None of the above
  4. What line(s) did you replace it/them with?
    mph = toNonnegativeDouble(args[0]);
    kph = toKPH(mph);
    Dashboard.setSpeed(kph);
    All of the above
    None of the above
  5. Compile SpeedSetter.java (It should compile without any errors.)
  6. Execute SpeedPrinter with a single command line argument of 60.0
  7. What happened?

5. Common Mistakes: This part of the lab will give you experience with some common mistakes made by beginning programmers.
  1. Add the following code to the Converter class in between the } that ends the method toKPH() and the } that ends the class.
        /**
         * Multiply a speed by 2
         */
        public static double double(double mph)
        {
           return 2.0 * mph;   
        }
        
  2. Compile Converter.java
  3. What errors were generated?

  4. Why were these errors generated?

  5. Change the name of the method to twice() (i.e., change the second double to twice).
  6. Compile Converter.java (It should compile without errors.)
  7. Add a semi-colon to the end of the declaration of the twice() function. In other words, your code should now look like:
        public static double twice(double mph);
        {
           return 2.0 * mph;   
        }
        
  8. Compile Converter.java
  9. What errors were generated?

  10. Why were these errors generated?

  11. Move the method named twice() to below the last }.
  12. Compile Converter.java
  13. What errors were generated?

  14. Why were these errors generated?

  15. Delete the method named twice().
  16. Compile Converter.java (It should compile without errors.)

Copyright 2013