1. Engineering
  2. Computer Science
  3. write a constructor and a helper method for the class...

Question: write a constructor and a helper method for the class...

Question details

Write a constructor and a helper method for the class as shown below:

ParseJSON class: it contains a field called ArrayList list

ArrayList list : This ArrayList of Strings field contains objects from the JSON data passed in via the constructors of this class.

// extra information

Constructors

1.

public ParseJSON (String json) 

This constructor takes a String that contains all the objects of a JSON data and calls appropriate functions to initialize the field list.

2.

public ParseJSON (InputStream stream) 

This constructor takes an InputStream object, parses the JSON data from the stream by calling method streamToString, and stores all the parsed objects in the field list. We have designed a helper function streamToString() which reads everything from the stream and forms a String as its return value. By utilizing this helper function within this constructor, you can make use of the first constructor to initialize the list (use "this" keyword to invoke another constructor).

Instance Methods

protected static String streamToString (InputStream stream)

This method is the helper method for one of the constructors. It should use stream to read in the JSON data, put the data into a String, and return the String.

Hints: 1. write the helper method first then the constructor

2. method streamToString takes in Inputstream and return String, constructor public ParseJSON (String json) takes in string and stores all the parsed objects in the field list. So the content of the constructor public ParseJSON (InputStream stream) is about invoking the method and the first constructor.

3. ignore the word Json, just treat it as a name. Don't use Json Object Library.

Thanks!

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution