// Name: Whitaker Brand // Course: CSE142 // Section: AB // TA: Miya // // This program demonstrates a couple good usages of if/else, and returning // values from methods. import java.util.*; public class GoodIfReturn { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("give me an int-> "); int n = console.nextInt(); n = update(n); System.out.println("updated n = " + n); System.out.println(); n = update2(n); System.out.println("updated n = " + n); } // This method updates the value of n. It is supposed to either add one to // n (for negatives), reset n to 1000 (for 0), or subtract 1 from n (for // positives). Because it uses the wrong if/else structure, it potentially // performs multiple updates public static int update(int n) { if (n < 0) { n++; } else if (n == 0) { n = 1000; } else { // if (n > 0) n--; } return n; } // This method performs a "special" update that either adds 42 to n (for // negatives) or subtracts 13 from n (for non-negatives). public static int update2(int n) { System.out.println("special update in progress"); int change = 0; if (n < 0) { change = 42; } else { // if (n >= 0) change = -13; } n = n + change; System.out.println("updated by " + change); return n; } }