import javax.swing.JOptionPane; public class Ch5 { double balance = -100; // predicate methods return a boolean value public void predicateMethods() { char ch = 'f'; if (Character.isLowerCase(ch)) System.out.println(ch + " is lowercase."); if (Character.isLetter(ch)) System.out.println(ch + " is a letter."); } // predicate method: public boolean isOverdrawn() { return balance < 0; } public void booleanExpressions() { double amount = 100; double cash = 200; System.out.println(amount > 0); System.out.println(2 > 3); boolean married = false; System.out.println(married == true); System.out.println(amount > 0 && cash > 500); System.out.println(amount > 0 || cash > 500); String ltr = "A"; System.out.println(ltr.equals("B")); System.out.println( ! ltr.equals("B")); String msg = "\nWe expect:\ntrue\nfalse\nfalse\nfalse\ntrue\nfalse\ntrue\n"; System.out.println(msg); } public void nestedIF() { // simplified tax calculator boolean married = false; double income = 75000, tax; if (income >= 200000) if (married) // same as if (married == true) tax = 0.25 * income; else tax = 0.2 * income; else if (income >= 100000) if (married) tax = 0.2 * income; else tax = 0.18 * income; else if (income >= 50000) if (married) tax = 0.15 * income; else tax = 0.12 * income; else if (married) tax = 0.1 * income; else tax = 0.08 * income; String msg = "Your income is $" + income; msg += " and you are married: " + married; msg += " and your tax is $" + tax; JOptionPane.showMessageDialog(null, msg); } public void ifStatement() { int age = 91; String msg = ""; if (age < 16) msg = "You'll have to wait."; else if (age == 16) msg = "You can get a learner's permit."; else if (age == 17) msg = "You can get a provisional license."; else if (age >= 18 && age <= 90) msg = "You can get a full driver's license."; else msg = "You probably shouldn't be driving."; System.out.println(msg); } public void compareNumbers() { double r = Math.sqrt(2); double d = r * r - 2; System.out.println("d: " + d); //if (d == 0) if (Math.abs(d) < 1.0E-15) System.out.println("you get 0"); else System.out.println("you don't get 0"); } public void compareStrings() { String name1 = "Patrick"; String name2 = "Luka"; String name3 = "Luka"; if (name1.equals(name2)) System.out.println("They're the same."); else if (! name1.equals(name2)) System.out.println("They're different."); // OR int result = name1.compareTo(name2); System.out.println("result: " + result); result = name2.compareTo(name3); System.out.println("result: " + result); } public static void main(String[] args) { Ch5 c = new Ch5(); //c.ifStatement(); //c.compareNumbers(); //c.compareStrings(); //c.nestedIF(); //c.predicateMethods(); //System.out.println(c.isOverdrawn()); c.booleanExpressions(); } }