Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.

Similar presentations


Presentation on theme: "Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course."— Presentation transcript:

1 Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in JAVA: V22.0002

2  2000 Prentice Hall, Inc. All rights reserved. 2 Displaying Text in a Dialog Box 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)

3  2000 Prentice Hall, Inc. All rights reserved. 3 Review Packages in Java –Two groups of packages in Java API –Core packages Begin with java Included with Java 2 Software Development Kit –Extension packages Begin with javax –import declarations Used by compiler to identify and locate classes used in Java programs Tells compiler to load class JOptionPane from javax.swing package 4 // Java packages 5 import javax.swing.JOptionPane; // program uses OptionPane

4  2000 Prentice Hall, Inc. All rights reserved. 4// InputDataDemo.java: Entering input from input dialog boxes. Finding the square of a number import javax.swing.JOptionPane; public class Class5_Input_DataDemo { public static void main(String args[]) { int result; // declare the result // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); result = num * num ; // Display the result in a message dialog box JOptionPane.showMessageDialog(null, num + " squared is " + result, "Input Window Demo", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

5  2000 Prentice Hall, Inc. All rights reserved. 5 Three Basic Control Structures All programs can be written with just these types of structures –Sequence structure Statements run one after the other –Selection structure Depending on a condition, do one thing (single selection using if); otherwise, do something else (Double selection using if else) if, if-else, and switch (multiple selections). –Repetition structure Repeat some actions over and over for loops, while loops, and do/while loops.

6  2003 Prentice Hall, Inc. All rights reserved (Modified). 6 Flow Chart Basics 1 Rectangles represent statements of work. For example: print() Diamonds (decision symbol) contain conditions flow line

7  2003 Prentice Hall, Inc. All rights reserved (Modified). 7 Flow Chart Basics Sequence structure Connector symbol Triangle boxes represent statements such as x=30; int a; System.out.print(“x”); x=20;

8  2003 Prentice Hall, Inc. All rights reserved (Modified). 8 Decision Making: Equality and Relational Operators if control statement –If a condition is true, then the body of the if statement gets executed –Control always resumes after the if structure if ( condition ) statement executed if condition true No semicolon needed after condition –Else: otherwise the conditional task is not performed

9  2003 Prentice Hall, Inc. All rights reserved (Modified). 9 The if structure If some condition is true – do this Example: if ( x == y ) { System.out.println(“ x is equal to y!\n” ) ; } Every programming language has some form of an if statement. Note the operator: = vs ==

10  2003 Prentice Hall, Inc. All rights reserved (Modified). 10 Flow Chart Basics Selection structure Single selection (if) print “passed” grade >=60 true Connector symbol false

11 Equality and Relational Operators

12  2003 Prentice Hall, Inc. All rights reserved (Modified). 12 The if structure If some condition is true – do this –else, do something else Example: if ( x == y ) { System.out.println(“ x is equal to y!\n“ ) ; } else { System.out.println(“ x is NOT equal to y!\n“); }

13  2000 Prentice Hall, Inc. All rights reserved. 13 if/else Flow Chart grade >=60 Print “You passed” Print “You failed” True False

14  2000 Prentice Hall, Inc. All rights reserved. 14 Even Odd program Lets write a program in class that will: – input a number between 1-10 – test whether a number is even or odd

15  2000 Prentice Hall, Inc. All rights reserved. 15 Even or Odd numbers // even & odd numbers: using "mod" import javax.swing.JOptionPane; public class even_odd { public static void main(String args[]) { // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); // Display the result in a message dialog box if ( ( num % 2 ) == 0 ) System.out.println(" The number " + num + " is even."); else System.out.println(" The number " + num + " is odd."); System.exit(0); }

16  2003 Prentice Hall, Inc. All rights reserved. 16 Comparison.java 1. import 2. Class Comparison 2.1 main 2.2 Declarations 2.3 Input data ( showInputDialo g ) 2.4 parseInt 2.5 Initialize result 1 // Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 5 // Java packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output 16 17 int number1; // first number to compare 18 int number2; // second number to compare 19 20 // read first number from user as a string 21 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); 22 23 // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); 26 27 // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); 30 31 // initialize result to empty String 32 result = ""; 33

17  2003 Prentice Hall, Inc. All rights reserved. 17 Comparison.java 3. if statements 4. showMessageDialo g 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; 36 37 if ( number1 != number2 ) 38 result = result + number1 + " != " + number2; 39 40 if ( number1 < number2 ) 41 result = result + "\n" + number1 + " < " + number2; 42 43 if ( number1 > number2 ) 44 result = result + "\n" + number1 + " > " + number2; 45 46 if ( number1 <= number2 ) 47 result = result + "\n" + number1 + " <= " + number2; 48 49 if ( number1 >= number2 ) 50 result = result + "\n" + number1 + " >= " + number2; 51 52 // Display results 53 JOptionPane.showMessageDialog( null, result, "Comparison Results", 54 JOptionPane.INFORMATION_MESSAGE ); 55 56 System.exit( 0 ); // terminate application 57 58 } // end method main 59 60 } // end class Comparison Test for equality, create new string, assign to result. Notice use of JOptionPane.INFORMATION_MESSAGE

18  2003 Prentice Hall, Inc. All rights reserved. 18 Program Output

19  2003 Prentice Hall, Inc. All rights reserved (Modified). 19 2.8Decision Making: Equality and Relational Operators –Lines 1-12: Comments, import JOptionPane, begin class Comparison and main –Lines 13-18: declare variables Can use comma-separated lists instead: –Lines 21-30: obtain user-input numbers and parses input string into integer variables 13 String firstNumber, 14 secondNumber, 15 result;

20  2003 Prentice Hall, Inc. All rights reserved (Modified). 20 2.8Decision Making: Equality and Relational Operators –Initialize result with empty string –if statement 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 32 result = ""; 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2;

21  2003 Prentice Hall, Inc. All rights reserved (Modified). 21 2.8Decision Making: Equality and Relational Operators –Lines 37-50: other if statements testing for less than, more than, etc. If number1 = 123 and number2 = 123 –Line 34 evaluates true (if number1 = = number 2) Because number1 equals number2 –Line 40 evaluates false (if number1 < number 2) Because number1 is not less than number2 –Line 49 evaluates true (if number1 >= number2) Because number1 is greater than or equal to number2 –Lines 53-54: result displayed in a dialog box using showMessageDialog

22  2003 Prentice Hall, Inc. All rights reserved (Modified). 22 2.8Decision Making: Equality and Relational Operators Precedence of operators –All operators except for = (assignment) associates from left to right For example: x = y = z is evaluated x = (y = z)

23  2000 Prentice Hall, Inc. All rights reserved. 23 Lets write a program in class: –Write a program that will input a number from 1-10 (using input dialog box) –Determine if the number entered is equal to 5 –OR less than 5 –Or larger than 5

24  2000 Prentice Hall, Inc. All rights reserved. 24 // less than five!! import javax.swing.JOptionPane; public class less_than_5{ public static void main(String args[]) { // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); // Display the result in a message dialog box if ( num < 5 ) System.out.println(" The number " + num + " is less than five."); else if (num == 5) System.out.println(" The number " + num + " is equal to five."); else System.out.println(" The number " + num + " is greater than five."); System.exit(0); }


Download ppt "Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course."

Similar presentations


Ads by Google