-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDA.java
More file actions
57 lines (51 loc) · 1.56 KB
/
PDA.java
File metadata and controls
57 lines (51 loc) · 1.56 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
import java.util.*;
public class PDA
{
private Scanner scanner = new Scanner(System.in);
private int age;
private int LOWER_BOUND = 14;
private boolean shouldContinue = true;
/**
* Constructor for objects of class PDA
*/
public PDA()
{
// We don't need to do anything in the constructor for
// our program.
}
/**
* This is the main event loop for our PDA program
*/
public void runEventLoop() {
while (shouldContinue) {
System.out.println("How old are you? Type 0 to quit.");
try {
age = scanner.nextInt();
if (age == 0) {
shouldContinue = false;
System.out.println("Closing...");
} else if (age < LOWER_BOUND) {
System.out.println(age + " is too young!");
} else {
System.out.println("Your acceptable dating range is " + getYounger(age) + " to " + getOlder(age) + " years old.");
}
} catch (InputMismatchException error) {
System.out.println("Please enter an integer!");
scanner.next();
}
}
}
public int getYounger(int age) {
return (int)Math.ceil(0.5 * age + 7);
}
public int getOlder(int age) {
return 2 * (age - 7);
}
/**
* The main method instantiates and runs the program
*/
public static void main (String[] args) {
PDA pda = new PDA();
pda.runEventLoop();
}
}