Using System Classes

Part 2:
Using JOptionPane to Input Numerical Data Types,
Using the Math Class, and
Using the DecimalFormat Class

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should be able to:
  • Describe the difference between object declaraion and object creation.
  • Explain the purpose and use of import statements.
  • Use classes from the standard Java packages (such as Date, SimpleDateFormat, String, JOptionPane, Math, GregorianCalendar, and DecimalFormat), including:
    • Using dialog windows to display and input text, and converting input String values to numerical data.
    • Writing mathematical expressions using methods in the Math class.
    • Using the GregorianCalendar class in manipulating date information such as year, month, and day.
    • Using the DecimalFormat class to format numerical data.
  • Select proper types for numerical data.
  • Write arithmetic expressions in Java.
  • Evaluate arithmetic expressions following the precedence rules.
  • Input data by using System.in and output data by using System.out.

Items in red are learning objectives for this part of the project.

Instructions:

Individual work

  • Do this project by yourself, to make sure you master the mechanics that it presents.
  • Get help as needed from your instructor and the student assistants.
  • After each change, compile and run the program, correcting errors as necessary.

UsingSystemClasses report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your UsingSystemClassesReport2.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in ? to ? minutes after seeing their instructor demonstrate the ideas.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.

    Preliminaries

  1. Create a new project using the Basic Console Application template and the project name UsingSystemClasses2.
  2. Create a file called UsingSystemClassesReport2.txt, placing it in the docs subfolder of your UsingSystemClasses2 project.
  3. Open your UsingSystemClassesReport2.txt file. Use it to record your answers to the questions in this exercise
  4. Skim this document. Then return to this point.
  5. Question: About how many minutes do you think it will take you to complete Part 2 of UsingSystemClasses?
  6. Question: What time is it now?
  7. Creating the CVS Module

  8. Create a new module for your UsingSystemClasses2 project, calling the module UsingSystemClasses2, spelled just like that.
  9. Add your report and the initial contents of your UsingSystemClasses2 project to your new CVS module.
  10. Documenting the Project

  11. Write JavaDoc comments based on the planned behavior of the project. Include all of the required tags. Ensure that you can generate the HTML documentation correctly.
  12. Using Numerical Data Types

  13. Add the following declarations to your program:
          int i = 3, j = 4, k = 5;
          float x = 34.5f, y = 12.25f;
  14. Question: What does the f in 34.5f mean? What happens if you remove it?
  15. Use JOptionPane to display the value for each of the expressions below, preceded by the String literal "The value of expression (a) is", replacing the letter in parentheses with the appropriate letter in each case. Note: Remember to import the package that includes JOptionPane.
    1. ( j / 8 ) * y
    2. ( (float) j / 8 ) * y
    3. ( j / 8.0 ) * y
    4. -x * -y
    5. ( i + j ) / k
    6. -x * -y * ( i + j ) / k
    7. x + 1.5 / 250.0 * i / j
    8. ( x + 1.5 ) / ( 250.0 * ( i / j ) )
    9. ( 3 * k - j ) % i
    10. x % y
  16. Questions:
    1. Why is the value of expression (a) 0.0?
    2. The (float) operator is an explicit type casting operator. Explain its role in expression (b).
    3. Explain why expression (c) has the same value of expression (b) even without the explicit type casting operator.
    4. Why is the value of expression (d) positive?
    5. Why is the value of expression (e) 1 instead of 1.4?
    6. Given that the value of the expression -x * -y is 422.625, and the value of ( i + j ) / k is 1 due to the truncation that occurs during integer division, why is the value of expression (f) 591.675?
    7. The correct value of expression (g) is 34.5045. Explain why the following statement displays the incorrect output 34.50.0045000000000000005?
            JOptionPane.showMessageDialog(
               null,
               "The value is "
               + x + 1.5 / 250.0 * i / j
               );
    8. Why is the value of expression (h) Infinity?
    9. Why are the parentheses necessary in expression (i)?
    10. How could you use the modulus operator (i.e. %) to find the fractional part of a float variable?
  17. Comment out the statements that create the dialog windows. An easy way to do that in JCreator Pro is to select (highlight) all of the statements and press Ctrl-M. Make sure that your program still compiles and runs (it shouldn't produce any output, though).
  18. Question: Suggest one reason to comment the statements out instead of deleting them.
  19. Using JOptionPane to Input Numerical Data

  20. Add a statement that causes the dialog window below to appear. The statement should also assign the text that the user enters as the value of a String variable called in.

  21. Question: What happens if you enter non-numeric data as the value of coeffient a?
  22. Add the following statement following the one you just added:
          double a = Double.parseDouble( in );
  23. Question: What happens if you pass non-numeric data to the Double.parseDouble() method? We will learn later how to deal with input errors like this.
  24. Display the value of the variable a in a dialog window like this one:

  25. Question: How many digits of the input are displayed in the output?
  26. Add the necessary statements to input values for variables b and c, which should also be of type double. Use one dialog to display the values of a, b, and c once they have all been input, but display them on separate lines.
  27. Question: How do you display the values on separate lines?
  28. Add a new variable D of type double and assign it the value of the expression b * b - 4 * a * c. Modify your output dialog so that it also displays the value of D.
  29. Question: What is the value of D given the inputs a = 1.5, b = -12, and c = 22.5?
  30. Using the Math Class

  31. Add a new statement that creates a dialog window to display the value of the expresssion Math.sqrt( D ).
  32. Question: What is the value of Math.sqrt( D ) given the inputs a = 1.5, b = -12, and c = 22.5?
  33. Add new variables root1 and root2 of type double and assign them the values of the expressions (-b + Math.sqrt( D ) ) / ( -2 * a ) and (-b - Math.sqrt( D ) ) / ( -2 * a ).Modify the dialog window that you just added so that it also displays the values root1 and root2.
  34. Question: What are the values of root1 and root2 given the inputs a = 1.5, b = -12, and c = 22.5?
  35. Modify the dialog window so that it also displays the value of the expression Math.PI.
  36. Question: What is the seventh digit of Pi (the sixth after the decimal point)?
  37. Using the DecimalFormat Class

  38. Add the following statement before the ones that create the output dialog windows:
          		DecimalFormat df = new DecimalFormat( "0.000000" );
  39. Question: What compiler error do you get when you attempt to declare and create a DecimalFormat object, and what do you need to do to fix the problem?
  40. Fix the compiler error. Then add a statement that displays the value of the expression df.format( Math.E ).
  41. Question: How would you change the number of digits of e that are displayed after the decimal point?
  42. Use the df.format method to display all of the numbers in your output dialog windows to six digits of precision.
  43. Question: What is now displayed as the seventh digit of Pi (the sixth after the decimal place)?
  44. Question: Does the df.format method truncate or round?
  45. Using Standard Output and Standard Input

  46. Comment out the statement that creates the dialog window that prompts the user for the value of the coefficient a. Replace it with these:
          System.out.println ( "What is the value of coefficient a?" );
    
          BufferedReader streami = new BufferedReader(
             new InputStreamReader(System.in));
  47. Question: What compiler error do you get when you attempt to declare and create a BufferedReader object, and what do you need to do to fix the problem?
  48. Fix the compiler error. Then add the following statement:
          String inA = streami.readLine();
  49. Question: What compiler error do you get when you attempt to use the readLine method of your fancy new BufferedReader object?
  50. We'll talk more about exceptions later in the course. For now, just replace the line
          public static void main(String args[]) {
    with the line
          public static void main(String args[]) throws IOException {
    In other words, add the throws IOException clause to the declaration of the main method. You'll need to do this for any method that uses standard input.
  51. Question: Where do the input and output for the coefficient a happen now?
  52. Comment out the statements that create the other dialog windows, and replace them with the statements necessary to use standard input and output. Note that when using standard output, it is customary to use separate output statements instead of the \n character.
  53. Question: Suggest one reason for using standard input and output instead of dialog windows.
  54. Question: Suggest one reason for using dialog windows instead of standard input and output.
  55. Submitting the Project

  56. Make any final changes that you want to your project.
  57. Generate the HTML documentation for your project.
  58. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  59. Tag your CVS module UsingSystemClasses2.
  60. Summary

  61. Question: What method do you use to convert a String object to a double value?
  62. Question: What class provides mathematical functions beyond simple arithmetic operations?
  63. Question: What method do you use to format numerical output?
  64. Question: Describe the process of reading a double value from the standard input.
  65. Question: How much time did you spend on Part 2 of UsingSystemClasses? Compare your answer to how much time you had estimated that you would spend. You receive full credit no matter how far off your estimate is!