Problem Statement
You are required to read data from a stream, process the data, and write data to another stream.
Given class UpdateUtils:
class UpdateUtils {
/**
* Creates a new UpdateUtils object corresponding to the given data seed.
*
* @param dataSeed is an integer between 0 and 10^9, inclusive.
*/
public UpdateUtils(int dataSeed);
/**
* Gets the number of objects stored in InputStream
* returned by getInputStream().
*
* @return An integer between 1 and 100, inclusive.
*/
public int getObjectsCount();
/**
* Gets an InputStream to read, containing getObjectsCount()
* Data objects.
*
* @return InputStream ready to be read.
*/
public InputStream getInputStream();
/**
* Gets an OutputStream to write data to.
*
* @return OutputStream to save Data objects.
*/
public OutputStream getOutputStream();
/**
* Calculates a checksum for objects written to the OutputStream.
*
* @return Checksum for all saved objects.
*/
public int getObjectsChecksum();
}
Given class Data:
Represents data storage.
class Data implements Serializable {
/**
* Returns a string that represents the data stored in this object.
*
* @return String containing between 1 and 1000 characters, inclusive.
* Each character is one of 'a'-'z', '0'-'9', '{', '}'.
*/
public String getData();
/**
* Changes the data stored in this object.
*
* @param data is a string containing the new data to store.
*/
public void setData (String data);
}
DEFINITION
You must define public class UpdateObjects with public method int getObjectsChecksum(int dataSeed).
Create DataUtils dUtils using DataUtils(int). Get the input and output streams from dUtils. Read all data objects from the input stream.
Update each data object:
- Find all occurrences of "{beginnumber}number{endnumber}" in the data string (where number is a non-empty sequence of digits '0'-'9').
- Replace each such occurrence by "[beginnumber]number[endnumber]", where "number" must remain unchanged.
For example, "{beginnumber}12345{endnumber}" must be replaced by "[beginnumber]12345[endnumber]".
Write all updated objects to the output stream in the same order as you read them. After all the objects are written, return the check sum for all saved objects.
Definition
- Class:
- UpdateObjects
- Method:
- getObjectsChecksum
- Parameters:
- int
- Returns:
- int
- Method signature:
- int getObjectsChecksum(int dataSeed)
- (be sure your method is public)
Constraints
- dataSeed will be between 0 and 1,000,000,000, inclusive.
Examples
1
Returns: 366528390
There are two objects in the input stream. The data in the first object is "7{endnumber}" and the data in the second one is "{{endnumber}", so none of them needs to be changed.2
Returns: 1659649679
There are three objects in this input stream with the following data: "{endnumber}", "{beginnumber}", "hs4eqh59s1". Again, none of them needs to be changed.3
Returns: -914827982
There are 4 objects in this input stream and they contain the following data: "nf{beginnumber}sbg036ycav{beginnumber}4sttfur9ths0", "d4xtxn7tzepa5a2{{pvaesk{endnumber}}afdns5z{endnumber}", "ewxwx{endnumber}l9ge5hxx62{beginnumber}43d{endnumber}", "{beginnumber}454925477{endnumber}{beginnumber}bmjg". The first three can be left unchanged and in the last object you need to replace "{beginnumber}454925477{endnumber}" with "[beginnumber]454925477[endnumber]".4
Returns: -1053002881
There are 20 objects in the input stream.