Textbook Notes (290,000)
CA (170,000)
Ryerson (10,000)
ITM (900)
ITM 200 (20)
Chapter 4

Chapter 4

Information Technology Management
Course Code
ITM 200
Vikraman Baskaran

This preview shows pages 1-2. to view the full 7 pages of the document.
Chapter 4 – Inputs and strings
4.1 Adding two numbers
If you wish to write a program which adds together 2 numbers
There some initial questions which you should ask in order to clarify what is required
oWhat do you mean by numbers?
There is more than 1 type of number in Java
Integers or floating point numbers
These are stored differently in Java
You can use the + operator to add either but you cannot store floating point
numbers in integer-type variables
oCan the numbers be negative, or will they always be positive?
oWhat do you do with the answer?
Write it into a file on the hard disk?
Write it on the screen in a dialog box?
Write it on the screen in the command window?
Use it in a further calculation?
oWill you use a graphical user interface (GUI) or command line input and output?
Let us suppose that you are going to read in 2 integers and write their sum on the screen
using methods from JOptionPane for input and output
The plan
Ask the user to enter an integer
Read the number in and store it in a variable
Ask the user for second integer
Read it in and store it in a variable
Add together the 2 numbers and store the result in a variable

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Display the result on the screen
Getting input
Class JOptionPane has a method called showInputDialog()
oIt takes a String as a parameter (but only 1 parameter this time, no need for null),
and displays it on the screen in a box
The difference is that this box contains a text field, into which the user may type a String
Parameter String is used as a prompt to the user, telling them what kind of thing the
program would like them to type
oTo read in the first number for the addition problem
oString numString1 = JOptionPane.showInputDialog(“Enter your first integer”);
oIf the user types in 42 in the text field after the box appears
oWhen user clicks OK button, the dialog box shows 42
oYou could assign the String ‘42’ to numString1 as follows
numString1 =42”;
oWhen an assignment statement is performed, the expression on the right hand side
of the = sign is evaluated, then the result is stored in the variable on the left hand
oIf the method call
JOptionPane.showInputDialog(“Enter your first integer”);
oEvaluates to ‘42’ then the assignment
numString1 = JOptionPane.showInputDialog(Enter your first integer”);
oStores this value in numString1
String variables
Variables of type int, char and double are called primitive type variables
Reference variables
You're Reading a Preview

Unlock to view full version