Programming Concepts

advertisement
Department of Computer Engineering
Decisions
2140101 Computer Programming for International Engineers
Department of Computer Engineering
Objectives
Students should:
• Be able to use Java conditional constructs,
including if, if-else, and switch, as well as
the nested version of those constructs
correctly.
• Be able to perform equality testing
suitable with each primitive data type.
• Be able to design programs that require
the inclusion of decisions.
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
2
Department of Computer Engineering
Conditional constructs
• Provide
– Control whether statements listed is executed.
• Two constructs
if
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
3
Department of Computer Engineering
‘If’ Construct
• Syntax
if (Test Expression)
Action
• Action is either a single statement or
a group of statements within braces.
Test Expression
• If the boolean of Test Expression is
true then execute Action.
• Otherwise, the program will skip
over Action to the next statements, if
there are any.
true
Action
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
false
4
Department of Computer Engineering
Example
if (d < 0) {
d = -d;
}
Is our number
negative?
d<0
If d is less than
zero then we need
to update its value
to that of its additive
inverse
true
false
d = -d
If d is not
less than zero
then our number
is fine as is
Our number is now
definitely
nonnegative
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
5
Department of Computer Engineering
Sorting two values
import java.io.*;
public class ShowInOrder
{
public static void main(String[] args) throws IOException
{
double d1,d2,temp;
BufferedReader stdin
= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the 1 st. number:");
d1 = Double.parseDouble(stdin.readLine());
System.out.print("Enter the 2 nd. number:");
d2 = Double.parseDouble(stdin.readLine());
if(d1 > d2){
temp = d1;
d1 = d2;
d2 = temp;
}
System.out.println("Showing the numbers from small to large.");
System.out.println(d1+", "+d2);
}
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
6
Department of Computer Engineering
If semantics
Rearrange d1 and d2
to put their values
in the proper order
d2 < d1
true
false
temp = d1;
d1 = d2;
d2 = temp;
The numbers were rearranged into
the proper order
The numbers were initially
in order
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
7
Department of Computer Engineering
‘If-else’ Construct
• Syntax
if (Test Expression)
Action1
else
Action2
• If Test Expression is true then
execute Action1 otherwise execute
Action2
Test Expression
true
false
Action1
Action2
• The actions are either a single statement
or a list of statements within braces
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
8
Department of Computer Engineering
Finding the maximum of two values
import java.io.*;
public class PrintBiggerNumber
{
public static void main(String[] args) throws IOException
{
double d1,d2,bigger;
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the 1 st. number:");
d1 = Double.parseDouble(stdin.readLine());
System.out.print("Enter the 2 nd. number:");
d2 = Double.parseDouble(stdin.readLine());
if(d1 < d2)
bigger = d2;
else
bigger = d1;
System.out.println("The bigger number is "+bigger);
}
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
9
Department of Computer Engineering
Finding the maximum of two values (2)
Is d2 larger than d1
Yes, it is . So d2 is
larger than d1. In
this case, bigger is
set to d2
No, its not. So d1
is at least as large as
d2. In this case,
bigger is set to
d1
d1 < d2
false
true
bigger = value2
bigger = value1
Either case, bigger is set
correctly
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
10
Department of Computer Engineering
Nested If Construct
• Syntax
An if statement
if(Test Expression 1)
if(Test Expression2)
Action
Another if statement nested inside the
first one. This if statement is just one
of valid Java statements.
Or,
if(Test Expression 1){
if(Test Expression 2){
Action
}
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
11
Department of Computer Engineering
More Complex
if(Test Expression 1){
Action1
else{
can also be
if(Test Expression 2){
written as
Action2
}else{
if(Test Expression 3){
Action3
}
}
}
if(Test Expression 1)
Action1
else if(Test Expression 2)
Action2
else if(Test Expression 3)
Action3
See example program, FunnyEncoder 2
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
12
Department of Computer Engineering
Use Braces to Avoid Coding Confusions
• Consider the following code segment.
if(p)
System.out.println("A");
if(q)
System.out.println("B");
else{
System.out.println("C");
}
• What will the program print out when:
p is false and q is true?
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
13
Department of Computer Engineering
Use Braces to Avoid Coding Confusions
• Consider the following code segment.
if(p)
System.out.println("A");
if(q)
System.out.println("B");
else{
System.out.println("C");
}
• What will the program print out when:
p is false and q is true?
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
14
Department of Computer Engineering
Use Braces to Avoid Coding Confusions (2)
• Better
if(p){
System.out.println("A");
}
if(q){
System.out.println("B");
}else{
System.out.println("C");
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
15
Department of Computer Engineering
The ?: Operator
• An if statement can sometimes be replaced by the
? : operator
• Syntax
Test Expression ? Expression1 : Expression2
• If Test Expression is true, the value of the whole
expression is equivalent to the value of Expression1.
• If Test Expression is false, the value of the whole
expression is equivalent to the value of Expression2.
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
16
Department of Computer Engineering
Example
int bigger = (intA > intB) ? intA : intB;
is equivalent to:
int bigger;
if(intA > intB){
bigger = intA;
else{
bigger = intB;
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
17
Department of Computer Engineering
Equality Testing
• Consider
int a = 1;
int b = 1;
System.out.println("a and b are "+((a==b)?"equal.":"not equal."));
//comparing char
char c1 = 'z';
char c2 = 'z';
System.out.println("c1 and c2 are "+((c1==c2)?"equal.":"not equal."));
//comparing double
double d1 = 1.44;
doubled2 = 1.44;
System.out.println("d1 and d2 are "+((d1==d2)?"equal.":"not equal."));
//comparing double
double d3 = 0.9;
double d4 = 0.3+0.3+0.3;
System.out.println("d3 and d4 are "+((d3==d4)?"equal.":"not equal."));
System.out.println("d3="+d3);
System.out.println("d4="+d4);
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
18
Department of Computer Engineering
Equality Testing
• We can see that a and b are equal, c1 and c2 are equal, as
well as d1 and d2 are equal as expected.
• However, d3 and d4 are not while they should. The
reason lies in the limitation in representing floating
points with binary representation.
• To compare whether two floating point values are equal,
we instead test whether the difference between them are
small enough. Thus, two values, x and y, are said to be
equal as long as |x-y| < ε, where ε is a small tolerance
value.
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
19
Department of Computer Engineering
Example
• Consider
public class EqualityTesting2
{
public static void main(String[] args)
{
double d3, d4;
final double MAX_DIFF = 1e-10;
d3 = 0.9;
d4 = 0.3+0.3+0.3;
System.out.println("d3="+d3);
System.out.println("d4="+d4);
boolean isEqual = (Math.abs(d3-d4)<MAX_DIFF)? true : false;
System.out.println("d3 and d4 are "+(isEqual?"equal.":"not equal."));
}
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
20
Department of Computer Engineering
Example
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
21
Department of Computer Engineering
String Equality Testing
• Consider
System.out.print("Enter a string: ");
String s1 = stdin.readLine();
System.out.print("Enter another string: ");
String s2 = stdin.readLine();
if (s1 == s2) {
System.out.println("Same");
}
else {
System.out.println("Different");
}
• What is the output if the user enters “test” both times?
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
22
Department of Computer Engineering
String Equality Testing
s1
“Test”
False!!
s2
“Test”
s1 == s2
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
23
Department of Computer Engineering
String Equality Testing
• To compare whether two String objects carry
similar character sequences, a method called
equals() is used.
• Consider
String s1
String s2
boolean p
boolean q
=
=
=
=
“Viva Java”;
“Viva Java”;
s1.equals(s2);
(s1==s2);
• The boolean variable p is true, while q is false.
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
24
Department of Computer Engineering
‘Switch’ Constructs
• Java provides a
conditional construct that
selects which code
segment to be executed
from a number of them
based on an integer value.
• E.g. a value of type int,
char,
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
25
Department of Computer Engineering
‘Switch’ Constructs (2)
• Syntax
switch(SwitchExpression){
case Case1 :
Action1
break;
case Case2 :
Action2
break;
Case1
:
Action1
:
:
case CaseN :
ActionN
break;
default :
DefaultAction
}
SwitchExpression
CaseN
Case2
Action2
….
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
Action N
Default
Default Action
26
Department of Computer Engineering
‘Switch’ Constructs (3)
• When SwitchExpression is evaluated. The program flow
jumps to the case whose associated CaseExpression equals
to the value of SwitchExpression.
• if the value of SwitchExpression does not match with any
CaseExpression’s, the program flowjumps to default.
• After that, the code below that point will be executed
until a break command or the end of the switch block is
reached.
• When a break is reached, the program flow jumps to the
closing braces of that switch statement immediately.
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
27
Department of Computer Engineering
SwitchDemo
import java.io.*;
public class SwitchDemo
{
public static void main(String[] args) throws
IOException
{
int n;
String stringToPrint;
BufferedReader stdin
= new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter an integer from 1-4");
n = Integer.parseInt(stdin.readLine());
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
28
Department of Computer Engineering
SwitchDemo
switch(n){
case 1:
stringToPrint
break;
case 2:
stringToPrint
break;
case 3:
stringToPrint
break;
case 4:
stringToPrint
break;
default:
stringToPrint
}
= "*";
= "* *";
= "* * *";
= "* * * *";
= "Integer out of range.";
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
29
Department of Computer Engineering
SwitchDemo
System.out.println(stringToPrint);
}
}
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
30
Department of Computer Engineering
SwitchDemo2
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
31
Department of Computer Engineering
SwitchDemo2
See another
example,
BaseConverter
2140101 Computer Programming for International Engineers
INTERNATIONAL SCHOOL OF ENGINEERING
CHULALONGKORN UNIVERSITY
32
Download