 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
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.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMT Programming Software Applications
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 3: Variables and Input Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
 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.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
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.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Java Applications
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 3 – Introduction to C# Programming
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Introduction to C Programming
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a Line of Text 2.3Another Java Application: Adding Integers 2.4Memory Concepts 2.5 Arithmetic 2.6Decision Making: Equality and Relational Operators

 2000 Prentice Hall, Inc. All rights reserved Introduction In this chapter –Introduce examples to illustrate features of Java –Two program styles - applications and applets

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Application –Program that executes using the java interpreter Sample program –Show program, then analyze each line

 2000 Prentice Hall, Inc. All rights reserved. Outline 4 Java program Program Output 1// Fig. 2.1: Welcome1.java 2// A first program in Java 3 4public class Welcome1 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome to Java Programming!" ); 8 } Welcome to Java Programming! 9 }

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –// remainder of line is comment Comments ignored Document and describe code –Multiple line comments: /*... */ /* This is a multiple line comment. It can be split over many lines */ –Another line of comments –Note: line numbers not part of program, added for reference 1// Fig. 2.1: Welcome1.java 2// A first program in Java

 2000 Prentice Hall, Inc. All rights reserved. 6 –Blank line Makes program more readable Blank lines, spaces, and tabs are whitespace characters –Ignored by compiler –Begins class definition for class Welcome1 Every Java program has at least one user-defined class Keyword: words reserved for use by Java –class keyword followed by class name Naming classes: capitalize every word –SampleClassName 2.2A Simple Program: Printing a Line of Text 3 4public class Welcome1 {

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $value, _value, button7 –7button is invalid Case sensitive (capitalization matters) –a1 and A1 are different –For chapters 2 to 7, use public keyword Certain details not important now Mimic certain features, discussions later 4public class Welcome1 {

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Saving files File name is class name and.java extension Welcome1.java –Left brace { Begins body of every class Right brace ends definition (line 9 ) –Part of every Java application Applications begin executing at main –Parenthesis indicate main is a method –Java applications contain one or more methods 4public class Welcome1 { 5 public static void main( String args[] )

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Exactly one method must be called main –Methods can perform tasks and return information void means main returns no information For now, mimic main 's first line –Left brace begins body of method definition Ended by right brace 5 public static void main( String args[] ) 6 {

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Instructs computer to perform an action Prints string of characters –String - series characters inside double quotes Whitespaces in strings are not ignored by compiler –System.out Standard output object Print to command window (i.e., MS-DOS prompt) –Method System.out.println Displays line of text Argument inside parenthesis –This line known as a statement Statements must end with semicolon ; 7 System.out.println( "Welcome to Java Programming!" );

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Ends method definition –Ends class definition –Can add comments to keep track of ending braces –Lines 8 and 9 could be rewritten as: –Remember, compiler ignores comments 8 } 9 } 8 } // end of method main() 9 } // end of class Welcome1

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Compiling a program –Open a command window, go to directory where program is stored –Type javac Welcome1.java –If no errors, Welcome1.class created Has bytecodes that represent application Bytecodes passed to Java interpreter Executing a program –Type java Welcome1 Interpreter loads.class file for class Welcome1.class extension omitted from command –Interpreter calls method main

 2000 Prentice Hall, Inc. All rights reserved. Outline 13 Java program Program Output 1// Fig. 2.1: Welcome1.java 2// A first program in Java 3 4public class Welcome1 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome to Java Programming!" ); 8 } 9 }

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Other methods –System.out.println Prints argument, puts cursor on new line –System.out.print Prints argument, keeps cursor on same line

 2000 Prentice Hall, Inc. All rights reserved. Outline Comments 2. Blank line 3. Begin class Welcome2 3.1 Method main 4. Method System.out.print 4.1 Method System.out.println 5. end main, Welcome2 Program Output 1// Fig. 2.3: Welcome2.java 2// Printing a line with multiple statements 3 4public class Welcome2 { 5 public static void main( String args[] ) 6 { 7 7 System.out.print( "Welcome to " ); 8 System.out.println( "Java Programming!" ); 9 } 10} Welcome to Java Programming! System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Escape characters –Backslash ( \ ) –Indicates special characters be output Backslash combined with character makes escape sequence \n - newline \r - carriage return \" - double quote \t - tab \\ - backslash Usage –Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" );

 2000 Prentice Hall, Inc. All rights reserved. Outline 17 Class Welcome1 1. main 2. System.out.println (uses \n for newline) Program Output 1// Fig. 2.4: Welcome3.java 2// Printing multiple lines with a single statement 3 4public class Welcome3 { 5 public static void main( String args[] ) 6 { 7 7 System.out.println( "Welcome\nto\nJava\nProgramming!" ); 8 } 9}9} Welcome to Java Programming! Notice how a new line is output for each \n escape sequence.

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Display –Most Java applications use windows or a dialog box We have used command window –Class JOptionPane allows us to use dialog boxes Packages –Set of predefined classes for us to use –Groups of related classes called packages Group of all packages known as Java class library or Java applications programming interface (Java API) –JOptionPane is in the javax.swing package Package has classes for using Graphical User Interfaces (GUIs)

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Sample GUI

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text Upcoming program –Application that uses dialog boxes –Explanation will come afterwards

 2000 Prentice Hall, Inc. All rights reserved. Outline 21 Java program using dialog box Program Output 1// Fig. 2.6: Welcome4.java 2// Printing multiple lines in a dialog box 3import javax.swing.JOptionPane; // import class JOptionPane 4 5public class Welcome4 { 6 public static void main( String args[] ) 7 { 8 JOptionPane.showMessageDialog( 9 null, "Welcome\nto\nJava\nProgramming!" ); System.exit( 0 ); // terminate the program 12 } 13}

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Lines 1-2: comments as before –import statements Locate the classes we use Tells compiler to load JOptionPane from javax.swing package –Lines 4-7: Blank line, begin class Welcome4 and main 3import javax.swing.JOptionPane; // import class JOptionPane 4 5public class Welcome4 { 6 public static void main( String args[] ) 7 {

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Call method showMessageDialog of class JOptionPane Requires two arguments Multiple arguments separated by commas (, ) For now, first argument always null Second argument is string to display –showMessageDialog is a static method of class JOptionPane static methods called using class name, dot (. ) then method name –All statements end with ; A single statement can span multiple lines Cannot split statement in middle of identifier or string 8 JOptionPane.showMessageDialog( 9 null, "Welcome\nto\nJava\nProgramming!" );

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Executing lines 8 and 9 displays the dialog box Automatically includes an OK button –Hides or dismisses dialog box Title bar has string Message 8 JOptionPane.showMessageDialog( 9 null, "Welcome\nto\nJava\nProgramming!" );

 2000 Prentice Hall, Inc. All rights reserved A Simple Program: Printing a Line of Text –Calls static method exit of class System Terminates application –Use with any application displaying a GUI Because method is static, needs class name and dot (. ) Identifiers starting with capital letters usually class names –Argument of 0 means application ended successfully Non-zero usually means an error occurred –Class System part of package java.lang No import statement needed java.lang automatically imported in every Java program –Lines 12-13: Braces to end Welcome4 and main 11 System.exit( 0 ); // terminate the program

 2000 Prentice Hall, Inc. All rights reserved. Outline import statement 2. Class Welcome4 2.1 main 2.2 showMessageDialog 2.3 System.exit Program Output 1// Fig. 2.6: Welcome4.java 2// Printing multiple lines in a dialog box 3import javax.swing.JOptionPane; // import class JOptionPane 4 5public class Welcome4 { 6 public static void main( String args[] ) 7 { 8 JOptionPane.showMessageDialog( 9 null, "Welcome\nto\nJava\nProgramming!" ); System.exit( 0 ); // terminate the program 12 } 13}

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers Upcoming program –Use input dialogs to input two values from user –Use message dialog to display sum of the two values

 2000 Prentice Hall, Inc. All rights reserved. Outline 28 Java Program using input dialogs 1// Fig. 2.8: Addition.java 2// An addition program 3 4import javax.swing.JOptionPane; // import class JOptionPane 5 6public class Addition { 7 public static void main( String args[] ) 8 { 9 String firstNumber, // first string entered by user 10 secondNumber; // second string entered by user 11 int number1, // first number to add 12 number2, // second number to add 13 sum; // sum of number1 and number // read in first number from user as a string 16 firstNumber = 17 JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a string 20 secondNumber = 21 JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int 24 number1 = Integer.parseInt( firstNumber ); 25 number2 = Integer.parseInt( secondNumber ); // add the numbers 28 sum = number1 + number2; // display the results

 2000 Prentice Hall, Inc. All rights reserved. Outline 29 Program Output 31 JOptionPane.showMessageDialog( 32 null, "The sum is " + sum, "Results", 33 JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate the program 36 } 37}

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Location of JOptionPane for use in the program –Begins public class Addition Recall that file name must be Addition.java –Lines 7-8: main –Declaration firstNumber and secondNumber are variables 4import javax.swing.JOptionPane; // import class JOptionPane 6public class Addition { 9 String firstNumber, // first string entered by user 10 secondNumber; // second string entered by user

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Variables Location in memory that stores a value –Declare with name and data type before use firstNumber and secondNumber are of data type String (package java.lang ) –Hold strings Variable name: any valid identifier Declarations end with semicolons ; –Can declare multiple variables of the same type at a time –Use comma separated list –Can add comments to describe purpose of variables 9 String firstNumber, // first string entered by user 10 secondNumber; // second string entered by user

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Declares variables number1, number2, and sum of type int int holds integer values (whole numbers): i.e., 0, -4, 97 Data types float and double can hold decimal numbers Data type char can hold a single character Primitive data types - more Chapter 4 11 int number1, // first number to add 12 number2, // second number to add 13 sum; // sum of number1 and number2

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Reads String from the user, representing the first number to be added Method JOptionPane.showInputDialog displays the following: Message called a prompt - directs user to perform an action Argument appears as prompt text If wrong type of data entered (non-integer), error occurs 15 // read in first number from user as a string 16 firstNumber = 17 JOptionPane.showInputDialog( "Enter first integer" );

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Result of call to showInputDialog given to firstNumber using assignment operator = Assignment statement = binary operator - takes two operands –Expression on right evaluated and assigned to variable on left Read as: firstNumber gets value of JOptionPane.showInputDialog( "Enter first integer" ) 15 // read in first number from user as a string 16 firstNumber = 17 JOptionPane.showInputDialog( "Enter first integer" );

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Similar to previous statement Assigns variable secondNumber to second integer input –Method Integer.parseInt Converts String argument into an integer (type int ) –Class Integer in java.lang Integer returned by Integer.parseInt is assigned to variable number1 (line 24) –Remember that number1 was declared as type int Line 25 similar 19 // read in second number from user as a string 20 secondNumber = 21 JOptionPane.showInputDialog( "Enter second integer" ); 23 // convert numbers from type String to type int 24 number1 = Integer.parseInt( firstNumber ); 25 number2 = Integer.parseInt( secondNumber );

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Assignment statement Calculates sum of number1 and number2 (right hand side) Uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2 27 // add the numbers 28 sum = number1 + number2;

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Use showMessageDialog to display results –"The sum is " + sum Uses the operator + to "add" the string literal "The sum is" and sum Concatenation of a String and another data type –Results in a new string If sum contains 117, then "The sum is " + sum results in the new string "The sum is 117" Note the space in "The sum is " More on strings in Chapter JOptionPane.showMessageDialog( 32 null, "The sum is " + sum, "Results", 33 JOptionPane.PLAIN_MESSAGE );

 2000 Prentice Hall, Inc. All rights reserved Another Java Application: Adding Integers –Different version of showMessageDialog Requires four arguments (instead of two as before) First argument: null for now Second: string to display Third: string in title bar Fourth: type of message dialog –JOptionPane.PLAIN_MESSAGE - no icon –JOptionPane.ERROR_MESSAGE –JOptionPane.INFORMATION_MESSAGE –JOptionPane.WARNING_MESSAGE –JOptionPane.QUESTION_MESSAGE 31 JOptionPane.showMessageDialog( 32 null, "The sum is " + sum, "Results", 33 JOptionPane.PLAIN_MESSAGE );

 2000 Prentice Hall, Inc. All rights reserved. Outline import 2. class Addition 2.1 Declare variables (name and data type) 3. showInputDialog 4. parseInt 5. Add numbers, put result in sum 1// Fig. 2.8: Addition.java 2// An addition program 3 4import javax.swing.JOptionPane; // import class JOptionPane 5 6public class Addition { 7 public static void main( String args[] ) 8 { 9 9 String firstNumber, // first string entered by user 10 secondNumber; // second string entered by user 11 int number1, // first number to add 12 number2, // second number to add 13 sum; // sum of number1 and number // read in first number from user as a string firstNumber = 17 JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a string 20 secondNumber = 21 JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); 25 number2 = Integer.parseInt( secondNumber ); // add the numbers sum = number1 + number2; // display the results Declare variables: name and data type. Input first integer as a String, assign to firstNumber. Convert strings to integers. Add, place result in sum.

 2000 Prentice Hall, Inc. All rights reserved. Outline showMessageDialog 7. System.exit Program Output 31 JOptionPane.showMessageDialog( 32 null, "The sum is " + sum, "Results", 33 JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate the program 36 } 37}

 2000 Prentice Hall, Inc. All rights reserved Memory Concepts Variables –Every variable has a name, a type, a size and a value Name corresponds to location in memory –When new value is placed into a variable, replaces (and destroys) previous value –Reading variables from memory does not change them Visual representation number1 45

 2000 Prentice Hall, Inc. All rights reserved Arithmetic Arithmetic calculations used in most programs –Usage * for multiplication / for division +, - No operator for exponentiation (more in Chapter 5) –Integer division truncates remainder 7 / 5 evaluates to 1 –Modulus operator % returns the remainder 7 % 5 evaluates to 2

 2000 Prentice Hall, Inc. All rights reserved Arithmetic Operator precedence –Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed –Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

 2000 Prentice Hall, Inc. All rights reserved Arithmetic

 2000 Prentice Hall, Inc. All rights reserved Decision Making: Equality and Relational Operators if control structure –Simple version in this section, more detail later –If a condition is true, then the body of the if statement executed 0 interpreted as false, non-zero is true –Control always resumes after the if structure –Conditions for if structures can be formed using equality or relational operators (next slide) if ( condition ) statement executed if condition true No semicolon needed after condition

 2000 Prentice Hall, Inc. All rights reserved Decision Making: Equality and Relational Operators Upcoming program uses if structures –Discussion afterwards <_ >_ =

 2000 Prentice Hall, Inc. All rights reserved. Outline 47 1// Fig. 2.17: Comparison.java 2// Using if statements, relational operators 3// and equality operators 4 5import javax.swing.JOptionPane; 6 7public class Comparison { 8 public static void main( String args[] ) 9 { 10 String firstNumber, // first string entered by user 11 secondNumber, // second string entered by user 12 result; // a string containing the output 13 int number1, // first number to compare 14 number2; // second number to compare // read first number from user as a string 17 firstNumber = 18 JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string 21 secondNumber = 22 JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int 25 number1 = Integer.parseInt( firstNumber ); 26 number2 = Integer.parseInt( secondNumber ); // initialize result to the empty string 29 result = ""; 30

 2000 Prentice Hall, Inc. All rights reserved. Outline if ( number1 == number2 ) 32 result = result + number1 + " == " + number2; if ( number1 != number2 ) 35 result = result + number1 + " != " + number2; if ( number1 < number2 ) 38 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 41 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 44 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 47 result = result + "\n" + number1 + " >= " + number2; // Display results 50 JOptionPane.showMessageDialog( 51 null, result, "Comparison Results", 52 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 55 } 56}

 2000 Prentice Hall, Inc. All rights reserved. Outline 49 Program Output

 2000 Prentice Hall, Inc. All rights reserved Decision Making: Equality and Relational Operators –Lines 1-9: Comments, import JOptionPane, begin class Comparison and main –Declare variables –Input data from user and assign to variables 10 String firstNumber, // first string entered by user 11 secondNumber, // second string entered by user 12 result; // a string containing the output 13 int number1, // first number to compare 14 number2; // second number to compare 17 firstNumber = 18 JOptionPane.showInputDialog( "Enter first integer:" ); 21 secondNumber = 22 JOptionPane.showInputDialog( "Enter second integer:" );

 2000 Prentice Hall, Inc. All rights reserved Decision Making: Equality and Relational Operators –Convert String s to int s and assign to variables –Initialize result with empty string –if structure to test for equality using ( == ) If variables equal (condition true) –result concatenated using + operator –result = result + other strings –Right side evaluated first, new string assigned to result If variables not equal, statement skipped 25 number1 = Integer.parseInt( firstNumber ); 26 number2 = Integer.parseInt( secondNumber ); 29 result = ""; 31 if ( number1 == number2 ) 32 result = result + number1 + " == " + number2;

 2000 Prentice Hall, Inc. All rights reserved Decision Making: Equality and Relational Operators Other if structures –Lines 50-52: result displayed in a dialog box using showMessageDialog 34 if ( number1 != number2 ) 35 result = result + number1 + " != " + number2; if ( number1 < number2 ) 38 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 41 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 44 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 47 result = result + "\n" + number1 + " >= " + number2; 48

 2000 Prentice Hall, Inc. All rights reserved. Outline import 2. Class Comparison 2.1 main 2.2 Declarations 2.3 Input data ( showInputDialog ) 2.4 parseInt 2.5 Initialize result 1// Fig. 2.17: Comparison.java 2// Using if statements, relational operators 3// and equality operators 4 5import javax.swing.JOptionPane; 6 7public class Comparison { 8 public static void main( String args[] ) 9 { 10 String firstNumber, // first string entered by user 11 secondNumber, // second string entered by user 12 result; // a string containing the output 13 int number1, // first number to compare 14 number2; // second number to compare // read first number from user as a string 17 firstNumber = 18 JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string 21 secondNumber = 22 JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int 25 number1 = Integer.parseInt( firstNumber ); 26 number2 = Integer.parseInt( secondNumber ); // initialize result to the empty string 29 result = ""; 30

 2000 Prentice Hall, Inc. All rights reserved. Outline if statements 4. showMessageDialog if ( number1 == number2 ) 32 result = result + number1 + " == " + number2; if ( number1 != number2 ) 35 result = result + number1 + " != " + number2; if ( number1 < number2 ) 38 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 41 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 44 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 47 result = result + "\n" + number1 + " >= " + number2; // Display results 50 JOptionPane.showMessageDialog( 51 null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 55 } 56} Notice use of JOptionPane.INFORMATION_MESSAGE Test for equality, greater than, less than, etc. Create new string, assign to result.

 2000 Prentice Hall, Inc. All rights reserved. Outline 55 Program Output