|
Learning objectives:After completing this project, you should be able to:
Items in red are learning objectives for this part of the project. |
Individual work
| UsingSystemClasses report
| Time limit
|
UsingSystemClasses
. UsingSystemClassesReport.txt
,
placing it in the docs
subfolder of the project that you just created.
UsingSystemClassesReport.txt
file.
UsingSystemClasses
project,
calling the module UsingSystemClasses
, spelled just like that.
UsingSystemClasses
project to your new CVS module.
JOptionPane
for Output 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.
javax.swing.JOptionPane.showMessageDialog( null, "Welcome to Prof. Merkle's UsingSystemClasses program." );
showMessageDialog
method do?
System.exit( 0 );
System.exit(0)
statement. Would they ever be exectued?
javax.swing.from the beginning of the statement that causes the dialog box to appear (you should remove the period between
swing
and JOptionPane
).
import javax.swing.JOptionPane;
import
statement.
import
statement by replacing JOptionPane
with a single asterisk. import
statement tell the
compiler?
As the first dialog implied,\nthis program was written by Prof. Merkle
\n
have on the way the message is
displayed? JOptionPane
class in JDK Help.
javax.swing
) to import in order to be able to
access a class? String
Variables 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.
main
method:
String author;
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).
new
operator, and the assignment statement.Date
Class 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. Date
object without importing the appropriate package?
Date
class in JDK help.Date
classes?
import
statements to your program to give you access to both Date
classes.
Date
constructor that
allocates a
Date
object and initializes it so that it represents the time at which it was allocated?
import
statement that allows initialization of a Date
object to
represent the time at which it is initialized. Remove the other one.String
argument as follows:
"Welcome to " + author + "'s UsingSystemClasses program.\n" + startTime.toString()
startTime
variable
when it has been declared but not assigned cause a compilation error?startTime
object just before the statement that
displays
the first dialog, as follows:
startTime = new Date();
toString
method of the Date
class. startTime
object? endTime
object just before the statement that displays the final dialog, and
display the value in the dialog.
startTime
and endTime
objects?SimpleDateFormat
Class SimpleDateFormat
.
Use JDK help to find out what package contains that class, and add an import
statement for that package. SimpleDateFormat
class?SimpleDateFormat
object called theFormat
. theFormat
, as follows:
theFormat = new SimpleDateFormat();
theFormat
(the object that you just declared and assigned) to format startTime
by replacing
startTime.toString()with
theFormat.format( startTime )
String
objects resulting from using the default SimpleDateFormat
value to format Date
objects?theFormat
to format endTime
.
theFormat
a more interesting value, as follows:
theFormat = new SimpleDateFormat( "EEEE, hh:mm:ss a" );
String
object used to initialize
theFormat
.
String
Variables, Revisited 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.
JOptionPane
for InputshowInputDialog
methods of the JOptionPane
class.showInputDialog
methods does the JOptionPane
class have? What do they do?
JOptionPane.showInputDialog( null, "Welcome. What is your name?" );
String userName = JOptionPane.showInputDialog( null, "Welcome. What is your name?" );
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 ) );
UsingSystemClasses1
.new
operator do? What is special about it's use in the context of String
objects? import
so that you can create dialog windows?
String
input?