Writing
Interactive Programs - Activities
A
note on displaying applications using Windows rather than a text screen
Java
has a large number of classes organised into packages which are
called the Java class library or the Java Applications Programming
Interface (API). We have already usedio,
applet in
the first examples. The recommended text Deitel and Deitel uses thejavax.swing
package in the introductory chapters.
The classJOptionPane
from thejavax.swing
package is useful for its Graphical
User Interface (GUI) components that make the task of data entry into
a program or formatting (organising the display) of the output easier.The
Java GUI and event handling will be examined in greater detail in Unit
8.
The
following isGreetings.javaexample
written using thejavax.swing.JOptionPane
line
|
Source
code statements of the application GreetingsVersion2.java
|
1
2
3
4
5 |
import
javax.swing.JOptionPane;
public
class GreetingsVersion2{
public
static void main(String[] args)
{
JOptionPane.showMessageDialog(null,
"Good afternoon ");
System.exit(0);
}
} |
Line
1 is animport
statement. This statement tells the compiler
to load the classes required for compiling the program. In this case the
compiler will load theJOptionPane
class from thejavax.swing
package
Line
2 begins the class definition forclass
GreetingsVersion2.It
is important to remember that the file in which the program is saved has
to have the same name i.e.GreetingsVersion2.java
Line
3 is the method main
part of every Java application. Every
Java application must have one method calledmain.
The body of the method is enclosed within
the braces{}.
This includes the lines 4 and 5.
Line
4 sends a message showMessageDialog
toJOptionPane.
TheshowMessageDialog
is a method with two arguments, the first
of which is alwaysnull,
and the second a String to be displayed.
The method is static, which
means that it is called using class name ??method
name, rather than object
name ??method name.
Line
5 is another static method of theSystem
class used to terminate the application.
TheSystem
class is part of thejava.lang
package which is always imported by default.
Writing Interactive
Programs
The
first examples only had output, and data was either 慼ardcoded?(fixed)
or generated by the programs. We are more accustomed to interactive work,
which means input of data to a program usually from the keyboard and ?
output from the program usually on the screen.
For
example if we wish to change the TicketDiscount program so that instead
of the age being randomly generated, the user is expected to enter the
age after which the relevant message is displayed.
We
will first examine the TicketDiscount.java listed below and decide on the
changes needed to produce the new version TicketDiscountVersion2.java.
line
|
Source
code statements of the application TicketDiscount.java
|
1
2
3
4
5
6
7
8
9
|
import
java.io.*;
class
TicketDiscount {
public
static void main( String args[] )
{
int
age;
age
= (int) ( 100 * Math.random() );
System.out.println("age
is: " + age);
if(
age >= 60 )
{
System.out.println(
age + "is 60 or over" );
System.out.println("qualifies
for discount");
}
}
} |
Line
5 would need to be replaced with code what will obtain input from the user.
A prompt should be displayed on the screen telling the user that input
is expected e.g. 揈nter age: ?
Line
6 will no longer be necessary because the user input will be shown on the
screen.
Another
change would be to display a message even if there is no discount "Sorry
no discount" because with interactive programs users expect a response
from the program. For this reason the else branch of the selection
will be added.
A note on
Java input and output
In
Java all data displayed on the screen or input from the keyboard is in
the form of Strings. Strings are a series of characters treated as a single
entity and as text.
If
we need to use data in a different form, other than text e.g. numbers the
String data needs to be converted. Java has a range of parse methods,
which convert strings into a variety of types. parse methods that
convert Strings to simple/scalar/built-in data belong to wrapper classes
such as:Integer,
Float, Double, Long, Boolean, Character. To
convert a String
sto
anintwe
use the methodparseInt, a
metod of wrapper classInteger.
Line
8 reads a line of input.
Line
9 converts the input String to an integer.
line
|
Source
code statements of the application TicketDiscountVersion2.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
java.io.*;
class
TicketDiscountVersion2 {
public
static void main( String args[] ) throws IOException
{
int
age;
String
s;
BufferedReader
in =new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter
age: ");
s
= in.readLine();
age
= Integer.parseInt(s);
if(
age >= 60 )
{
System.out.println(
age + " is 60 or over" );
System.out.println("qualifies
for discount");
}
else
System.out.println(
"Sorry no discount");
}
} |
The
changes made to the program lines 3, 5, 6, 7, 8, 9, 13, 14 are shown in
boldface font.
If
the user enter 54 the output will be:
Enter age: 54
Sorry no discount
Using
the JOptionPane dialog box
|
Source
code statements of the application TicketDiscountVersion3.java
|
|
import
javax.swing.JOptionPane;
public
class TicketDiscountVersion3{
public
static void main(String[] args)
{
String
s;
s
= JOptionPane.showInputDialog("Enter age: ");
int
age = Integer.parseInt(s);
if(
age >= 60 )
JOptionPane.showMessageDialog(
null, age + " is 60
or
over\n qualifies for discount");
else
JOptionPane.showMessageDialog(null,
"Sorry no
discount");
System.exit(0);
}
} |
The
output would be as follows:
When
user inputs the number 60 and presses OK,
the
message is displayed is :
Activity
5Interactive AdditionApplet
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|