I need help figuring out how to write the following program. I don’t know where
ID: 3887221 • Letter: I
Question
I need help figuring out how to write the following program. I don’t know where to start ! Any online resources would also be helpful.
We need to write a simple Java program that will read a JSON file, parse it with the gson library.
We were provided with:
-JSON File
"title": "Midsummer Nightu0027s Dream",
"year": "1595",
"acts": [
"Act1",
"Act2",
"Act3",
"Act4",
"Act5"
],
"Act1": {
"Scene1": {
"paragraphs": [
{
"charID": "Theseus",
"text": "Now, fair Hippolyta, our nuptial hour [p]Draws on apace; four happy days bring in [p]Another moon: but, O, methinks, how slow [p]This old moon wanes! she lingers my desires, [p]Like to a step-dame or a dowager [p]Long withering out a young man revenue. ",
"paragraphNum": 2
},
-PlayJSONReader whose method readPlay reads the JSON file. This class will be provided.
-AccessorUtils.java This class will access elements from the play structure. This class will be provided along with documentation on the methods in the class.
We are to write the following class
public class PlayWriter
{
public static void writePlay(String thePlay,String outputFile) {
try {
JsonElement jelement = new JsonParser().parse(thePlay);
JsonObject thePlayJsonObject= (JsonObject)jelement;
…..
}
privatestatic void writePlayTitleAndYear(){}
private static void writeActs(){}
private static void writeAct(){}
private static void writeScenesOfAct(){}
private static void writeSceneOfAct(){}
private static void writeSceneParagraphs(){}
}
Explanation / Answer
PlayWriter.java
-------------------------------------------
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class PlayWriter {
private static String input = "MidsummerNightsDream.json";
private static String output = "MidsummerNightsDream.txt";
private static BufferedWriter writer = null;
/**
* The main method to start the reading and writing of the play
*/
public static void main(String []args){
String thePlay = PlayJSONReader.readPlay(input);
writePlay(thePlay, output);
}
/**
* responsible for handling the writing of the play by calling helper methods
* that use a "top-down" approach. Takes the the Play as a string, writes the output
* to a text file
*/
public static void writePlay(String thePlay, String outputFile) {
try {
writer = new BufferedWriter (new FileWriter(output));
} catch (IOException e) {
e.printStackTrace();
}
//takes the string of the play and parses it as a jsonObject
JsonElement jelement = new JsonParser().parse(thePlay);
JsonObject thePlayJsonObject = (JsonObject)jelement;
writePlayTitleAndYear(thePlayJsonObject);
writeActs(thePlayJsonObject);
}
/**
* Takes the jsonObject of the entire play and writes the title and year to the
* output file
*/
private static void writePlayTitleAndYear(JsonObject thePlayJson) {
try {
writer.write((AccessorUtils.getPlayTitle(thePlayJson)) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(AccessorUtils.getPlayYear(thePlayJson) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Takes the play as a jsonObject and extracts the acts as plain text
* Then it iterates through each act calls the helper
* method writeAct to write the text in the act
*/
private static void writeActs(JsonObject thePlayJson) {
ArrayList<String> acts = AccessorUtils.getActsOfPlayByName(thePlayJson);
for (int i = 0; i < acts.size(); i++) {
//passes the act as a jsonObject to the writeAct function, also passes the name of the act for writing
writeAct(AccessorUtils.getActOfPlay(thePlayJson, acts.get(i)), acts.get(i));
}
}
/**
* Takes the actNameString and prints it at the beginning of each act, then
* passes the actName JsonObject to the writeScenesOfAct helper function to
* write each scene of the act
*/
private static void writeAct(JsonObject actName, String actNameString) {
try {
writer.write((actNameString) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writeScenesOfAct(actName, actNameString);
}
/**
* Takes the actName as a JsonObject and iterates through each scene to print the header
* for each scene and passes the scene JsonObject to the writeSceneOfAct helper function
*/
private static void writeScenesOfAct(JsonObject actName, String actNameString) {
ArrayList<String> scenes = AccessorUtils.getScenesOfActByName(actName);
for (int i = 0; i < scenes.size(); i++) {
try {
writer.write(actNameString + ", " + scenes.get(i) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writeSceneOfAct(AccessorUtils.getSceneOfAct(actName, scenes.get(i)));
}
}
/**
* takes the JsonObject of the scene and iterates through each paragraph to call the
* writeSceneParagraphs helper function to write the text of each paragraph
*/
private static void writeSceneOfAct(JsonObject scene) {
JsonArray paragraphs = AccessorUtils.getSceneParagraphs(scene);
// System.out.println(paragraphs.size());
for (int i = 0; i < paragraphs.size(); i++) {
writeSceneParagraphs(AccessorUtils.getParagraphFromParagraphs(paragraphs, i));
}
}
/**
* Takes the paragraph as a JsonObject and writes the text from each paragraph to the output file
*/
private static void writeSceneParagraphs(JsonObject paragraph) {
try {
writer.write(AccessorUtils.getCharacterFromParagraph(paragraph) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(AccessorUtils.getTextFromParagraph(paragraph) + " ");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-------------------------------------------------------------------
PlayJSONReader.java
--------------------------------------
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class PlayJSONReader {
public static String readPlay (String inputJSONFileName) {
BufferedReader reader;
StringBuilder buffer = new StringBuilder();
String line;
String thePlayAsJSONString = null;
try {
// System.out.println ("Input file " + inputJSONFile);
reader = new BufferedReader(new FileReader(inputJSONFileName));
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + " ");
}
reader.close();
thePlayAsJSONString = buffer.toString();
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
}
return thePlayAsJSONString;
}
}
---------------------------------------------------------------------------
AccessorUtils.java
--------------------------------------
import java.util.ArrayList;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class AccessorUtils {
private static final String ACTS = "acts";
private static final String TITLE = "title";
private static final String YEAR = "year";
private static final String SCENE = "Scene";
private static final String PARAGRAPHS = "paragraphs";
private static final String CHARACTER = "charID";
private static final String SPEECH_TEXT = "text";
private static final String LOCATION = "location";
/**
* Returns the names of the acts in the play as a list of strings in
* ArrayList
*/
public static ArrayList <String> getActsOfPlayByName (JsonObject thePlay) {
JsonArray actsAsJsonObject = thePlay.getAsJsonArray(ACTS);
ArrayList <String> actNames = new ArrayList<String>();
for (int i=0; i<actsAsJsonObject.size(); i++) {
actNames.add(actsAsJsonObject.get(i).getAsString());
}
return actNames;
}
/**
* Returns the named act of the play as a JsonObject
*/
public static JsonObject getActOfPlay (JsonObject thePlay, String actName) {
return thePlay.get(actName).getAsJsonObject();
}
/**
* Returns the title of the play as a String
*/
public static String getPlayTitle (JsonObject thePlay) {
return thePlay.getAsJsonPrimitive(TITLE).getAsString();
}
/**
* Returns the year the play was written
*/
public static String getPlayYear (JsonObject thePlay) {
return thePlay.getAsJsonPrimitive(YEAR).getAsString();
}
public static ArrayList <String>getScenesOfActByName(JsonObject aPlayAct) {
int n = 1;
String sceneName = SCENE + n;
ArrayList<String> scenes = new ArrayList <String>();
while (aPlayAct.has(sceneName)) {
scenes.add(sceneName);
n++;
sceneName = SCENE + n;
}
return scenes;
}
public static JsonObject getSceneOfAct (JsonObject aPlayAct, String sceneName) {
return aPlayAct.get(sceneName).getAsJsonObject();
}
/**
* Returns the location description for the scene
*/
public static String getSceneLocation (JsonObject scene) {
return scene.get(LOCATION).getAsString();
}
public static JsonArray getSceneParagraphs (JsonObject scene ) {
return scene.getAsJsonArray(PARAGRAPHS);
}
/**
* Returns the paragraph object (character, their dialog, and a paragraph number)
* at index "pindex" from the array of scene paragraphs
*/
public static JsonObject getParagraphFromParagraphs (JsonArray paragraphs, int pIndex) {
return paragraphs.get(pIndex).getAsJsonObject();
}
/**
* Returns the name of the character from the paragraph object. This is the character
* that is speaking the dialog for this paragraph.
*/
public static String getCharacterFromParagraph (JsonObject paragraph) {
return paragraph.get(CHARACTER).getAsString() ;
}
/**
* Returns the dialog of the character from the paragraph.
*/
public static String getTextFromParagraph (JsonObject paragraph) {
return paragraph.get(SPEECH_TEXT).getAsString() ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.