Solutions to the 'Making Decisions' Exercises

  1. The program will print unknown transaction type! no matter what input it is given. This is because == does not compare the individual characters in each string, but rather checks whether the two are the 'same' string.

  2. 1  import java.io.*;
    2  
    3  class Ex42 {
    4  
    5    public static void main(String args[]) throws IOException {
    6  
    7      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    8      String line1, line2;
    9  
    10     System.out.print("Enter some text: ");
    11     line1 = in.readLine();
    12     System.out.print("Enter some more: ");
    13     line2 = in.readLine();
    14 
    15     if (line1.equals(line2)) {
    16       System.out.println("Snap!");
    17     }
    18   }
    19 }
    	
  3. 1  import java.io.*;
    2  
    3  class Ex43 {
    4  
    5    public static void main(String args[]) throws IOException {
    6  
    7      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    8      int num1, num2;
    9  
    10     System.out.print("Enter a number: ");
    11     num1 = Integer.parseInt(in.readLine());
    12     System.out.print("Enter another: ");
    13     num2 = Integer.parseInt(in.readLine());
    14 
    15     if (num1 == num2) {
    16       System.out.println("Equal!");
    17     }
    18     else if (num1 > num2) {
    19       System.out.println("Greater!");
    20     }
    21     else {
    22       System.out.println("Less!");
    23     }
    24   }
    25 }
    	
  4. For x = 27 and y = 42, the code will print less than. For x = 12 and y = 9 it will print close, but greater than.

  5. switch (i) {
     case 1:
     case 2:
     case 3:
     case 4:
     case 5:
      System.out.println("Group A");
      break;
     case 6:
     case 7:
     case 8:
     case 9:
     case 10:
      System.out.println("Group B");
      break;
     case 11:
     case 12:
     case 13:
     case 14:
     case 15:
      System.out.println("Group C");
      break;
     default:
      System.out.println("Group D");
    }
    	
  6. if ((c == 'a') || (c == 'A')) {
      System.out.println("Option A selected");
    }
    else if ((c == 'b') || (c == 'B')) {
      System.out.println("Option B selected");
    }
    else if ((c == 'c') || (c == 'C')) {
      System.out.println("Option C selected");
    }
    else {
      System.out.println("Unknown option selected");
    }
    	
  7. 1  import java.io.*;
    2  
    3  class Ex47 {
    4  
    5    public static void main(String[] args) throws IOException {
    6  
    7      System.out.print("Enter a character: ");
    8      char c = (char)System.in.read();
    9  
    10     if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
    11       System.out.println("Vowel");
    12     }
    13     else if (c == 'b' || c == 'c' || c == 'd' || c == 'f' || c == 'g' ||
    14              c == 'h' || c == 'j' || c == 'k' || c == 'l' || c == 'm' ||
    15              c == 'n' || c == 'p' || c == 'q' || c == 'r' || c == 's' ||
    16              c == 't' || c == 'v' || c == 'w' || c == 'x' || c == 'y' ||
    17              c == 'z') {
    18       System.out.println("Consonant");
    19     }
    20     else if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' ||
    21              c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
    22       System.out.println("Digit");
    23     }
    24     else {
    25       System.out.println("Other character");
    26     }
    27   }
    28 }
    	
  8. 1  import java.io.*;
    2  
    3  class Ex48 {
    4  
    5    public static void main(String[] args) throws IOException {
    6  
    7      System.out.print("Enter a character: ");
    8      char c = (char)System.in.read();
    9  
    10     switch (c) {
    11      case 'a':
    12      case 'e':
    13      case 'i':
    14      case 'o':
    15      case 'u':
    16       System.out.println("Vowel");
    17       break;
    18      case 'b':
    19      case 'c':
    20      case 'd':
    21      case 'f':
    22      case 'g':
    23      case 'h':
    24      case 'j':
    25      case 'k':
    26      case 'l':
    27      case 'm':
    28      case 'n':
    29      case 'p':
    30      case 'q':
    31      case 'r':
    32      case 's':
    33      case 't':
    34      case 'v':
    35      case 'w':
    36      case 'x':
    37      case 'y':
    28      case 'z':
    39       System.out.println("Consonant");
    40       break;
    41      case '0':
    42      case '1':
    43      case '2':
    44      case '3':
    45      case '4':
    46      case '5':
    47      case '6':
    48      case '7':
    49      case '8':
    50      case '9':
    51       System.out.println("Digit");
    52       break;
    53      default:
    54       System.out.println("Other character");
    55     }
    56   }
    57 }
    	

Scott Mitchell
Last modified: Tue Oct 13 20:13:16 BST 1998