School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.

Slides:



Advertisements
Similar presentations
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 1 Progress By now you should have: Worked through units 1 to 5 of the learning.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
Chapter 2 - Introduction to Java Applications
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Introduction to Java. 2 A Brief History of OOP and Java Early high level languages –_________________________ More recent languages –BASIC, Pascal, C,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Dialog Boxes.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Object Oriented Programming Object and Classes Lecture 3 MBY.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Object Oriented Programming (OOP) LAB # 3 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Introduction to Java. 2 A Brief History of OOP and Java Early high level languages –FORTRAN, COBOL, LISP More recent languages –BASIC, Pascal, C, Ada,
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
2.5 Another Java Application: Adding Integers
Dialogues and Wrapper Classes
Chapter 2 - Introduction to Java Applications
Message, Input, Confirm, and Specialized Dialogs
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Message, Input, and Confirm Dialogs
Object Oriented Programming
Chapter 2 - Introduction to Java Applications
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and strings

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.

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?

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.

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.

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");

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

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

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

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);

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);

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.

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

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 ); }

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 }

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

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