Java Reference
In-Depth Information
Double.parseDouble(strExpression)
where
strExpression
is an expression containing a numeric string.
21
.
The method
showInputDialog
of the
class
JOptionPane
is used to
create an input dialog box.
22
.
The method
showMessageDialog
of the
class
JOptionPane
is used to
create an output message dialog box.
23
.
The
class
JOptionPane
is contained in the
package
javax.swing
.
24
.
If a program uses input and output dialog boxes, it must also use the statement:
System.exit(0);
25
.
To format a floating-point number to a specific number of decimal places,
you can use the
String
method
format
.
26
.
To input data from a file, you use the
class
es
Scanner
and
FileReader
;
to send output to a file, you use the
class
PrintWriter
.
3
27
.
File I/O is a four-step process: (i) import the necessary classes from the
package
s
java.util
and
java.io
into the program; (ii) create and
associate the appropriate objects with the input/output sources; (iii) use
the appropriate methods associated with the objects created in Step ii to
input/output the data; and (iv) close the file(s).
EXERCISES
1.
Mark the following statements as true or false.
a.
A variable declared using a
class
is called an object.
b.
In the statement
x
ΒΌ
console.nextInt() ;
,
x
must be a variable.
c.
You generate the newline character by pressing Enter (return) on the
keyboard.
d.
The methods
printf
and
format
are used to format a decimal number
to a specific number of decimal places.
2.
How does a variable of a primitive type differ from a reference variable?
3.
What is an object?
4.
What does the operator
new
do?
5.
Suppose that
str
is a
String
variable. Write a Java statement that uses the
operator
new
to instantiate the object
str
and assign the string
"Java
Programming"
to
str
.
6.
What is garbage collection? Write the statement that instructs the Java
system to (immediately) perform garbage collection.
7.
Which package contains
class
String
? If a program uses this class, explain
why it is not necessary to explicitly import this class using the import statement.
Search WWH ::
Custom Search