1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if -...
-
date post
21-Dec-2015 -
Category
Documents
-
view
220 -
download
2
Transcript of 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if -...
![Page 1: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/1.jpg)
1
Tirgul no. 2Tirgul no. 2
Topics covered:
Reading Input from the user. Printing Output to the user. if - else statement and switch. Boolean Operators. Checking your input. Logical Operators.
![Page 2: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/2.jpg)
2
Reading Input from the userReading Input from the user We leave the discussion of Input for later chapters For now we will supply you with classes for getting input in
all exercises and examples. The class called EasyInput allows you to read input from the
user.
In order to read an integer/double we use EasyInput as follows:
int numberOfItems = EasyInput.readInt(); double width = EasyInput.readDouble();
![Page 3: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/3.jpg)
3
Reading Input (contd.)Reading Input (contd.)
Please note that EasyInput is not a core Java class. We have supplied it for simplicity.
The EasyInput api is supplied on the course site.
![Page 4: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/4.jpg)
4
Example - Multiplying two numbersExample - Multiplying two numbers// Requests two integers and prints their// multiplication.class MultiplicationExample { public static void main(String[] args) { int a,b; int mul; System.out.print(“The first number: “); a = EasyInput.readInt(); System.out.print(“The second number: “); b = EasyInput.readInt(); mul = a * b; System.out.println(“The multiplication is”
+ mul); }}
![Page 5: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/5.jpg)
5
Example: Circle area and Example: Circle area and circumferencecircumference
// Reads the radius of a circle and prints// its circumference and area to an output windowclass CircleExample { static final double PI = 3.1415927; public static void main(String[] args) { double r, circumference, area;
System.out.print(“Enter radius: “); r = EasyInput.readDouble(); circumference = 2*PI*r; area = PI*r*r; System.out.print(“Circumference: “); System.out.println(circumference); System.out.print(“Area: “);
System.out.println(area); }}
![Page 6: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/6.jpg)
6
The if StatementThe if Statement
The Java if statement has the following syntax:
if (boolean-condition)
statement;
If the Boolean condition is true, the statement is executed; if it is false, the statement is skipped
This provides basic decision making capabilities
![Page 7: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/7.jpg)
7
TemperatureTemperature
class Temperature {
static final int THRESHOLD = 65;
public static void main(String[] args) {
System.out.print(“Enter the temperature:”);
int temperature = EasyInput.readInt();
System.out.println(“Current temperature “+
temperature);
if (temperature < THRESHOLD)
System.out.println(“It’s cold in here!”);
}
}
![Page 8: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/8.jpg)
8
Boolean ExpressionsBoolean Expressions
The condition of an if statement must evaluate to a true or false result
Java has several equality and relational operators:
Operator
==!=<<=>>=
Meaning
equal tonot equal to
less thanless than or equal to
greater thangreater than or equal to
![Page 9: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/9.jpg)
9
Block StatementsBlock Statements
Several statements can be grouped together into a block statement
Blocks are delimited by braces A block statement can be used wherever a
statement is called for in the Java syntax See Temperature2.java
![Page 10: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/10.jpg)
10
Example - Temperature2Example - Temperature2class Temperature2 { static final int THRESHOLD = 65;
public static void main(String[] args) { System.out.print(“Enter the temperature: ”); int temperature = EasyInput.readInt(); System.out.println(“Current temperature “+ temperature); if (temperature < THRESHOLD) { System.out.println(“It’s cold in here!”); System.out.println(“But we’ll survive.”); }
}}
![Page 11: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/11.jpg)
11
If .. Else StatementIf .. Else Statement An else clause can be added to an if
statement to make it an if-else statement:if (condition) statement1;else statement2;
If the condition is true, statement1 is executed; if the condition is false, statement2 is executed
See Temperature3.java.
![Page 12: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/12.jpg)
12
Example - Temperature3Example - Temperature3class Temperature3 {
static final int FREEZING_POINT = 32;
public static void main(String[] args) { System.out.print(“Enter the temperature: ”); int temperature = EasyInput.readInt();
if (temperature <= FREEZING_POINT)
System.out.println(“It’s freezing!”);
else
System.out.println(“Above freezing.”);
}
}
![Page 13: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/13.jpg)
13
Checking your InputChecking your Input When requesting input from the user, you keep in
mind that the input may be invalid. One way to handle this is to use if – else control statement:
//number of items should be positive
int numberOfItems = EasyInput.readInt(); if (numberOfItems < 0) { System.out.println( “Number of items must be positive!”);}else { double price = numberOfItems * ITEM_PRICE; System.out.print(“The total price is:“);
System.out.println(price);}
![Page 14: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/14.jpg)
14
Logical OperatorsLogical Operators
There are three logical operators in Java:
They all take boolean operands and produce boolean results
Logical NOT is unary (one operand), but logical AND and OR are binary (two operands)
Operator
!&&||
Operation
Logical NOTLogical ANDLogical OR
![Page 15: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/15.jpg)
15
Logical NOTLogical NOT
The logical NOT is also called logical negation or logical complement
If a is true, !a is false; if a is false, then !a is true
Logical expressions can be shown using truth tables
a
falsetrue
!a
truefalse
![Page 16: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/16.jpg)
16
Logical ANDLogical AND
The expression a && b is true if both a and b are true, and false otherwise
Truth tables show all possible combinations of all terms
a
falsefalsetruetrue
b
falsetruefalsetrue
a && b
falsefalsefalsetrue
![Page 17: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/17.jpg)
17
Logical ORLogical OR
The expression a || b is true if a or b or both are true, and false otherwise
a
falsefalsetruetrue
b
falsetruefalsetrue
a || b
falsetruetruetrue
![Page 18: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/18.jpg)
18
Logical OperatorsLogical Operators
Logical operators are used to form more complex logical expressions
if (a<1 || a%2!=0) {
System.out.println(
“The input should be a positive “+
“even number!”);
return;
}
Logical operators have precedence relationships between themselves and other operators
![Page 19: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/19.jpg)
19
Logical OperatorsLogical Operators
Full expressions can be evaluated using truth tables
a < 1
falsefalsetruetrue
a%2!=0
falsetruefalsetrue
a<1 || a%2!=0
falsetruetruetrue
![Page 20: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/20.jpg)
20
Nested IfNested If// Receives 2 integers and compares themclass CompareExample { public static void main(String[] args) { System.out.print(“First number: ); int a = EasyInput.readInt(); System.out.print(“Second number: ); int b = EasyInput.readInt();
if (a != b) if (a > b) System.out.println(a+” is greater”); else System.out.println(b+” is greater”); else System.out.println(“the numbers are equal”); }}
![Page 21: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/21.jpg)
21
Switch ConstructSwitch ConstructSwitch( integral expression) { case integral-value1: statements; break; case integral-value2: statements; break; : : case integral-valueN: statements; break; default: statements; break;}
![Page 22: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/22.jpg)
22
Switch Construct (cont.)Switch Construct (cont.)Switch( integral expression) { case integral-value1: case integral-value2: statements; break; : : case integral-valueN: statements; break; default: statements; break;}
![Page 23: 1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649d5a5503460f94a3add7/html5/thumbnails/23.jpg)
23
public class Schedule { public final int SUNDAY = 1; public final int MONDAY = 2; public final int TUESDAY = 3; public void printSchedule(int day) { System.out.println(“Schedule for today is:”); switch(day) { case SUNDAY: System.out.println(“10:00 - 12:00 : Infi”); System.out.println(“12:00 - 14:00 : Alg”); break; case MONDAY: case TUESDAY: System.out.println(“10:00 - 13:00 discrete math”); break; default: System.out.println(“Error: No such day”); break; } }}