public class MultiwaySplit
1: import java.util.Scanner;
3: /**
4: * This program shows multiple ways of writing code to do a three-way split.
5: * The code is supposed to print messages for 70, 40 and 10 year-olds, saying
6: * what age group (Senior, Adult, Child) they belong to.
7: *
8: * The BEST way is the LAST one. Some of the others are wrong, and even if
9: * they're not wrong, they are ERROR-PRONE -- easier to get wrong.
10: *
11: * Of course it's ALWAYS possible to make mistakes, but the last version has
12: * the smallest number of possible mistakes.
13: *
14: * IGNORE the for loops. We'll talk about them next week.
15: *
16: * @author Mark Young (A00000000)
17: */
18: public class MultiwaySplit {
20: public static void main(String[] args) {
21: // create scanner
22: Scanner kbd = new Scanner(System.in);
24: // introduce yourself
25: System.out.print("\n"
26: + "This program has five different versions of a three-way "
27: + "split. For each of\nthree ages it says whether the person "
28: + "is an adult, child, or senior.\n\n"
29: + "One version of the code is actually wrong.\n\n"
30: + "The other versions are all correct, but three of them are "
31: + "more likely to have\nmistakes in them "
32: + "because they have more logical tests.\n\n"
33: + "See the source code for explanations.\n\n"
34: + "...press enter to continue...");
35: kbd.nextLine();
36: System.out.println();
38: // first try
39: for (int age = 70; age > 0; age -= 30) {
40: System.out.print("Your age is " + age + ". You are a(n) ");
42: // First try.
43: // Correct, but ERROR-PRONE
44: if (age < 18) {
45: System.out.print("Child");
46: }
47: if (age >= 18 && age < 65) {
48: System.out.print("Adult");
49: }
50: if (age >= 65) {
51: System.out.print("Senior");
52: }
53: System.out.println(".");
54:
55: // Possible mistakes (4)
56: // write age <= 18 instead of age < 18 ==> 18 --> ChildAdult
57: // write age > 18 instead of age >= 18 ==> 18 --> (NOTHING!)
58: // write age <= 65 instead of age < 65 ==> 65 --> AdultSenior
59: // write age > 65 instead of age >= 65 ==> 65 --> (NOTHING!)
60: }
61: System.out.print("\n...press enter to continue...");
62: kbd.nextLine();
63: System.out.println();
65: // second try
66: for (int age = 70; age > 0; age -= 30) {
67: System.out.print("Your age is " + age + ". You are a(n) ");
69: // Second try.
70: // WRONG!
71: if (age < 18) {
72: System.out.print("Child");
73: }
74: if (age >= 18 && age < 65) {
75: System.out.print("Adult");
76: } else {
77: System.out.print("Senior");
78: }
79: System.out.println(".");
81: // Actual error ==> 10 --> ChildSenior
82: }
83: System.out.print("\n...press enter to continue...");
84: kbd.nextLine();
85: System.out.println();
87: // third try
88: for (int age = 70; age > 0; age -= 30) {
89: System.out.print("Your age is " + age + ". You are a(n) ");
91: // Third try
92: // Correct, but ERROR-PRONE
93: if (age < 18) {
94: System.out.print("Child");
95: } else {
96: if (age >= 18 && age < 65) {
97: System.out.print("Adult");
98: } else {
99: System.out.print("Senior");
100: }
101: }
102: System.out.println(".");
103:
104: // Possible mistakes (3)
105: // write age <= 18 instead of age < 18 ==> 18 --> Child
106: // write age > 18 instead of age >= 18 ==> 18 --> Senior
107: // write age <= 65 instead of age < 65 ==> 65 --> Adult
108: }
109: System.out.print("\n...press enter to continue...");
110: kbd.nextLine();
111: System.out.println();
113: // fourth try
114: for (int age = 70; age > 0; age -= 30) {
115: System.out.print("Your age is " + age + ". You are a(n) ");
117: // Fourth try
118: // Correct, but ERROR-PRONE
119: if (age < 18) {
120: System.out.print("Child");
121: } else if (age >= 18 && age < 65) {
122: System.out.print("Adult");
123: } else if (age >= 65) {
124: System.out.print("Senior");
125: }
126: System.out.println(".");
127:
128: // Possible mistakes (4)
129: // write age <= 18 instead of age < 18 ==> 18 --> Child
130: // write age > 18 instead of age >= 18 ==> 18 --> (NOTHING!)
131: // write age <= 65 instead of age < 65 ==> 65 --> Adult
132: // write age > 65 instead of age >= 65 ==> 65 --> (NOTHING!)
133: }
134: System.out.print("\n...press enter to continue...");
135: kbd.nextLine();
136: System.out.println();
138: // BEST version
139: for (int age = 70; age > 0; age -= 30) {
140: System.out.print("Your age is " + age + ". You are a(n) ");
142: // Fifth try
143: // Correct, and LEAST error-prone
144: if (age < 18) {
145: System.out.print("Child");
146: } else if (age < 65) {
147: System.out.print("Adult");
148: } else {
149: System.out.print("Senior");
150: }
151: System.out.println(".");
152:
153: // Possible mistakes (2)
154: // write age <= 18 instead of age < 18 ==> 18 --> Child
155: // write age <= 65 instead of age < 65 ==> 65 --> Adult
156: }
157: System.out.print("\n...press enter to continue...");
158: kbd.nextLine();
159: System.out.println();
160: }
162: }