-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hair.java
66 lines (58 loc) · 1.57 KB
/
Hair.java
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
58
59
60
61
62
63
64
65
66
package Hair;
import java.util.*;
public class Hair{
static Random r = new Random();
public static double meanLife = 4.5;/**mean life of a hair*/
public static double sigmaLife = 0.4;/**stdev of life of a hair*/
public static double rate = 12; /**growth rate in cm per year*/
double age, length, lifeExp;
boolean alive;
/**creates a new hair*/
public Hair() {
age = 0;
length = 0;
lifeExp = Math.max(r.nextGaussian()*sigmaLife + meanLife, 0);
alive = true;
}
/**gets age
* @return double, age*/
public double getAge() {
return age;
}
/**gets length of hair
* @return double, length*/
public double getLength() {
return length;
}
/**gets expected life of hair
* @return double, lifeExp*/
public double getLifeExp() {
return lifeExp;
}
/**cuts a hair to a certain length
* @param goalLength double, the goal length, must be smaller than length
* @return void*/
public void cut(double goalLength) {
if (goalLength >= 0 && goalLength <= length) {
length = goalLength;
}
}
/**grows a hair after a certain time
* @param t double, time elapsed
* @return void*/
public void grow(double t) {
if (t >= 0) {
double growTime = Math.min(t, lifeExp - age);/**actual time of growth!*/
length = length + rate*growTime;
age = Math.min(lifeExp, age + t);
if (age == lifeExp) {
alive = false;/**hair dies!*/
}
}
}
/**checks if a hair is alive
* @return boolean, alive */
public boolean isAlive() {
return alive;
}
}