Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Computers and Programming in JAVA: V22.0002 Relational Operators Control structures Decisions using “if” statements 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. 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) 2000 Prentice Hall, Inc. All rights reserved. 3 Review Packages in Java 4 // Java packages – Two groups of packages in Java API – Core packages • Begin with java • Included with Java 2 Software Development Kit – Extension packages • Begin with javax 5 import javax.swing.JOptionPane; // program uses OptionPane – import declarations • Used by compiler to identify and locate classes used in Java programs • Tells compiler to load class JOptionPane from javax.swing package 2000 Prentice Hall, Inc. All rights reserved. // 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); } } 2000 Prentice Hall, Inc. All rights reserved. 4 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. 2000 Prentice Hall, Inc. All rights reserved. 6 Flow Chart Basics 1 Diamonds (decision symbol) contain conditions Rectangles represent statements of work. For example: flow line print() 2003 Prentice Hall, Inc. All rights reserved (Modified) . Flow Chart Basics Sequence structure Connector symbol Triangle boxes represent statements such as x=30; int a; System.out.print(“x”); x=20; 2003 Prentice Hall, Inc. All rights reserved (Modified) . 7 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 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 == 2003 Prentice Hall, Inc. All rights reserved (Modified) . 10 Flow Chart Basics Selection structure Single selection (if) Connector symbol grade >=60 true print “passed” false 2003 Prentice Hall, Inc. All rights reserved (Modified) . Equality and Relational Operators 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“); } 2003 Prentice Hall, Inc. All rights reserved (Modified) . 13 if/else Flow Chart False Print “You failed” 2000 Prentice Hall, Inc. All rights reserved. True grade >=60 Print “You passed” 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 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); } } 2000 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 16 // Comparison.java // Compare integers using if statements, relational operators // and equality operators. // Java packages import javax.swing.JOptionPane; public class Comparison { 1. import // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user String result; // a string containing the output int number1; int number2; Comparison.java // first number to compare // second number to compare 2. Class Comparison 2.1 main 2.2 Declarations // read first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); 2.3 Input data (showInputDialo g) // read second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer:" ); 2.4 parseInt // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); // initialize result to empty String result = ""; 2.5 Initialize result 2003 Prentice Hall, Inc. All rights reserved. 17 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 if ( number1 == number2 ) result = result + number1 + " == " + number2; if ( number1 != number2 ) result = result + number1 + " != " + number2; Test for equality, createComparison.java new string, assign to result. if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; 3. if statements 4. showMessageDialo g if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; // Display results JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end method main } // end class Comparison Notice use of JOptionPane.INFORMATION_MESSAGE 2003 Prentice Hall, Inc. All rights reserved. 18 Program Output 2003 Prentice Hall, Inc. All rights reserved. 19 2.8 Decision 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: 13 14 15 String firstNumber, secondNumber, result; – Lines 21-30: obtain user-input numbers and parses input string into integer variables 2003 Prentice Hall, Inc. All rights reserved (Modified) . 20 2.8 32 Decision Making: Equality and Relational Operators result = ""; – Initialize result with empty string 34 35 if ( number1 == number2 ) result = result + number1 + " == " + number2; – 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 2003 Prentice Hall, Inc. All rights reserved (Modified) . 21 2.8 Decision 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 2003 Prentice Hall, Inc. All rights reserved (Modified) . 22 2.8 Decision 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) Op era tors Assoc ia tivity Typ e * / % left to right multiplicative + left to right additive < <= > >= left to right relational == != left to right equality = right to left assignment Fig. 2.21 Prec ed enc e a nd a ssoc ia tivity of the op era tors d isc ussed so fa r. 2003 Prentice Hall, Inc. All rights reserved (Modified) . 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 2000 Prentice Hall, Inc. All rights reserved. // 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); } 2000 Prentice Hall, Inc. All rights reserved. 24