relational operators control structures decisions using “if” statements 2000 prentice hall,...

24
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

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

Relational OperatorsControl 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

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

2

2000 Prentice Hall, Inc. All rights reserved.

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)

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

3

2000 Prentice Hall, Inc. All rights reserved.

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

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

4

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

}

}

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

5

2000 Prentice Hall, Inc. All rights reserved.

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.

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

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

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

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;

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

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

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

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 ==

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

2003 Prentice Hall, Inc. All rights reserved (Modified) .

10

Flow Chart Basics Selection structure Single selection (if)

print “passed”grade >=60true

Connector symbol

false

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

Equality and Relational Operators

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

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

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

13

2000 Prentice Hall, Inc. All rights reserved.

if/else Flow Chart

grade >=60

Print “You passed”

Print “You failed”

TrueFalse

 

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

14

2000 Prentice Hall, Inc. All rights reserved.

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

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

15

2000 Prentice Hall, Inc. All rights reserved.

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

elseSystem.out.println(" The number " + num + " is odd.");

System.exit(0); } }

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

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 (showInputDialog)

2.4 parseInt

2.5 Initialize result

1 // Comparison.java2 // Compare integers using if statements, relational operators 3 // and equality operators.4 5 // Java packages6 import javax.swing.JOptionPane;7 8 public class Comparison {9 10 // main method begins execution of Java application11 public static void main( String args[] )12 {13 String firstNumber; // first string entered by user14 String secondNumber; // second string entered by user15 String result; // a string containing the output16 17 int number1; // first number to compare18 int number2; // second number to compare19 20 // read first number from user as a string21 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" );22 23 // read second number from user as a string24 secondNumber =25 JOptionPane.showInputDialog( "Enter second integer:" );26 27 // convert numbers from type String to type int28 number1 = Integer.parseInt( firstNumber );29 number2 = Integer.parseInt( secondNumber );30 31 // initialize result to empty String32 result = ""; 33

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

2003 Prentice Hall, Inc.All rights reserved.

17

Comparison.java

3. if statements

4. showMessageDialog

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 application57 58 } // end method main59 60 } // end class Comparison

Test for equality, create new string, assign to result.

Notice use of JOptionPane.INFORMATION_MESSAGE

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

2003 Prentice Hall, Inc.All rights reserved.

18

Program Output

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

2003 Prentice Hall, Inc. All rights reserved (Modified) .

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:

– Lines 21-30: obtain user-input numbers and parses input string into integer variables

13 String firstNumber, 14 secondNumber, 15 result;

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

2003 Prentice Hall, Inc. All rights reserved (Modified) .

20

2.8 Decision 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;

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

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

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

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)

Operators Associativity Type * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality = right to left assignment Fig. 2.21 Precedence and associativity of the operators discuss ed so far.

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

23

2000 Prentice Hall, Inc. All rights reserved.

• 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

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

24

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