-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameSurferEntry.java
More file actions
82 lines (74 loc) · 2.1 KB
/
Copy pathNameSurferEntry.java
File metadata and controls
82 lines (74 loc) · 2.1 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
77
78
79
80
81
/*
* File: NameSurferEntry.java
* --------------------------
* This class represents a single entry in the database. Each
* NameSurferEntry contains a name and a list giving the popularity
* of that name for each decade stretching back to 1900.
*/
//import acm.util.*;
import java.util.*;
public class NameSurferEntry implements NameSurferConstants {
/* Constructor: NameSurferEntry(line) */
/**
* Creates a new NameSurferEntry from a data line as it appears
* in the data file. Each line begins with the name, which is
* followed by integers giving the rank of that name for each
* decade.
*/
public NameSurferEntry(String line) {
// You fill this in //
//rank = {0,0,0,0,0,0,0,0,0,0,0,0,0};
name = "";
StringTokenizer st = new StringTokenizer(line," ");
name = st.nextToken();
int i = 0;
while(st.hasMoreTokens()){
String number = st.nextToken();
rank[i] = Integer.parseInt(number);
i++;
}
}
/* Method: getName() */
/**
* Returns the name associated with this entry.
*/
public String getName() {
// You need to turn this stub into a real implementation //
return name;
}
/* Method: getRank(decade) */
/**
* Returns the rank associated with an entry for a particular
* decade. The decade value is an integer indicating how many
* decades have passed since the first year in the database,
* which is given by the constant START_DECADE. If a name does
* not appear in a decade, the rank value is 0.
*/
public int getRank(int decade) {
// You need to turn this stub into a real implementation //
if(decade<=11 && decade>=0){
return rank[decade];
} else {
return 0;
}
}
/* Method: toString() */
/**
* Returns a string that makes it easy to see the value of a
* NameSurferEntry.
*/
public String toString() {
// You need to turn this stub into a real implementation //
String str = "";
str = str + name;
for(int i=0; i<rank.length; i++) {
str = str+" ";
if (i == 0) str = str+"[";
str = str + rank[i];
if (i == 11) str = str +"]";
}
return str;
}
private String name;
private int[] rank = {0,0,0,0,0,0,0,0,0,0,0,0};
}