Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Boolean and Relational Expressions

Similar presentations


Presentation on theme: "More Boolean and Relational Expressions"— Presentation transcript:

1 More Boolean and Relational Expressions
Lecture 7.2 More Boolean and Relational Expressions

2 Java boolean expressions
boolean isDry, isBig, hasMoney; isDry = true; isBig = !isDry; hasMoney = isDry || isBig; System.out.println( hasMoney ); if (!(isDry && !isBig)) { System.out.println(‘A’); } else { System.out.println(‘B’); } © 2006 Pearson Addison-Wesley. All rights reserved

3 Java relational expressions
int bagSize, groupCount; Oval topCircle, bottomCircle; boolean areSameCircle; bagSize = 3; bagSize++; groupCount = bagSize + 2; System.out.println( bagSize > groupCount ); if (topCircle == bottomCircle) { System.out.println(“Null Circles”); } topCircle = new Oval(10, 10, 50, 50); bottomCircle = topCircle; System.out.println( topCircle == bottomCircle ); bottomCircle = new Oval(10, 10, 50, 50); © 2006 Pearson Addison-Wesley. All rights reserved

4 Exercise Write a method that returns true/false to reflect whether
or not both of its Oval parameters are colored red.

5 The == Operator The == and != operations apply to both primitive and reference expressions. What is meant by the following expression? myInt == yourInt How about the following? myContainer == yourContainer java.awt.Container myContainer, yourContainer; myContainer = new Container(); myContainer.setBounds(10, 20, 30, 40); yourContainer = new Container(); yourContainer.setBounds(10, 20, 30, 40); System.out.println( myContainer == yourContainer ); The “==“ operation tests for equality of content in the case of primitives and tests for equality of identity in the case of reference expressions.

6 assert Java includes a special statement, assert, for run-time testing of assertions. Example /** pre: r1 != null and r2 != null * post: (r1’s area > r2’s area) implies result == true * and (r1’1 area <= r2’s area) implies result == false */ private boolean hasMoreArea( Rectangle r1, Rectangle, r2) { assert r1 != null && r2 != null : “Can’t compare null rectangles.”; return r1.getWidth() * r1.getHeight() > r2.getWidth()*r2.getHeight(); } © 2006 Pearson Addison-Wesley. All rights reserved

7 Exercise These are called NESTED if instructions.
Write a method that returns a letter grade for an exam score based upon a scale for A, B, C, D, F. /** pre: examScore > 0 */ private char letterGrade(int examScore) { } char grade; These are called NESTED if instructions.

8 a really BAD alternative…
/** pre: examScore > 0 */ private char letterGrade(int examScore) { char grade; if (examScore >= 90) { grade = 'A'; } else { if (examScore < 90 && examScore >= 80) { grade = 'B'; if (examScore < 80 && examScore >= 70) { grade = 'C'; if (examScore < 70 && examScore >= 60) { grade = 'D'; grade = 'F'; }

9 a really GOOD alternative…
/** pre: examScore > 0 */ private char letterGrade(int examScore) { char grade; if (examScore >= 90) { grade = 'A'; } else if (examScore < 90 && examScore >= 80) { grade = 'B'; } else if (examScore < 80 && examScore >= 70) { grade = 'C'; } else if (examScore < 70 && examScore >= 60) { grade = 'D'; } else { grade = 'F'; }

10 an even BETTER alternative…
/** pre: examScore > 0 */ private char letterGrade(int examScore) { char grade; if (examScore >= 90) { grade = 'A'; } else if (examScore >= 80) { grade = 'B'; } else if (examScore >= 70) { grade = 'C'; } else if (examScore >= 60) { grade = 'D'; } else { grade = 'F'; } Such cascading conditions work in certain cases.

11 Exercises Write a private void method with three double parameters. This method outputs (using System.out.println) the value of its parameters one per line in increasing order from smallest to largest. Chess players generally associate the following numeric values to define the importance of each chess piece: So, for example, a player with two rooks, a bishop and four pawns remaining would have a combined value of 2*5 + 3*3 + 4*1 = 23. Write a method with two parameters. The first parameter is a char that indicates a piece type ('P' for pawn, 'K' for knight, 'B' for bishop, 'R' for rook or 'Q' for queen). The second parameter is the quantity of such pieces still on the board. This method returns the total value of this quantity of this type of piece. 3. Write a private void method with a single int parameter. This method outputs (using System.out.println) a single line that contains the value of the parameter following by one of these messages: is negative.  for a parameter with a negative value is a prime digit.  for a parameter that is a prime number less than 10 is not prime.  for a parameter that is less than 10 and not prime is even.  for a parameter that is an even number 10 or greater is odd.  for a parameter that is an odd number 10 or greater

12 Exercises 4. Below is a table that shows the amount of each RGB component of a Color object. The Color class includes getRed(), getGreen() and getBlue() methods each returning an int. Write a private void method with one Color parameter that outputs one of the following words: (redish, greenish, blueish, grayish). Output colorish if one color - R,G or B – exceeds each of the other two by at least 20% and grayish only when none of the R,G or B dominates by 20%. Color getRed() getGreen() getBlue() Color.black Color.darkGray 64 Color.gray 128 Color.lightGray 192 Color.white 255 Color.red Color.yellow Color.green Color.cyan Color.blue Color.magenta

13 Exercises 5. Write a private method that returns the total cost as an integer number of cents for an electric bill. This method has a double parameter for the number of kilowatt hours (KWH) and a second parameter that is the month number (1 for January, 2 for February, and so forth). The cost is to be calculated as follows. (Assume the month number is a valid month.) Monthly Usage Categories 0 through 500 KWH … a flat fee of $10 over 500 through 1500 … $10 plus 12¢ per KWH over 500 (for Oct thru Mar) $10 plus 15¢ per KWH over 500 (for Apr thru Sep) over … 1st 1500 KWH calculated like above plus 10¢ per KWH over 1500

14 Exercises 6. Write a private method that accepts two playing cards as parameters and returns true exactly when the first playing card has a greater face amount than the second. Playing cards are represented with a single character, from greatest to least face value as follows: Ace  'A' King  'K' Queen  'Q' Jack  'J' 10  'T' 9  '9' 8  '8' . . . 2  '2' You may assume both parameters are one of the above 13 characters.


Download ppt "More Boolean and Relational Expressions"

Similar presentations


Ads by Google