George Washington | Va | Federalist | 1789-1797 |
John Adams | Mass | Federalist | 1797-1801 |
Thomas Jefferson | Va | Democratic-Republican | 1801-1809 |
James Madison | Va | Democratic-Republican | 1809-1817 |
James Monroe | Va | Democratic-Republican | 1817-1825 |
John Quincy Adams | Mass | National Republican | 1825-1829 |
Andrew Jackson | S.C. | Democratic | 1829-1837 |
Martin Van Buren | N.Y. | Democratic | 1837-1841 |
William Henry Harrison | Va | Whig | 1841 |
John Tyler | Va | Whig | 1841-1845 |
James K. Polk | N.C. | Democratic | 1845-1849 |
Zachary Taylor | Va | Whig | 1849-1850 |
Millard Fillmore | N.Y. | Whig | 1850-1853 |
Franklin Pierce | N.H. | Democratic | 1853-1857 |
James Buchanan | Pa | Democratic | 1857-1861 |
Abraham Lincoln | Ky | Republican | 1861-1865 |
To be able to retrieve the value of a particular field from within such a string, it is necessary to be able to find the beginning and end of the substring that describes that value. One way to do that is to terminate each field with some particular character.1 One could reasonably refer to a string of this form as a Field-Terminated String, or FTS.
To illustrate, consider the table to the right, which presents data about the first sixteen presidents of the United States. Specifically, each row gives a president's name, his home state, his political party, and the span of years during which he was president.
Letting the semicolon character play the role of the terminator, translating the data in the table into a sequence of FTS's yields the following:
George Washington;Va;Federalist;1789-1797; John Adams;Mass;Federalist;1797-1801; Thomas Jefferson;Va;Democratic-Republican;1801-1809; James Madison;Va;Democratic-Republican;1809-1817; James Monroe;Va;Democratic-Republican;1817-1825; John Quincy Adams;Mass;National Republican;1825-1829; Andrew Jackson;S.C.;Democratic;1829-1837; Martin Van Buren;N.Y.;Democratic;1837-1841; William Henry Harrison;Va;Whig;1841; John Tyler;Va;Whig;1841-1845; James K. Polk;N.C.;Democratic;1845-1849; Zachary Taylor;Va;Whig;1849-1850; Millard Fillmore;N.Y.;Whig;1850-1853; Franklin Pierce;N.H.;Democratic;1853-1857; James Buchanan;Pa;Democratic;1857-1861; Abraham Lincoln;Ky;Republican;1861-1865; |
Between the sample program execution shown below (in which the user has provided "Scranton;1888;Royals;" as the input string) and the comments accompanying the methods in the Java source code, it should be clear what each one is intended to do.
$ java FTSDriver Scranton;1888;Royals; FTS Driver ... fts is "Scranton;1888;Royals;" and has 3 fields Testing getField() --------------------- getField(fts,0) yields "Scranton" getField(fts,1) yields "1888" getField(fts,2) yields "Royals" Testing setField() --------------------- setField(fts,0,"FIELD") yields "FIELD;1888;Royals;" and has 3 fields setField(fts,1,"FIELD") yields "Scranton;FIELD;Royals;" and has 3 fields setField(fts,2,"FIELD") yields "Scranton;1888;FIELD;" and has 3 fields Testing removeField() ------------------------ removeField(fts,0) yields "1888;Royals;" and has 2 fields removeField(fts,1) yields "Scranton;Royals;" and has 2 fields removeField(fts,2) yields "Scranton;1888;" and has 2 fields Testing (2-arg version of) insertField() ------------------------------------------- insertField(fts,0) yields ";Scranton;1888;Royals;" and has 4 fields insertField(fts,1) yields "Scranton;;1888;Royals;" and has 4 fields insertField(fts,2) yields "Scranton;1888;;Royals;" and has 4 fields insertField(fts,3) yields "Scranton;1888;Royals;;" and has 4 fields Testing (3-arg version of) insertField() ------------------------------------------- insertField(fts,0,"FIELD") yields "FIELD;Scranton;1888;Royals;" and has 4 fields insertField(fts,1,"FIELD") yields "Scranton;FIELD;1888;Royals;" and has 4 fields insertField(fts,2,"FIELD") yields "Scranton;1888;FIELD;Royals;" and has 4 fields insertField(fts,3,"FIELD") yields "Scranton;1888;Royals;FIELD;" and has 4 fields Done!!! |
As usual, the student should submit the relevant Java source code (meaning that in the file FTSDriver.java) to the appropriate dropbox.