Download presentation
Presentation is loading. Please wait.
1
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and strings
2
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 2 Motivation So far, our programs have been limited in the sense that all their data must be defined within the program source code. We will now learn how to write programs which enable data to be entered via the keyboard, while the program is running. Such programs can be made to operate upon different data every time they run, making them much more flexible and useful. We will also learn more about strings, which are a powerful tool in Java programming.
3
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 3 The problem We wish to write a program which adds together two numbers input by the user Initial questions –What do we mean by numbers? –What do we do with the answer? –Shall we use swing (GUI) or command line input?
4
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 4 The plan Ask the user to enter a number Read the number in and store it in a variable Ask the user for a second number Read it in and store it in a variable Add together the two numbers and store the result in a variable. Display the result on the screen.
5
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 5 Doing input Class JOptionPane has a method called showInputDialog As with method showMessageDialog, this method takes a string as a parameter, 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. The parameter string is used as a prompt to the user, telling them what kind of thing to type.
6
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 6 Doing input When the user clicks the box’s OK button, the string of text s/he typed is returned as the result of the showInputDialog method. To use this string value in our program, we may store it in a string variable Example: to read in our first number String firstNumber;. firstNumber = JOptionPane.showInputDialog("Enter first integer");
7
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 7 Strings A string in Java is an object This means that, in addition to its value (a sequence of characters), a string also has methods associated with it These methods are defined in the class String, which is in package java.lang This makes strings a very powerful tool in Java programming; more on this later firstNumber is a String reference variable; the assignment makes it refer to the string typed by the user
8
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 8 Problem solved?? Can we now do the same for another variable, secondNumber, and simply add firstNumber and secondNumber together? If not, why not? What’s the result of firstNumber + secondNumber
9
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 9 Primitive types and object references The types int, char and double are called primitive types. Variables of these types may be thought of as named boxes in memory into which we may place integer, character and floating point values respectively. However, other variables may be defined to be references to objects of a specified class. We have already met the class String Another class in the package java.lang is Integer
10
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 10 Converting strings to integers The class Integer has a static method called parseInt which converts a String argument into an int value Now, if we define an int type variable int number1; we can then store the int equivalent of the String firstNumber in this variable as follows number1 = Integer.parseInt(firstNumber);
11
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 11 The end of the road? Almost! We can now use assignment to add our two int variables together and store the result in a third int variable. Sum = number1 + number2; We then write out the result in a message dialog box (or use println) JOptionPane.showMessageDialog(null, “The sum is “+ sum, “Results”, JOptionPane.PLAIN_MESSAGE);
12
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 12 NOTE: 4 parameters The two extra parameters are “Results” –a message for the box’s title bar JOptionPane.PLAIN_MESSAGE –indicates no icon to be displayed with the message.
13
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 13 The program: Declarations // Fig. 2.9: Addition.java // An addition program import javax.swing.JOptionPane; // import class JOptionPane public class Addition { public static void main( String args[] ) { String firstNumber, // first string entered by user secondNumber; // second string entered by user int number1, // first number to add number2, // second number to add sum; // sum of number1 and number2
14
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 14 The program: Getting the numbers // read in first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); }
15
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 15 The program: Calculating and displaying the result // add the numbers sum = number1 + number2; // display the results JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate the program }
16
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 16 Dry running We can test our program without using a computer by dry running it on paper We act as the computer, following the instructions of the program, recording the values of the variables at each stage This is usually easier than getting the program to run on a computer –don’t need to worry whether details of syntax are correct –just concentrate on the logic of the algorithm –when we’re happy with the dry run tests, we try it on a computer
17
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 17 Further work Get this program from the Deitel and Deitel CD - it’s figure 2.9 in Chapter 2 Modify it to read in and add 3 numbers Finally modify it to compute and write out the average of the 3 numbers CONGRATULATIONS! You have now completed assessed short exercise number 2 - get it marked! REMEMBER: The deadline for getting short exercises 1 and 2 marked is your lab of week 7 HOWEVER I would expect the vast majority of you to have finished these exercises WELL before this deadline
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.