|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectPassport
public class Passport
The Passport
class is a simplified representation of a real passport.
It contains information about which countries the owner of the passport has visited, and
for how long.
This class should have THREE instance variables (you must not add any other instance variables):
String
).
int
).
String
).String
has zero or more items stored
in this form (there is one space after the comma): countryName, duration
where countryName is the name of the country visited by the passport owner, and duration is an integer (starting from 0) that indicates how many days the passport owner spent in that country.
Each item is separated by a semi-colon followed by one space. There should not be a
semi-colon and space at the beginning or end of the String
. Here is an example:
"Canada, 175; Germany, 1; Russia, 25; Germany, 10"
If the passport owner has not visited any countries, then the String
is empty ("").
Constructor Summary | |
---|---|
Passport(String n,
int s)
Create a new passport with name n, serial number s, and no countries visited. |
Method Summary | |
---|---|
void |
addVisited(String country,
int duration)
Add a new entry to the countries visited section of this passport, using the information provided by the user (in the 2 arguments); The new information should be appended to the end of the current variable; Make sure to preserve the format of this variable, as described above; You can assume that the user will not put in negative integers as the duration. Also, there must be NO consecutive entries with the same country; if the previous entry has the same country, then the two entries must be merged and their durations added together to form one entry (e.g., "Canada, 10; Canada, 10"
should be "Canada, 20" ). |
void |
clear()
Clear the history of the countries visited for this passport (i.e., make it empty). |
String |
firstCountryVisited()
Return the first country that the owner of this passport has visited, or empty string ("") if no countries have been visited. |
String |
getCountriesVisited()
Return the countries visited section of this passport. |
String |
getName()
Return the name of this passport's owner. |
int |
getSerialNumber()
Return the serial number of this passport. |
String |
lastCountryVisited()
Return the last country that the owner of this passport has visited, or empty string ("") if no countries have been visited. |
String |
longestVisitedCountry()
Return the country that the owner of this passport has visited for the longest duration, or empty string ("") if no countries have been visited; If there is a tie between two or more countries, return the most recent of these countries (i.e., the first one from the right of the String variable). |
int |
numEntries()
Return the total number of entries in this passport's countries visited section; You must count every entry, including multiple visits to the same country. |
int |
numVisits(String country)
Return the number of times this passport's owner has visited the country specified by the user. |
String |
toString()
Return the information about this passport as a String in
the following format (3 lines; there must be one space after the first ":" on each line):Name: n Serial Number: s Countries Visited: cv where n is the name of this passport's owner, s is the serial number of this passport, and cv is this passport's record of countries visited (along with the durations); If no countries have been visited, then the value of cv must be the String "none". |
int |
totalDuration(String country)
Return the total number of days that the owner of this passport spent in the specified country. |
String |
whichCountryVisited(int index)
Return the country that the owner of this passport visited at the specified index -- which starts at 1 (not zero!); If the user specified an invalid index (i.e., less than 1 or greater than the number of entries), then return the String: "Invalid index!" (it's important that the String be EXACTLY like this, with the capital I at the beginning and ! at the end). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Passport(String n, int s)
Method Detail |
---|
public String getName()
public int getSerialNumber()
public String getCountriesVisited()
public void addVisited(String country, int duration)
"Canada, 10; Canada, 10"
should be "Canada, 20"
).
public void clear()
public int numEntries()
public int numVisits(String country)
public String firstCountryVisited()
public String lastCountryVisited()
public String whichCountryVisited(int index)
public String longestVisitedCountry()
public int totalDuration(String country)
public String toString()
String
in
the following format (3 lines; there must be one space after the first ":" on each line):
toString
in class Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |