-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusiness.java
More file actions
76 lines (69 loc) · 1.77 KB
/
Copy pathBusiness.java
File metadata and controls
76 lines (69 loc) · 1.77 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Write a description of class Business here.
*
* @author Tracy
* @version 2
*/
public class Business {
public String id;
public String value;
public int[] stars = new int[6];
public int getNOfRatings(){
return stars[0] + stars[1] + stars[2] + stars[3] + stars[4] + stars[5];
}
public int getTotalRating(){
return stars[1] + (stars[2] *2) + (stars[3]*3) + (stars[4]*4) + (stars[5]*5);
}
public double getAverageRating(){
if (getNOfRatings() == 0){
return 0;
}
return (double)getTotalRating()/(double)getNOfRatings();
}
public int getMedianRating(){
int n = getNOfRatings();
int median =0;
int sum =0;
if ( n == 0){
return 0;
}
if (n % 2 == 0){
int middleLeft = (n/2);
int middleRight = (n/2) +1;
for (int x =0; x < 6; x++){
sum += stars[x];
if (sum >= middleLeft){
if (sum >= middleRight){
median =x;
} else {
median = x=1;
}
break;
}
}
} else {
int middle = (n/2) +1;
for (int x =0; x < 6; x++){
sum += stars[x];
if (sum >= middle){
median =x;
break;
}
}
}
return median;
}
public int getModeRating(){
int n = getNOfRatings();
if ( n == 0){
return 0;
}
int mode =0;
for (int x =0; x < 6; x++){
if (stars[x] > stars[mode]){
mode =x;
}
}
return mode;
}
}