We have seen how to write a simple program. We saw what commands and variables are. We have seen how to assign a value to a variable. But the examples we have seen do not allow the person who runs (or executes) the program to change any variables. This person, called the user would be the person who uses our program after we finish writing it. We may want to allow him to alter the progam. Remember , once we finish writing a program, we save it to disk and can run it any number of times in the future. We may want these future runnings of the program to change according to the desire of the user. Any time you buy a software product you are a user or more exactly an end user. So how do we allow this end user to affect our program when it is running ?
Before I answer this question I need to make one more point clear. In our examples, all the values have been hard coded . That means that once the program is running they cannot be changed. If we would want to change the values we would have to stop the program and start editing the lines of the program. Carelessness in doing this could ruin our program. Therefore we must think of a way to provide for changes while the program is running and in a way that prevents careless errors from getting in. We achieve this goal by creating a user interface. Iterface is a computer science term to mean a method for using something. For example we might say that the buttons on a blender are the interface for the blender. No one would open up the blender to turn it on. The manufacturer of the blender provides us with a safe interface to the inner working of the blender. Similarly, the floor pedals of a car are the interface to the engine of the car. They provide a simple, clean and safe way to operate the car. The user interface of a program is based on the same concept. The joystick or keyboard provides an interface to a program. The joystick and keyboard are called input devices because they provide the physical means for inserting data into a program. But the programmer must decide what various inputs he wants to get from the user and what they will signify.
Similarly, we might want to show the user text or pictures on the screen (or monitor). The screen is called an output device because it provides the physical means for displaying data to the user. The programmer can decide what data he wants to display on the output device. Whenever we operate or run a program, although we may not think of it this way, we are in fact inputing data and viewing data output.
For example, let's say we want to create a simple calculator capable of adding any two numbers. We want to allow the user to decide which numbers he wants to add. So we need to ask him! We'll have to ask him for two different numbers and then print the result. Input is any information we get from the user. Output is any information we provide to the user. The two numbers he will give us in this example are the input, and the sum we print is the output.
Regarding our question of how to affect a program while it is running we can say that we must provide a means for the user to input data. The way this is done is Java is by creating a dialog box. is a small window that pops up on the screen with a message in it and a small space to type. It is called a dialog box because it allows the program to talk to (or have a dialog with) the user. What ever the user types will be receivied by the program in a variable. We will define this variable and give it a name of our choosing. After this we can use the information stored in the variable and with it whatever we want.
In the following example I have made a simple addition program. It asks the user for two integers and after he has provided them, it tells him their sum. Try running the program.
/*1*/ import javax.swing.JOptionPane; /*2*/ public class myAddNumbers{ /*3*/ public static void main( String args[]) /*4*/ { /*5*/ String Number1_as_string ; /*6*/ String Number2_as_string ; /*7*/ int Number1_as_integer; /*8*/ int Number2_as_integer; /*9*/ int sum; /*10*/ JOptionPane.showMessageDialog(null, "Welcome to my addition program."); /*11*/ Number1_as_string=JOptionPane.showInputDialog("Enter First Integer."); /*12*/ Number2_as_string=JOptionPane.showInputDialog("Enter Second Integer."); /*13*/ Number1_as_integer=Integer.parseInt(Number1_as_string); /*14*/ Number2_as_integer=Integer.parseInt(Number2_as_string); /*15*/ sum=Number1_as_integer+Number2_as_integer; /*16*/ JOptionPane.showMessageDialog(null, "Your total sum is: " + sum); /*17*/ System.exit(0); /*18*/ } /*19*/ }
Let us go through this program and explain
each line. I have added line numbers inside the symbol /* */
. The Java compiler will ignore the line number. They are
there for ease of reference. Some of the lines I will not
explain yet. You should just learn to copy them.
For example, lines 1 through 4 should be copied as is, only making one change
to line 2. the word myAddnumbers is the name of the program.
If I save my program with a different name, then
put that name in place of myAddNumbers.
Lines 7 though 9 should be clear to you. They define
the names of three integer variable I will use in a future line of the program.
Lines 5 and 6 define a type of variable we have not yet seen.
That variable is not an int, it is a string<.
Strings are variables capable of containing letter, words and sentences. I can assign them values like I can ints, but there
value will be interpretted as a set of letters. For example I can write,
String aSentence;
aSentence="This is the best website I ever visited.";
This creates a variable of type String (note: unlike int, String is always capitalized) whose name is aSentence. Then that variable
is assign the content "This is the best website I ever visited."
So far not too complicated. Using Strings is a big topic which
we will deal with in more depth later. Let's go back to lines 5
and 6 of the example program. In these lines I
declare two variables of type String.
In line 10 I use a special command to create a pop up box called a message box. Here I will display a message to the user. Everything not in blue you must simple memorize or cut and paste. The part in blue is the text of the message you want the use to see.