0% found this document useful (0 votes)
23 views55 pages

Control Structures in Java Programming

Uploaded by

missioncancer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views55 pages

Control Structures in Java Programming

Uploaded by

missioncancer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

1

Chapter 4 - Control Structures: Part 1


Outline
4.1 Introduction
4.2 Algorithms
4.3 Pseudocode
4.4 Control Structures
4.5 if Single-Selection Statement
4.6 if else Selection Statement
4.7 while Repetition Statement
4.11 Compound Assignment Operators
4.12 Increment and Decrement Operators
4.13 Primitive Types

 2003 Prentice Hall, Inc. All rights reserved.


2

4.1 Introduction

• We learn about Control Structures


– Structured-programming principle
– Control structures help build and manipulate objects
(Chapter 8)

 2003 Prentice Hall, Inc. All rights reserved.


3

4.2 Algorithms

• Algorithm
– Series of actions in specific order
• The actions executed
• The order in which actions execute
• Program control
– Specifying the order in which actions execute
• Control structures help specify this order

 2003 Prentice Hall, Inc. All rights reserved.


4

4.3 Pseudocode

• Pseudocode
– Informal language for developing algorithms
– Not executed on computers
– Helps developers “think out” algorithms

 2003 Prentice Hall, Inc. All rights reserved.


5

4.4 Control Structures

• Sequential execution
– Program statements execute one after the other
• Transfer of control
– Three control structures can specify order of statements
• Sequence structure (default)
• Selection structure
• Repetition structure
• Activity diagram
– Models the workflow (flowchart)
• Action-state symbols
• Transition arrows

 2003 Prentice Hall, Inc. All rights reserved.


6

Corresponding Java statement:


add grade to total total = total + grade;

Corresponding Java statement:


add 1 to counter counter = counter + 1;

Fig 4.1 Sequence structure activity diagram.

 2003 Prentice Hall, Inc. All rights reserved.


7

J ava Keywords
abstract assert boolean break byte
case catch char class continue
default do double else extends
final finally float for if
implements import instanceof int interface
long native new package private
protected public return short static
strictfp super switch synchronized this
throw throws transient try void
volatile while
Keywords that are reserved, but not currently used
const goto
Fig. 4.2 J ava keywords.

 2003 Prentice Hall, Inc. All rights reserved.


8

4.4 Control Structures

• Java has a sequence structure “built-in”


• Java provides three selection structures
– if
– if…else
– switch

• Java provides three repetition structures


– while
– do…while
– for

• Each of these words is a Java keyword

 2003 Prentice Hall, Inc. All rights reserved.


9

4.5 if Single-Selection Statement

• Single-entry/single-exit control structure


• Perform action only when condition is true
• Action/decision programming model

 2003 Prentice Hall, Inc. All rights reserved.


10

if (studentGrade >= 60)


{
[Link] (“Passed”);
}

[grade >= 60]


print “Passed”

[grade < 60]

Fig 4.3 if single-selections statement activity diagram.

 2003 Prentice Hall, Inc. All rights reserved.


11

4.6 if…else Selection Statement

• Perform action only when condition is true


• Perform different specified action when condition
is false
• Conditional operator (?:)
• Nested if…else selection structures

 2003 Prentice Hall, Inc. All rights reserved.


12
if (studentGrade >= 60)
{
[Link] (“Passed”);
}
else
{
[Link] (“Failed”);
}

[grade < 60] [grade >= 60]


print “Failed” print “Passed”

Fig 4.4 if…else double-selections statement activity diagram.

 2003 Prentice Hall, Inc. All rights reserved.


13

Conditional Operator (?:)

[Link] (studentGrade >= 60 ?


“Passed”: “Failed”);

 2003 Prentice Hall, Inc. All rights reserved.


14

Nested if…else selection structures

if (studentGrade >= 90)


[Link] (“A”);
else if (studentGrade >= 80)
[Link] (“B”);
else if (studentGrade >= 70)
[Link] (“C”);
else if (studentGrade >= 60)
[Link] (“D”);
else
[Link] (“F”);

 2003 Prentice Hall, Inc. All rights reserved.


15

4.7 while Repetition Statement

• Repeat action while condition remains true


• Condition should eventually become false (or
never-ending loop)

 2003 Prentice Hall, Inc. All rights reserved.


16

while (product <= 1000)


{
product = 2 * product;
}

merge

decision
[product <= 1000]
double product value

[product > 1000]


Corresponding Java statement:
product = 2 * product;

Fig 4.5 while repetition statement activity diagram.

 2003 Prentice Hall, Inc. All rights reserved.


17
4.11 Compound Assignment
Operators
• Assignment Operators
– Abbreviate assignment expressions
– Any statement of form
• variable = variable operator expression;
– Can be written as
• variable operator= expression;
– e.g., addition assignment operator +=
•c = c + 3
– can be written as
• c += 3

 2003 Prentice Hall, Inc. All rights reserved.


18

Assignment Sample Explanation Assigns


operator expression
Assume: int c = 3,
d = 5, e = 4, f
= 6, g = 12;
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d - 4 1 to d
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
Fig. 4.12 Arithmetic assignment operators.

 2003 Prentice Hall, Inc. All rights reserved.


19
4.12 Increment and Decrement
Operators
• Unary increment operator (++)
– Increment variable’s value by 1
• Unary decrement operator (--)
– Decrement variable’s value by 1
• C++ is a language “one better than” C
• Preincrement / predecrement operator
• Postincrement / postdecrement operator

 2003 Prentice Hall, Inc. All rights reserved.


20

Operator C alled Sample Explanation


expression
++ preincrement ++a
Increment a by 1, then use the new
value of a in the expression in
which a resides.
++ postincrement a++ Use the current value of a in the
expression in which a resides, then
increment a by 1.
-- predecrement --b Decrement b by 1, then use the
new value of b in the expression in
which b resides.
-- postdecrement b-- Use the current value of b in the
expression in which b resides, then
decrement b by 1.
Fig. 4.13 The increment and decrement operators.

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 4.14: [Link] 21
2 // Preincrementing and postincrementing operators. Outline
3
4 public class Increment { Line 13 postincrements c
5 [Link]
6 public static void main( String args[] )
7 {
Line 13 postincrement
8 int c;
9
10 // demonstrate postincrement
11 c = 5; // assign 5 to c Line 21 preincrement
12 [Link]( c ); // print 5 Line 21 preincrements c
13 [Link]( c++ ); // print 5 then postincrement
14 [Link]( c ); // print 6
15
16 [Link](); // skip a line
17
18 // demonstrate preincrement
19 c = 5; // assign 5 to c
20 [Link]( c ); // print 5
21 [Link]( ++c ); // preincrement then print 6
22 [Link]( c ); // print 6
23
24 } // end main
25
26 } // end class Increment
5
5
6

5
6
6
 2003 Prentice Hall, Inc.
All rights reserved.
22

4.13 Primitive Types

• Primitive types
– “building blocks” for more complicated types
• Java is strongly typed
– All variables in a Java program must have a type
• Java primitive types
– portable across computer platforms that support Java

 2003 Prentice Hall, Inc. All rights reserved.


23

Type Size in bits Values Standard


boolean true or false
[Note: The representation of a boolean is
specific to the Java Virtual Machine on each
computer platform.]
char 16 '\u0000' to '\uFFFF' (ISO Unicode character set)
(0 to 65535)
byte 8 –128 to +127
(–27 to 27 – 1)
short 16 –32,768 to +32,767
(–215 to 215 – 1)
int 32 –2,147,483,648 to +2,147,483,647
(–231 to 231 – 1)
long 64 –9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
(–263 to 263 – 1)
float 32 Negative range: (IEEE 754 floating point)
–3.4028234663852886E+38 to
–1.40129846432481707e–45
Positive range:
1.40129846432481707e–45 to
3.4028234663852886E+38
double 64 Negative range: (IEEE 754 floating point)
–1.7976931348623157E+308 to
–4.94065645841246544e–324
Positive range:
4.94065645841246544e–324 to
1.7976931348623157E+308
Fig. 4.16The J ava primitive types.

 2003 Prentice Hall, Inc. All rights reserved.


24

Chapter 5 – Control Structures: Part 2


Outline
5.1 Introduction
5.2 Essentials of Counter-Controlled Repetition
5.3 for Repetition Statement
5.4 Examples Using the for Statement
5.5 do…while Repetition Statement
5.6 switch Multiple-Selection Statement
5.7 break and continue Statements
5.8 Labeled break and continue Statements
5.9 Logical Operators

 2003 Prentice Hall, Inc. All rights reserved.


25

5.2 Essentials of Counter-Controlled


Repetition
• Counter-controlled repetition requires:
– Control variable (loop counter)
– Initial value of the control variable
– Increment/decrement of control variable through each loop
– Condition that tests for the final value of the control variable

 2003 Prentice Hall, Inc. All rights reserved.


26
1 // Fig. 5.1: [Link] Outline
2 // Counter-controlled repetition.
3 import [Link];
4 [Link]
5 import [Link];
6
va
7 public class WhileCounter extends JApplet {
name is counter
Control-variableCondition tests for
8 counter’s final value Line 14
Control-variable initial value is 1
9 // draw lines on applet’s background
10 public void paint( Graphics g )
Line 16
11 {
12 [Link]( g ); // call paint method inherited from JApplet
13
for counter
Increment Line 18
14 int counter = 1; // initialization
15
16 while ( counter <= 10 ) { // repetition condition
17 [Link]( 10, 10, 250, counter * 10 );
18 ++counter; // increment
19
20 } // end while
21
22 } // end method paint
23
24 } // end class WhileCounter

 2003 Prentice Hall, Inc.


All rights reserved.
27

5.3 for Repetition Statement

• Handles counter-controlled-repetition details

 2003 Prentice Hall, Inc. All rights reserved.


28
1 // Fig. 5.2: [Link] Outline
2 // Counter-controlled repetition with the for statement.
3 import [Link];
4 [Link]
5 import [Link];
Condition tests for
6 is counterfinal value
Control-variable namecounter’s
7 public class ForCounter extends JApplet { Line 16
8 Control-variable initial value is 1 int counter =
9 // draw lines on applet’s background Increment for counter 1;
10 public void paint( Graphics g )
11 {
12 [Link]( g ); // call paint method inherited from JApplet
Line 16
13 counter <= 10;
14 // for statement header includes initialization,
15 // repetition condition and increment Line 16
16 for ( int counter = 1; counter <= 10; counter++ )
counter++;
17 [Link]( 10, 10, 250, counter * 10 );
18
19 } // end method paint
20
21 } // end class ForCounter

 2003 Prentice Hall, Inc.


All rights reserved.
29

Required Final value of control Required


for Control semicolon variable for which the semicolon
keyword variable separator condition is true separator

for ( int counter = 1; counter <= 10; counter++ )

Initial value of Loop-continuation Increment of control


control variable condition variable

Fig. 5.3 for statement header components.

 2003 Prentice Hall, Inc. All rights reserved.


30

5.3 for Repetition Structure (cont.)

for ( initialization; loopContinuationCondition; increment )


statement;

can usually be rewritten as:

initialization;
while ( loopContinuationCondition ) {
statement;
increment;
}

init, condition, increment all optional


Condition assumed to be true (unending loop) if omitted

 2003 Prentice Hall, Inc. All rights reserved.


31

Establish initial value of


control variable int counter = 1

[counter <= 10] Draw a line on the Increment the


applet control variable
[counter > 10]

Determine whether [Link]( counter++


the final value of 10, 10, 250,
counter * 10 );
control variable has
been reached

Fig. 5.4 for statement activity diagram.

 2003 Prentice Hall, Inc. All rights reserved.


32
5.4 Examples Using the for
Statement
• Varying control variable in for statement
– Vary control variable from 1 to 100 in increments of 1
• for ( int i = 1; i <= 100; i++ )
– Vary control variable from 100 to 1 in increments of –1
• for ( int i = 100; i >= 1; i-- )
– Vary control variable from 7 to 77 in increments of 7
• for ( int i = 7; i <= 77; i += 7 )

 2003 Prentice Hall, Inc. All rights reserved.


33
1 // Fig. 5.5: [Link] Outline
2 // Summing integers with the for statement.
3 import [Link];
4 increment number by 2 each iteration [Link]
5 public class Sum {
6
7 public static void main( String args[] ) Line 12
8 {
9 int total = 0; // initialize sum
10
11 // total even integers from 2 through 100
12 for ( int number = 2; number <= 100; number += 2 )
13 total += number;
14
15 // display results
16 [Link]( null, "The sum is " + total,
17 "Total Even Integers from 2 to 100",
18 JOptionPane.INFORMATION_MESSAGE );
19
20 [Link]( 0 ); // terminate application
21
22 } // end main
23
24 } // end class Sum

 2003 Prentice Hall, Inc.


All rights reserved.
34
1 // Fig. 5.6: [Link] Outline
2 // Calculating compound interest.
3 import [Link]; // class for numeric formatting
4
Java treats floating-points as
import [Link]; // class for country-specific information
type double [Link]
5
6 import [Link]; NumberFormat can format
7 import [Link]; numeric values as currency Lines 13-15
8
9 public class Interest { Line 18
10
11 Display
public static void main( String args[] ) currency values with
12 { dollar sign ($) Line 19
13 double amount; // amount on deposit at end of each year
14 double principal = 1000.0; // initial amount before interest
15 double rate = 0.05; // interest rate
16
17 // create NumberFormat for currency in US dollar format
18 NumberFormat moneyFormat =
19 [Link]( [Link] );
20
21 // create JTextArea to display output
22 JTextArea outputTextArea = new JTextArea();
23
24 // set first line of text in outputTextArea
25 [Link]( "Year\tAmount on deposit\n" );
26

 2003 Prentice Hall, Inc.


All rights reserved.
35
27 // calculate amount on deposit for each of ten years Outline
28 for ( int year = 1; year <= 10; year++ ) {
29
30 // calculate new amount for specified year
Calculate amount with for
[Link]
statement
31 amount = principal * [Link]( 1.0 + rate, year );
32
33 // append one line of text to outputTextArea Lines 28-31
34 [Link]( year + "\t" +
35 [Link]( amount ) + "\n" );
36
37 } // end for

38
39 // display results
40 [Link]( null, outputTextArea,
41 "Compound Interest", JOptionPane.INFORMATION_MESSAGE );
42
43 [Link]( 0 ); // terminate the application
44
45 } // end main
46
47 } // end class Interest

 2003 Prentice Hall, Inc.


All rights reserved.
36

5.5 do…while Repetition Statement

• do…while structure
– Similar to while structure
– Tests loop-continuation after performing body of loop
• i.e., loop body always executes at least once

 2003 Prentice Hall, Inc. All rights reserved.


37
1 // Fig. 5.7: [Link] Outline
2 // Using the do...while statement.
3 import [Link];
4 [Link]
5 import [Link];
6
a
7
Oval is drawn before testing
public class DoWhileTest extends JApplet {
8 counter’s final value Lines 16-20
9 // draw lines on applet
10 public void paint( Graphics g )
11 {
12 [Link]( g ); // call paint method inherited from JApplet
13
14 int counter = 1; // initialize counter
15
16 do {
17 [Link]( 110 - counter * 10, 110 - counter * 10,
18 counter * 20, counter * 20 );
19 ++counter;
20 } while ( counter <= 10 ); // end do...while
21
22 } // end method paint
23
24 } // end class DoWhileTest

 2003 Prentice Hall, Inc.


All rights reserved.
38

action state

[true]
condition

[false]

Fig. 5.8 do…while repetition statement activity


diagram.

 2003 Prentice Hall, Inc. All rights reserved.


39
5.6 switch Multiple-Selection
Statement
• switch statement
– Used for multiple selections
– case followed by integer or character (case 10 or case ‘y’)
– Multiple cases (without code) indicate the same thing done
for each case

case 3:
case 5:
case 7:
x=y+z;
break;

 2003 Prentice Hall, Inc. All rights reserved.


40
public class SwitchExample1 { Outline
public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements Get user’s input in JApplet
case 10: [Link]("10");
break;
case 20: [Link]("20");
break;
case 30: [Link]("30");
break;
//Default case statement
default:[Link]("Not in 10, 20 or 30");
}
}
 2003 Prentice Hall, Inc.
} All rights reserved.
41

case a [true]
case a action(s) break
[false]

[true]
case b case b action(s) break
[false]

.
.
.

[true]
case z case z action(s) break
[false]

default action(s)

Fig. 5.10 switch multiple-selection statement activity diagram with


break statements.

 2003 Prentice Hall, Inc. All rights reserved.


42

5.7 break and continue Statements

• break/continue
– Alter flow of control
• break statement
– Causes immediate exit from control structure
• Used in while, for, do…while or switch statements
• Escape early from loop or skip remainder of switch
• continue statement
– Skips remaining statements in loop body
– Proceeds to next iteration
• Used in while, for or do…while statements

 2003 Prentice Hall, Inc. All rights reserved.


43
1 // Fig. 5.11: [Link] Outline
2 // Terminating a loop with break.
3 import [Link];
4 [Link]
5 public class BreakTest {
6 exit for structure (break)
7 public static void main( String args[] ) Line 12
when count equals 5
8 {
Loop 10 times
9 String output = ""; Lines 14-15
10 int count;
11
12 for ( count = 1; count <= 10; count++ ) { // loop 10 times
13
14 if ( count == 5 ) // if count is 5,
15 break; // terminate loop
16
17 output += count + " ";
18
19 } // end for
20
21 output += "\nBroke out of loop at count = " + count;
22 [Link]( null, output );
23
24 [Link]( 0 ); // terminate application
25
26 } // end main
27
28 } // end class BreakTest

 2003 Prentice Hall, Inc.


All rights reserved.
44
1 // Fig. 5.12: [Link] Outline
2 // Continuing with the next iteration of a loop.
3 import [Link];
4 [Link]
5 public class ContinueTest {
Skip line 16 and proceed va
to
6
7 public static void main( String args[] ) line 11 when count equals 5
8 { Loop 10 times Line 11
9 String output = "";
10
Lines 13-14
11 for ( int count = 1; count <= 10; count++ ) { // loop 10 times
12
13 if ( count == 5 ) // if count is 5,
14 continue; // skip remaining code in loop
15
16 output += count + " ";
17
18 } // end for
19
20 output += "\nUsed continue to skip printing 5";
21 [Link]( null, output );
22
23 [Link]( 0 ); // terminate application
24
25 } // end main
26
27 } // end class ContinueTest

 2003 Prentice Hall, Inc.


All rights reserved.
45
5.8 Labeled break and continue
Statements
• Labeled block
– Set of statements enclosed by {}
– Preceded by a label
• Labeled break statement
– Exit from nested control structures
– Proceeds to end of specified labeled block
• Labeled continue statement
– Skips remaining statements in nested-loop body
– Proceeds to beginning of specified labeled block
– Controversial – disguised “goto” statement!
– Always a better way than labeled continue

 2003 Prentice Hall, Inc. All rights reserved.


46
1 // Fig. 5.13: [Link] Outline
2 // Labeled break statement.
3 import [Link];
4 BreakLabelTest.
5 public class BreakLabelTest {
6 stop is the labeled block java
7 public static void main( String args[] )
8 { Line 11
9 String output = ""; Loop 10 times
10
Line 14
11 stop: { // labeled block
12 Nested loop 5 times
13 // count 10 rows Line 17
14 for ( int row = 1; row <= 10; row++ ) {
15 Lines 19-20
16 // count 5 columns
17 for ( int column = 1; column <= 5 ; column++ ) {
18
19 if ( row == 5 ) // if row is 5,
20 break stop; // jump to end of stop block
21
22 output += "* ";
23
24 } // end inner for
25 Exit to line 35 (next slide)
26 output += "\n";
27
28 } // end outer for
29

 2003 Prentice Hall, Inc.


All rights reserved.
47
30 // following line is skipped Outline
31 output += "\nLoops terminated normally";
32
33 } // end labeled block BreakLabelTest.
34
35 [Link]( null, output,
java
36 "Testing break with a label",
37 JOptionPane.INFORMATION_MESSAGE );
38
39 [Link]( 0 ); // terminate application
40
41 } // end main
42
43 } // end class BreakLabelTest

 2003 Prentice Hall, Inc.


All rights reserved.
48
1 // Fig. 5.14: [Link] Outline
2 // Labeled continue statement.
3 import [Link];
4 ContinueLabelTe
5 public class ContinueLabelTest {
6 nextRow is the labeled block [Link]
7 public static void main( String args[] )
8 { Line 11
9 String output = ""; Loop 5 times
10
Line 14
11 nextRow: // target label of continue statement
12
13 // count 5 rows Line 17
Nested loop 10 times
14 for ( int row = 1; row <= 5; row++ ) {
15 output += "\n"; Lines 21-22
16
17 // count 10 columns per row
18 for ( int column = 1; column <= 10; column++ ) {
19
20 // if column greater than row, start next row
21 if ( column > row )
22 continue nextRow; // next iteration of labeled loop
23
24 output += "* ";
25
26 } // end inner for
27
continue to line 11 (nextRow)
28 } // end outer for

 2003 Prentice Hall, Inc.


All rights reserved.
49
29 Outline
30 [Link]( null, output,
31 "Testing continue with a label",
32 JOptionPane.INFORMATION_MESSAGE ); ContinueLabelTe
33
34 [Link]( 0 ); // terminate application
[Link]
35
36 } // end main
37
38 } // end class ContinueLabelTest

 2003 Prentice Hall, Inc.


All rights reserved.
50

5.9 Logical Operators

• Logical operators
– Allows for forming more complex conditions
– Combines simple conditions
• Java logical operators
– && (conditional AND) (short circuit)
– & (boolean logical AND)
– || (conditional OR) (short circuit)
– | (boolean logical OR)
– ^ (boolean logical exclusive OR)
– ! (logical NOT)

(project > 75 && exam>=


80)
 2003 Prentice Hall, Inc. All rights reserved.
51

expression1 expression2expression1 &&


expression2
false false false
false true false
true false false
true true true
Fig. 5.15 && (conditional AND) operator truth table.

expression1 expression2expression1 ||
expression2
false false false
false true true
true false true
true true true
Fig. 5.16 || (conditional OR) operator truth table.

 2003 Prentice Hall, Inc. All rights reserved.


52

expression1 expression2
expression1 ^
expression2
false false false
false true true
true false true
true true false
Fig. 5.17 ^ (boolean logical exclusive OR) operator truth table.

expression !expression
false true
true false
Fig. 5.18 ! (logical negation, or logical NOT) operator truth table.

 2003 Prentice Hall, Inc. All rights reserved.


53
1 // Fig. 5.19: [Link] Outline
2 // Logical operators.
3 import [Link].*;
4 LogicalOperator
5 public class LogicalOperators
6
[Link]
7 public static void main( String args[] )
8 { Lines 16-20
9 // create JTextArea to display results
10 JTextArea outputArea = new JTextArea( 17, 20 );
Lines 23-27
11
12 // attach JTextArea to a JScrollPane so user can scroll results
13 JScrollPane scroller = new JScrollPane( outputArea );
Conditional AND truth table
14
15 // create truth table for && (conditional AND) operator
16 String output = "Logical AND (&&)" +
17 "\nfalse && false: " + ( false && false ) +
18 "\nfalse && true: " + ( false && true ) +
19 "\ntrue && false: " + ( true && false ) +
20 "\ntrue && true: " + ( true && true );
21 Conditional OR truth table
22 // create truth table for || (conditional OR) operator
23 output += "\n\nLogical OR (||)" +
24 "\nfalse || false: " + ( false || false ) +
25 "\nfalse || true: " + ( false || true ) +
26 "\ntrue || false: " + ( true || false ) +
27 "\ntrue || true: " + ( true || true );
28

 2003 Prentice Hall, Inc.


All rights reserved.
54
29 // create truth table for & (boolean logical AND) operator Outline
30 output += "\n\nBoolean logical AND (&)" +
31 "\nfalse & false: " + ( false & false ) +
32 "\nfalse & true: " + ( false & true ) + LogicalOperator
33 "\ntrue & false: " + ( true & false ) +
Boolean logical AND [Link]
34 "\ntrue & true: " + ( true & true );
35 truth table
36 // create truth table for | (boolean logical inclusive OR) operator Lines 30-34
37 output += "\n\nBoolean logical inclusive OR (|)" +
38 "\nfalse | false: " + ( false | false ) +
Lines 37-41
39 "\nfalse | true: " + ( false | true ) +
40 "\ntrue | false: " + ( true | false ) +
41 "\ntrue | true: " + ( true | true );
Boolean logical inclusiveLines 44-48
42 OR truth table
43 // create truth table for ^ (boolean logical exclusive OR) operator Lines 51-53
44 output += "\n\nBoolean logical exclusive OR (^)" +
45 "\nfalse ^ false: " + ( false ^ false ) +
46 "\nfalse ^ true: " + ( false ^ true ) +
47 "\ntrue ^ false: " + ( true ^ false ) +
48 "\ntrue ^ true: " + ( true ^ true ); Boolean logical exclusive
49 OR truth table
50 // create truth table for ! (logical negation) operator
51 output += "\n\nLogical NOT (!)" +
52 "\n!false: " + ( !false ) +
53 "\n!true: " + ( !true ); Logical NOT truth table
54
55 [Link]( output ); // place results in JTextArea
56

 2003 Prentice Hall, Inc.


All rights reserved.
55
57 [Link]( null, scroller,
58 "Truth Tables", JOptionPane.INFORMATION_MESSAGE );
Outline
59
60 [Link]( 0 ); // terminate application
61 LogicalOperator
62 } // end main [Link]
63
64 } // end class LogicalOperators

 2003 Prentice Hall, Inc.


All rights reserved.

You might also like