Solutions to the Try It Out Exercises in Java For Dummies, 7th Edition
by Barry Burd

Chapter 5: Controlling Program Flow with Decision-Making Statements

In this chapter:

Make the Elevator program's output more natural

public class ElevatorFitter2 {

  public static void main(String args[]) {
    int weightOfAPerson = 150;
    int elevatorWeightLimit = 1400;
    int numberOfPeople = elevatorWeightLimit / weightOfAPerson;

    if (numberOfPeople >= 10) {
      System.out.println("You can fit all ten of the");
    } else {
      System.out.println("You can't fit all ten of the");
    }

    System.out.println("Brickenchicker dectuplets");
    System.out.println("on the elevator.");
  }
}

Add a third username/password combination to the list of acceptable logins

import javax.swing.JOptionPane;

public class Authenticator {

  public static void main(String args[]) {

    String username = JOptionPane.showInputDialog("Username:");
    String password = JOptionPane.showInputDialog("Password:");

    if (
        username != null && password != null &&
        (
          (username.equals("bburd") && password.equals("swordfish")) ||
          (username.equals("hritter") && password.equals("preakston")) ||
          (username.equals("ggow") && password.equals("ihaterr"))
        )
     )
    {
        JOptionPane.showMessageDialog(null, "You're in.");
    } else {
        JOptionPane.showMessageDialog(null, "You're suspicious.");
    }
  }
}

Modify the if statement's condition so that an all-uppercase entry for either username is acceptable

import javax.swing.JOptionPane;

public class Authenticator {

  public static void main(String args[]) {

    String username = JOptionPane.showInputDialog("Username:");
    String password = JOptionPane.showInputDialog("Password:");

    if (
        username != null && password != null &&
        (
          ((username.equals("bburd") || username.equals("BBURD"))
              && password.equals("swordfish")) ||
          ((username.equals("hritter") || username.equals("HRITTER"))
              && password.equals("preakston"))
        )
     )
    {
        JOptionPane.showMessageDialog(null, "You're in.");
    } else {
        JOptionPane.showMessageDialog(null, "You're suspicious.");
    }
  }
}

Change username != null && password != null to !(username == null || password == null)

The program still works because username != null && password != null is logically equivalent to !(username == null || password == null). This is so because of DeMorgan's Law of logic. It's a kind of distributive law for the !, &&, and || operators.

Change username != null && password != null to !(username == null && password == null)

The program crashes if the user clicks the Cancel button in one of the JOptionPane.showInputDialog calls. This happens because the condition !(username == null && password == null) isn't logically equivalent to the condition in the original program. Assume, for example, that the user types bburd in the Username: dialog box but clicks the Cancel button in the Password: dialog box. Then the value of username is bburd but the value of password is null (meaning that password doesn't have a valid String value). Then the condition !(username == null && password == null) is true because it's not the case that both username and password are null. So Java goes on to check if username.equals("bburd"), which it does. So then Java goes on to check if password.equals("swordfish"). But trying to call the equals method on the password variable causes the program to crash, because password isn't a valid String value. Instead, password is null.

If the user clicks Cancel for either the username or the password, the program replies with a Not enough information message

import javax.swing.JOptionPane;

public class Authenticator {

  public static void main(String args[]) {

    String username = JOptionPane.showInputDialog("Username:");
    String password = JOptionPane.showInputDialog("Password:");

    if (username == null || password == null) {
      JOptionPane.showMessageDialog(null, "Not enough information");
    } else if (
            (username.equals("bburd") && password.equals("swordfish")) ||
            (username.equals("hritter") && password.equals("preakston"))
       )
    {
        JOptionPane.showMessageDialog(null, "You're in.");
    } else {
        JOptionPane.showMessageDialog(null, "You're suspicious.");
    }
  }
}

Output the number of days in a month. Assume that February always has 28 days

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the name of a month: ");
    String month = keyboard.next();

    int numDays = 0;

    switch (month) {
    case "January":
    case "March":
    case "May":
    case "July":
    case "August":
    case "October":
    case "December":
      numDays = 31;
      break;
    case "February":
      numDays = 28;
      break;
    case "April":
    case "June":
    case "September":
    case "November":
      numDays = 30;
      break;
    }

    System.out.print(month + " has ");
    System.out.print(numDays);
    System.out.println(" days.");
  }
}

Have the user input yes or no in response to the question Is it a leap year?

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the name of a month: ");
    String month = keyboard.next();

    int numDays = 0;

    switch (month) {
    case "January":
    case "March":
    case "May":
    case "July":
    case "August":
    case "October":
    case "December":
      numDays = 31;
      break;
    case "February":
      numDays = 28;
      break;
    case "April":
    case "June":
    case "September":
    case "November":
      numDays = 30;
      break;
    }

    System.out.print("Is it a leap year (yes/no)? ");
    String isLeapYear = keyboard.next();

    if (month.equals("February") && isLeapYear.equals("yes")) {
      numDays = 29;
    }

    System.out.print(month + " has ");
    System.out.print(numDays);
    System.out.println(" days.");
  }
}