Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2."— Presentation transcript:

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

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

3 Relational operators

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

5 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.

6 AND operator Example

7 OR operator The symbol || (vertical-bar 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.

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

9 OR operator Example

10 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.

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

12 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

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

14 Question 01 Write a program called PassOrFail.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 PassOrFail 40 30 Total = 70. Your are PASS.

15 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.

16 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.

17 Question 4 Write a program called MyCalculator.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 MyCalculator Enter 1st number: 5 Enter 2nd number: 10 Enter operator: + Answer: 5 + 10 = 15

18 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.


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

Similar presentations


Ads by Google