Using System Classes

Part 1:
JOptionPane, Date, and SimpleDateFormat,

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 UsingSystemClassesReport.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in 105 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.

    Creating the Project and Report

  1. Create a new project using the Basic Console Application template (not the Basic Swing Application template)and the project name UsingSystemClasses.
  2. Download UsingSystemClassesReport.txt, placing it in the docs subfolder of the project that you just created.
  3. Open your UsingSystemClassesReport.txt file.
  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 1 of UsingSystemClasses?
  6. Question: What time is it now?
  7. Creating the CVS Module

  8. Create a new module for your UsingSystemClasses project, calling the module UsingSystemClasses, spelled just like that.
  9. Add your report and the initial contents of your UsingSystemClasses 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 System Classes

    Using JOptionPane for Output

  13. Add the following statement to the main method to display an introductory message:
          System.out.println( "Welcome to Prof. Merkle's UsingSystemClasses program." );
    Note: Use your instructor's name, not yours. You'll replace the name with yours later.
  14. Replace the statement that you just added with the following statement, so that the introductory message is displayed in a dialog:
          javax.swing.JOptionPane.showMessageDialog( 
             null, 
             "Welcome to Prof. Merkle's UsingSystemClasses program." 
             );
  15. Compile and run the program (you should do this after each change, even though the instructions will not tell you to do so explicitly after this). A dialog box should appear and display your message. When you click the OK button, the dialog box should close.
  16. Question: What does the showMessageDialog method do?
  17. Question: What happens if you try to run the program a second time? What if you try to recompile it after you've run it?
  18. If you can't run the program a second time or recompile it after you've run it, then the program is still running, even though the dialog has closed (yes, this is somewhat annoying). Press CTRL-B to stop the program. Then add the following statement at the end of the main method so that the program will terminate correctly:
          System.exit( 0 );
  19. Question: Suppose you added more statements after the System.exit(0) statement. Would they ever be exectued?
  20. Remove the
          javax.swing. 
    from the beginning of the statement that causes the dialog box to appear (you should remove the period between swing and JOptionPane).
  21. Question: What error message do you now get when you try to compile the project?
  22. Add the following statement immediately after the header comment:
          import javax.swing.JOptionPane;
  23. Question: Explain the role of the import statement.
  24. Change the import statement by replacing JOptionPane with a single asterisk.
  25. Question: What does an asterisk in an import statement tell the compiler?
  26. Add another statement that displays a second dialog with the following message:
          As the first dialog implied,\nthis program was written by Prof. Merkle
  27. Question: What effect does the \n have on the way the message is displayed?
  28. Look up the JOptionPane class in JDK Help.
  29. Question: How can you find out what package (e.g. javax.swing) to import in order to be able to access a class?
  30. Declaring, Assigning, and Referencing String Variables

  31. Question: Does it seem fair that you are doing all the typing and the program still says that Prof. Merkle wrote it?
  32. Replace the String literal in the statement that displays the first dialog by the following String expression:
          "Welcome to " + author + "'s UsingSystemClasses program.
    Make a similar change to the statement that displays the final dialog.
  33. Question: What compilation error do you get when you reference a variable that has not been declared?
  34. Add the following statement as the first one in the main method:
          String author;
  35. Question: What compilation error do you get when you declare a variable and reference it before it has been initialized?
  36. Add the following statement after the declaration that you just added:
          author = new String( "[author]" );
    replacing [author] with your name. As an aside, the syntactic structure of this statement works for objects of all classes (though the types of the arguments vary). At the end of this exercise you'll see a syntactically simpler statement that does the same thing, but the syntax only works with String objects).
  37. Question: Explain the roles of the declaration statement, the new operator, and the assignment statement.
  38. Using the Date Class

  39. At the beginning of the main method (somewhere before the statement that displays the first dialog), declare two Date objects called startTime and endTime. Remember, a declaration statement specifies a datatype and one or more variable names.
  40. Question: What error message do you get when you declare a Date object without importing the appropriate package?
  41. Look up the Date class in JDK help.
  42. Question: What packages contain Date classes?
  43. Add import statements to your program to give you access to both Date classes.
  44. Question: What error message do you get when you reference a class that is defined in more than one imported package?
  45. Question: Which package provides a Date constructor that allocates a Date object and initializes it so that it represents the time at which it was allocated?
  46. Keep the import statement that allows initialization of a Date object to represent the time at which it is initialized. Remove the other one.
  47. Modify the statement that displays the first dialog so that it also attempts to display the time at which the program started, by replacing the String argument as follows:
          "Welcome to " 
          + author 
          + "'s UsingSystemClasses program.\n" 
          + startTime.toString()
  48. Question: Why does referencing the startTime variable when it has been declared but not assigned cause a compilation error?
  49. Fix the error by assigning a value to the startTime object just before the statement that displays the first dialog, as follows:
          startTime = new Date();
  50. Question: List eight pieces of information that are included in the value returned by the toString method of the Date class.
  51. Question: What time is represented by the startTime object?
  52. Assign a value to the endTime object just before the statement that displays the final dialog, and display the value in the dialog.
  53. Question: What event's duration has the largest impact on the difference between the times represented by the startTime and endTime objects?
  54. Using the SimpleDateFormat Class

  55. In order to change the format of the dates, you need to create an object of type SimpleDateFormat. Use JDK help to find out what package contains that class, and add an import statement for that package.
  56. Question: What package contains the SimpleDateFormat class?
  57. Declare a SimpleDateFormat object called theFormat.
  58. Assign the default value to theFormat, as follows:
          theFormat = new SimpleDateFormat();
  59. Use theFormat (the object that you just declared and assigned) to format startTime by replacing
          startTime.toString()
    with
          theFormat.format( startTime )
  60. Question: What are the six pieces of information that are included in the String objects resulting from using the default SimpleDateFormat value to format Date objects?
  61. Use theFormat to format endTime.
  62. Assign theFormat a more interesting value, as follows:
          theFormat = new SimpleDateFormat( "EEEE, hh:mm:ss a" );
  63. Question: Explain the meaning of each part of the String object used to initialize theFormat.
  64. Assigning String Variables, Revisited

  65. String objects get preferential treatment by Java compilers. Replace the statement that initializes the author variable with this one:
          author = "[author]";
    again replacing [author] with your name.
  66. Using JOptionPane for Input

  67. Look up the JDK Help for the showInputDialog methods of the JOptionPane class.
  68. Question: How many showInputDialog methods does the JOptionPane class have? What do they do?
  69. Add the statement below to display an introductory message and prompt the user for their name:
          JOptionPane.showInputDialog( null, "Welcome.  What is your name?" );
  70. Question: What is the return type of the method call in the statement that you just added?
  71. Replace the statement that you just added with the one below, so that the user's name will be available for use later in the program:
          String userName = JOptionPane.showInputDialog( null, "Welcome.  What is your name?" );
  72. Personalize the two showMessageDialog method calls by including the user's name in the message. For example, you might replace the first one with this statement:
          JOptionPane.showMessageDialog( 
             null, 
             "Hi "
             + userName
             + ". Welcome to " 
             + author 
             + "'s FromScratch2 program.\nIt is now " 
             + theFormat.format( startTime )
             );
  73. Submitting the Project

  74. Make any final changes that you want to your project.
  75. Generate the HTML documentation for your project.
  76. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  77. Tag your CVS module UsingSystemClasses1.
  78. Summary

  79. Question: What does the new operator do? What is special about it's use in the context of String objects?
  80. Question: Which package must you import so that you can create dialog windows?
  81. Question: What class do you use to create dialog windows?
  82. Question: What method do you use to create a dialog window to display a message?
  83. Question: What method do you use to create a dialog window to display a message and prompt the user for a String input?
  84. Question: How much time did you spend on Part 1 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!