/** * This class doesn't have any state (fields), it's just a convenient * place to put some sample code that uses if statements. Don't forget * that some of our first examples of IF statements were in the Circle * class. * * @author Brad Richards */ public class Conditionals { /** * The mystery method takes two integer inputs, and prints "yep" if they're * the same. It prints "nope" otherwise. */ public void mystery(int a, int b) { if (b < a) { System.out.println("nope"); } else { if (a < b) { System.out.println("nope"); } else { System.out.println("yep"); } } } public void simplerMystery(int a, int b) { if (a == b) { System.out.println("yep"); } else { System.out.println("nope"); } } /** * Takes three integers as inputs and returns the smallest * of the three. * @param a An integer input * @param b An integer input * @param c An integer input * @return The smallest of the three inputs. */ public int smallest(int a, int b, int c) { if (a < b) { if (a < c) { return a; } else { return c; } } else { if (b < c) { return b; } else { return c; } } } /** * A shorter version that takes advantage of && (and) to reduce the * number of cases. */ public int shorterSmallest(int a, int b, int c) { if (a < b && a < c) { // is a the overall winner? return a; } else { // If not, it must be b or c if (b < c) { return b; } else { return c; } } } /** * This method takes a numeric score and converts it to * a letter grade. (A double is a numeric value that can have a * decimal point in it.) * * @param score A numeric score in the range 0..100 * @return The letter grade corresponding to the score */ public String scoreToGrade(double score) { if (score >= 90) { return "A"; } else { if (score >= 80) { return "B"; } else { if (score >= 70) { return "C"; } else { if (score >= 60) { return "D"; } else { return "F"; } } } } } /** * Takes a letter grade as input and returns true if it's a * "good grade". We'll assume the input doesn't have a + or a * - following it. Note that we're using .equals() to compare * two strings, since == doesn't check the actual text of two * strings for equality. (== is only for primitive values.) * * @param grade The letter grade to inspect * @return True if the grade is a C or higher */ public boolean goodGrade(String grade) { if (grade.equals("A")) { return true; } else { if (grade.equals("B")) { return true; } else { if (grade.equals("C")) { return true; } else { return false; } } } } /** * Here we're using || (or) to cram all three .equals checks into ONE * test. */ public boolean shorterGoodGrade(String grade) { if (grade.equals("A") || grade.equals("B") || grade.equals("C")) { return true; } else { return false; } } /** * Here we're only testing for the *bad* grades. There are fewer of * them, so the code ends up being a little shorter. */ public boolean evenShorterGoodGrade(String grade) { if (grade.equals("D") || grade.equals("F")) { return false; } else { return true; } } /** * Don't even need an IF! The boolean expression evaluates to true when * we want to return true, and evaluates to false when we want to return * false. We can just return the expression's value! */ public boolean evenEvenShorterGoodGrade(String grade) { return (grade.equals("A") || grade.equals("B") || grade.equals("C")); // return !(grade.equals("D") || grade.equals("F")); } /** * This method prints a message if n is unlucky (isn't 7). It's bad style * to leave the true or false branches empty though... */ public void isUnlucky(int n) { if (n == 7) { // This space left intentionally blank } else { System.out.println(n + " is unlucky!"); } } /** * Here we asked a different question so we didn't have a blank. */ public void betterIsUnlucky(int n) { if (n != 7) { System.out.println(n + " is unlucky!"); } } }