A snail in a well can climbs 3 meters up in the day, and slide 2 meters down at the night. Given the height of the well h, how many days does it take for the snail to climb out of the well?

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
public class Snail {
private int distance; // distance to the top
private static int height; // wall height
private int day;

public Snail(){
distance = 0;
height = 0;
day = 0;
}

public void setHeight(int high){
height = high;
}

public void setDistance(int dist){
distance = dist;
}

public void day(){
if (distance <= 0){
System.out.println("This snail has climbed out of wall");
return;
}
distance -= 3;
}

public void night(){
if (distance > 0 && distance < height){
day += 1;
distance += 2;
}
}

public int getDay(){
return day;
}

public int getDistance(){
return distance;
}

public static void main(String[] args){
Snail snail = new Snail();
// the well is 10 meters tall
snail.setHeight(10);
snail.setDistance(10);

while (snail.getDistance() > 0){
snail.day();
snail.night();
}
System.out.println(snail.getDay());
}
}