CMPS 134 Fall 2023
Prog. Assg. #4: Field-Terminated Strings
Due: 11:59pm, Friday, October 20

Background

George Washington VaFederalist1789-1797
John Adams MassFederalist1797-1801
Thomas Jefferson VaDemocratic-Republican1801-1809
James Madison VaDemocratic-Republican1809-1817
James Monroe VaDemocratic-Republican1817-1825
John Quincy Adams MassNational Republican1825-1829
Andrew Jackson S.C.Democratic1829-1837
Martin Van Buren N.Y.Democratic1837-1841
William Henry Harrison VaWhig1841
John TylerVaWhig1841-1845
James K. Polk N.C.Democratic1845-1849
Zachary TaylorVaWhig1849-1850
Millard Fillmore N.Y.Whig1850-1853
Franklin Pierce N.H.Democratic1853-1857
James BuchananPaDemocratic1857-1861
Abraham LincolnKyRepublican1861-1865
Character strings are employed for representing many different kinds of data. Often, a single string is used to represent several pieces of data, called fields, that are closely related to each other. The value of each field is described by a substring of the string as a whole.

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:

Field-Terminated Strings
describing presidents
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;


Requirements

Provided is an incomplete Java application, FTSDriver, the last five methods of which are left to the student to finish. The program expects as input a single FTS. When run from the command line environment (as illustrated in the sample program execution below), this input is provided via command line argument. When run from the jGrasp environment, this input is provided via a a run argument. Instructions for supplying run arguments to Java applications can be found here.

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.

Sample Program Execution
$ 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.

Footnote

[1] A terminator need not be a single character; a multi-character string also can play that role.