Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.

Similar presentations


Presentation on theme: "Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either."— Presentation transcript:

1 Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either true or false String myName = “fintan”; String userName; userName = JOptionPane.showInputDialog(null,” enter name :”); if (userName.equals(myName) ) { JOptionPane.showMessageDialog(null,” Hello, O great creator !”); } else { JOptionPane.showMessageDialog(null,” Hello, “+userName); } ints floats doubles are all primitive types: this is why we compare them with these operators. To compare reference types like Strings we use methods. The method.equals() belongs to a String ( userName, above) and takes another String as its argument. It returns true if both Strings contain the same chars in the same order, and false otherwise.

2 More conditionals with Strings We can use myName.equals(userName) in an if boolean expression Or we could use userName.equals(myName) if we liked Each uses the.equals method in a String to compare it to another string, and return true or false. We can use this.equals() method with any other reference type (we’ll see others later on). We use == only for primitive types like numbers. We can use other methods inside a boolean expression too, if we want: if (userName.length() > 50) { JOptionPane.showMessageDialog(null,” Your name is too long !”); } else { JOptionPane.showMessageDialog(null,” Hello, “+userName); }

3 Assume the following: // two boolean variables with // initial values boolean honest = true; boolean happy = false;

4 Nested if-statements if (honest){ if (happy){ System.out.print("Poor man"); } else { System.out.print("Beggarman"); } } else { if (happy) { System.out.print("Rich man"); } else { System.out.print("Thief"); }

5 One-liners: no braces necessary This is permissible: if (input > 0) System.out.println("input is ok"); else System.out.println("input is not ok"); But what if we add a line to either the 'true' set or the 'false' set? Recommend: always use braces, even for one-liners. (A single exception exists: multiple nested alternatives)

6 Multiple alternatives if (score >= 85.0) { grade = 'A'; } else { if (score >= 70.0) { grade = 'B'; } else { if (score >= 55.0) { grade = 'C'; } else { if (score >= 40.0) { grade = 'D'; } else {......................etc

7 Multiple alternatives: better if (score >= 85.0) grade = 'A'; else if (score >= 70.0) grade = 'B'; else if (score >= 55.0) grade = 'C'; else if (score >= 40.0) grade = 'D'; else grade = 'F';

8 Computing leap year Write a program to determine whether a given year is leap. A year is leap if: it is divisible by 4.......................................2004 is leap but if divisible by 100, it is not leap...........2100 is not leap unless it is also divisible by 400.................2000 was leap

9 Example to study….. Study example 3.1, p. 78-79. Note how each separate step in the problem is commented Note how the author uses JOptionPane dialogs to get input ( the author uses 4 arguments for JOptionPane dialogs, rather than the two options we have used. See page 48 for an explanation of the two extra arguments.) In the section “//compute mortgage” use is made of a method from the Math class: Math.pow(1/(…), numOfYears*12) // sample use of Math.pow int i = Math.pow(2,3); // i is 8

10 More on ‘Math.pow()’ Math.pow() is provided by the class Math The method’s name is pow It takes two arguments. Lets call these ‘a’ and ‘b’, both integers When called, it computes a-to-the-power-of-b The value returned is a-to-the-power-of-b In class Math the method’s definition would look something like this: public static int pow(int a, int b) { // code to compute a-to-the-b here }

11 Switch statement I // we can do this..... if (numOfYears == 7) { annualInterestRate = 7.25; } else if (numOfYears == 15) { annualInterestRate = 8.50 } else if (numOfYears == 30) { annualInterestRate = 9.0; } else { System.out.println(“....”); }

12 Switch statement II // or we can do this.... switch (numOfYears) { case 7: annualInterestRate = 7.25; break; case 15: annualInterestRate = 8.50; break; case 30: annualInterestRate = 9.0; break; default: System.out.println(“...”); }

13 Switch statement III Rules for switch statement: 1. choice among alternatives depends on the value in parentheses—the switch-expression. 2. The switch-expression must evaluate to an integer type (char, int, byte, short) 3. The cases are also indexed by an integer type 4. If the break statement is absent, execution continues with the next case statement 5. The default case is optional, and is executed if no case matches the switch-expression. 6. Use of this construct is entirely optional

14 Homework You should be reading through the textbook a group of pages every week, following what I’m doing the the lectures. You shouldn’t need me to tell you this! A reminder: for these last two lectures, you should be reading chapter 3, as far as page 81. You should also be doing exercises from the chapter! Pick exercises where you have to write a program. Then you can check if you got the exercise right by seeing if the program operates as required.


Download ppt "Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either."

Similar presentations


Ads by Google