-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSonAge.java
More file actions
57 lines (51 loc) · 1.46 KB
/
FindSonAge.java
File metadata and controls
57 lines (51 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @author marios yiannakou
*
* Purpose of the program is to find when the fathers age will be
* twice the sons age by incrementing both the ages and then
* dividing by two and comparing the dads age to the sons age.
*
* Program accepts EXACTLY two arguments.
* The first argument is the sons current age and
* the second argument is the dads current age.
*
* Exit Codes:
* -1 - Wrong number of arguments
* -2 - Erroneous input
*/
public class FindSonAge {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Need exactly two arguments, son's age and father's age respectively.");
System.exit(-1);
}
int sonAge = 0;
int dadAge = 0;
try {
sonAge = Integer.parseInt(args[0]);
dadAge = Integer.parseInt(args[1]);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.exit(-2);
}
if (sonAge <= 0 || dadAge <= 0) {
System.out.println("Age must be positive.");
System.exit(-2);
} else if (sonAge >= dadAge) {
System.out.println("Sons age must be less than dads age.");
System.exit(-2);
}
int iterations = 0;
while (true) {
sonAge ++;
dadAge ++;
iterations ++;
if (dadAge == 2 * sonAge) {
System.out.println("Son age: " + sonAge);
System.out.println("Dad age: " + dadAge);
System.out.println("Years Passed: " + iterations);
break;
}
}
}
}