Introduction to programming in java Lecture 11 Boolean

Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2

Boolean Expressions • Topics – Relational Operators (review) – Logical Operators – AND Operator – How to check that a number is in range – Boolean Expressions – OR Operator – Comparison between AND and OR – NOT Operator

Relational operators

Relational expression • The symbol && means AND. The if statement asks a question with two parts: if ( x >= 5 && x <= 10 ) 1 st part 2 nd part • Each part is a relational expression. • A relational expression uses a relational operator to compute a true or false value.

AND Operator • The and operator && is a logical operator. A logical operator examines two true/false values and outputs a single true/false value. • Here is what && does: – true && true = true – false && true = false – true && false = false – false && false = false • Use and when every requirement must be met.

AND operator Example

OR operator • The symbol || (vertical-bar) means OR operator. The OR operator evaluates to true when either qualification is met or when both are met. • The if statement asks a question with two parts: if (cash >= 25000 || credit >= 25000) If either part is true, or both parts are true, then the entire boolean expression is true.

OR operator (Cont…) • Here is how || works: – true || true = true – false || true = true – true || false = true – false || false = false

OR operator Example

NOT! Operator • The NOT operator in Java is this: ! (exclaimation point). • The NOT operator changes true to false and false to true, as seen in the truth table.

Example if ( !(cost < 50) ) System. out. println("Reject these shoes") else System. out. println("Acceptable shoes");

Practice Question • The front tires of a car should both have the same pressure. Also, the rear tires of a car should both have the same pressure (but not necessarily the same pressure as the front tires). Write a program that reads in the pressure of the four tires and writes a message that says if the inflation is OK or not. • Example: Input right front pressure 38 Input left front pressure 38 Input right rear pressure 42 Input left rear pressure 42 Inflation is OK

Homework Assignment # 2 (Total marks: 5) Submission deadline: 8 th Oct 2013 NOTES: 1. 2. 3. Only HAND WRITTEN assignments are acceptable on A 4 size white paper. If any student copy assignment from other then -5 marks will be deducted from the overall grade. Late submission is not allowed.

Question 01 • Write a program called Pass. Or. Fail. java that takes two positive integers as command-line arguments and prints PASS if sum of both the integers is greater than 60 otherwise it should prints FAIL. For example: > java Pass. Or. Fail 40 30 Total = 70. Your are PASS.

Question 02 • Write a program that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

Question 3 • A bank has the following rule: if a customer has more than $1000 dollars in their checking account or more than $1500 dollars in their savings account, then there is no service charge for writing checks. Otherwise there is a $0. 15 charge per check. Write a program that asks for the balance in each account and then writes out the service charge.

Question 4 • Write a program called My. Calculator. java which reads two integer values and one operator ( + , - , / , *) from the standard input and print the result. Note use switch statement in this program. For example: > java My. Calculator Enter 1 st number: 5 Enter 2 nd number: 10 Enter operator: + Answer: 5 + 10 = 15

Question 5 • The maximum possible efficiency of a steam engine depends on the temperature of the steam in the boiler and the temperature of the outside air: efficiency = 1 - Tair / Tsteam where Tair is the air temperature and Tsteam is the steam temperature. The temperatures are given in degrees above absolute zero. Normal air temperature is about 300 K. Boiling is 373 K. Write a program that asks the user for the air temperature and the steam temperature and writes out the maximum possible efficiency of a steam engine. However, if the steam temperature is less than 373 K there is no steam, so the efficiency is zero.
- Slides: 18