1 / 14

Java Methods

Chapter 5. Java Methods. Conditionals Booleans Relational and Logical Operators Switch statements. Topics Covered. If-else statements Use logical expressions Else statement is optional Booleans Primitive data type – only true or false Relational operators >,<,>=,<=, ==, !=

ansel
Download Presentation

Java Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Java Methods

  2. Conditionals Booleans Relational and Logical Operators Switch statements Topics Covered

  3. If-else statements • Use logical expressions • Else statement is optional • Booleans • Primitive data type – only true or false • Relational operators • >,<,>=,<=, ==, != • Most mistakes made confusing = and == Ideas already covered in previous sections

  4. Program that evaluates even or odd: int x = 15; if(x%2 == 1) {System.out.println(“odd”);} else {System.out.println(“even”);} --no need to evaluate with an expression since the only other possibility is even What would be returned above? Relational operators

  5. Program that evaluates gender String gender = “male”; if(gender == “male”) {System.out.println(“guy”);} Else {System.out.println(“lady”);} What is displayed on your screen? Relational operators

  6. Try this: String gender = “ma”; Gender += “le”; //gender now is “male”; if(gender == “male”) {System.out.println(“guy”);} Else {System.out.println(“lady”);} Anything change? Try it! Relational operators

  7. When using == and != for objects (like Strings), you compare their references, not their values, so it won’t always work Instead of == and !=, objects typically have methods for comparison: if(gender.equals(“male”)) {System.out.println(“guy”);} Else {System.out.println(“lady”);} Relational operators

  8. We have already covered &&, ||, and ! Results of these operators have boolean data types Match the following to it’s equivalent (2 pair): 1. !(p&& q) 2. !(p || q) 3. !p && !q 4. !p || !q Logical Operators

  9. Logical operators are read left-right If(rightIsClear() && frontIsClear() && hasBeepers()) If rightIsClear() is FALSE, the other operators ARE NOT evaluated – this is short circuit evaluation Short circuit evaluation

  10. We have used if-else statements which evaluate one of 2 possible conditions Given multiple conditons: If(condition1) {…} Else if (condition2) {…} Else if (condition3) {…} Else // optional given the situation If-else-if

  11. Given a situation where one of several outcomes depend on a value: IntAPscore; If(APscore = 1) {…} Else if (APscore = 2) {…} Else if (APscore = 3) {…} Else if (APscore = 4) {…} Else {…} Switch statement

  12. If-else-if statements can be a little messy • Use switch statement instead: • IntAPscore; • Switch(APscore) • { case 1: • …. //do action • break; • case 2: • … // do action • break; } Switch statements

  13. Practice Create a short program using switch statements that does the following: Create a scanner object that asks a user to input a number from 1-5 (if it is a different number, have the user re-enter the input) Have a different output phrase given the number Switch statements

  14. Case study – rolling dice Read carefully, as you will need to understand the game of craps to program Exercise set - 1, 2, 3, 4, 5, 6, 9, 11, 13, 15, 19 HW

More Related