Due in 3 hours public class Paragraph { private String words; public Paragraph()
ID: 3829729 • Letter: D
Question
Due in 3 hours
public class Paragraph {
private String words;
public Paragraph() {
this.words = "";
}
public void addWords(String moreWords) {
this.words += moreWords;
}
public String format(ParaStyle ps) {
if (ps == null)
return words + " ";
String firstLineIndent = "";
String indent = "";
int lineLength = ps.getLineLength();
String[] tokens;
String pageText = "";
String line;
for (int i = 0; i < ps.getLeftIndent(); i++)
indent += " ";
for (int i = 0; i < (ps.getLeftIndent() + ps.getFirstLineAdjustment()); i++)
firstLineIndent += " ";
tokens = words.split(" ");
if (tokens.length == 0)
return " ";
line = firstLineIndent + tokens[0];// assuming that line length will be
// always greater than
// firstLineIndent + tokens[0]
for (int i = 1; i < tokens.length; ++i) {
if ((line.length() + tokens[i].length() + 1) <= lineLength)
line += (" " + tokens[i]);
else {
pageText += (line + " ");
line = indent + tokens[i];
}
}
pageText += (line + " ");
return pageText;
}
}
//////////////////////////////////////////////////////////
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ParagraphDriver {
public static void main(String[] args) {
System.out.println("Welcome!");
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter style filename: ");
String styleName = stdIn.nextLine();
System.out.print("Enter document filename: ");
String documentName = stdIn.nextLine();
System.out.print("Enter output filename: ");
String outName = stdIn.nextLine();
// TODO
System.out.println("Good bye!");
stdIn.close();
}
public static Map readStyles(Scanner styleScanner) {
// TODO
}
public static void formatDocument(Scanner documentScanner, PrintWriter outputFileWriter,
Map styles) {
// TODO
}
}
/////////////////////////////////////////////////////////
public class ParaStyle {
//TODO
}
////////////////////////( The Files (Out1) )//////////////////////////////
Deep within the deserts of Jordan lies the ancient city of Petra.
Through a narrow gorge it emerges into view, revealing awe-inspiring
monuments cut into the surrounding cliffs. What is this astonishing
city? Who built it, and why?
Two thousand years ago, Petra stood at a crossroads of
the ancient Near East. Camel caravans passed through, loaded
with spices, textiles and incense from distant regions--and
through such commerce, the city flourished. Its people, the
Nabataeans, harnessed precious water, enabling the
population to soar to perhaps 20,000.
Re-discovering this astounding complex of cliff-carved faÁades as
Swiss explorer, Johann Ludwig Burckhardt, did exactly 200 years
ago must surely have been well worth writing home about. Now well
known to the world - in no small part due to its on-screen role
in 'Indiana Jones and the Last Crusade' - and in company with the
likes of Cambodia's Angkor Wat, Peru's Machu Picchu, and the
Great Wall of China on the list of 'New 7 Wonders of the World',
it is thrilling to imagine what this intrepid explorer must have
felt on first seeing Petra.
////////////////////////( The Files (Out2) )//////////////////////////////
Deep within the deserts of Jordan
lies the ancient city of Petra.
Through a narrow gorge it emerges
into view, revealing awe-inspiring
monuments cut into the surrounding
cliffs. What is this astonishing
city? Who built it, and why?
Two thousand years ago, Petra stood at a
crossroads of the ancient Near East. Camel
caravans passed through, loaded with spices,
textiles and incense from distant regions--and
through such commerce, the city flourished. Its
people, the Nabataeans, harnessed precious water,
enabling the population to soar to perhaps 20,000.
Re-discovering this astounding complex of cliff-carved
faÁades as Swiss explorer, Johann Ludwig
Burckhardt, did exactly 200 years ago must surely
have been well worth writing home about. Now well
known to the world - in no small part due to its
on-screen role in 'Indiana Jones and the Last
Crusade' - and in company with the likes of
Cambodia's Angkor Wat, Peru's Machu Picchu, and
the Great Wall of China on the list of 'New 7
Wonders of the World', it is thrilling to imagine
what this intrepid explorer must have felt on
first seeing Petra.
////////////////////////( The Files (petra.txt) )//////////////////////////////
.P para1
Deep within the deserts of Jordan lies the ancient city of Petra.
Through a narrow gorge it emerges into view, revealing awe-inspiring
monuments cut into the surrounding cliffs. What is this astonishing city?
Who built it, and why?
.P para2
Two thousand years ago, Petra stood at a crossroads of the ancient Near East.
Camel caravans passed through, loaded with spices, textiles and incense from distant
regions--and through such commerce, the city flourished. Its people, the Nabataeans,
harnessed precious water, enabling the population to soar to perhaps 20,000.
.P para3
Re-discovering this astounding complex of cliff-carved façades as Swiss explorer,
Johann Ludwig Burckhardt, did exactly 200 years ago must surely have been well worth
writing home about. Now well known to the world - in no small part due to its on-screen
role in 'Indiana Jones and the Last Crusade' - and in company with the likes of Cambodia's
Angkor Wat, Peru's Machu Picchu, and the Great Wall of China on the list of 'New 7 Wonders
of the World', it is thrilling to imagine what this intrepid explorer must have felt on
first seeing Petra.
////////////////////////( The Files (style1.txt) )//////////////////////////////
para1 0 70 0
para2 5 65 5
para3 5 70 -3
////////////////////////( The Files (style2.txt) )//////////////////////////////
para1 5 40 0
para2 0 50 5
para3 10 60 -4
Explanation / Answer
public class ParaStyle {
//class variables
String name;
int leftIndent;
int lineLength;
int firstLineAdjustment;
//constructor
public ParaStyle(String name, int leftIndent, int lineLength, int firstLineAdjustment) {
this.name = name;
this.leftIndent = leftIndent;
this.lineLength = lineLength;
this.firstLineAdjustment = firstLineAdjustment;
}
//getter setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLeftIndent() {
return leftIndent;
}
public void setLeftIndent(int leftIndent) {
this.leftIndent = leftIndent;
}
public int getLineLength() {
return lineLength;
}
public void setLineLength(int lineLength) {
this.lineLength = lineLength;
}
public int getFirstLineAdjustment() {
return firstLineAdjustment;
}
public void setFirstLineAdjustment(int firstLineAdjustment) {
this.firstLineAdjustment = firstLineAdjustment;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class ParagraphDriver {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
System.out.println("Welcome!");
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter style filename: ");
String styleName = stdIn.nextLine();
System.out.print("Enter document filename: ");
String documentName = stdIn.nextLine();
System.out.print("Enter output filename: ");
String outName = stdIn.nextLine();
// TODO
Map<Integer, String> map = new HashMap<Integer, String>();
File file = new File(styleName);
Scanner sc = new Scanner(file);
map = readStyles(sc, styleName);
// opening document to read words
File file1 = new File(documentName);
Scanner sc1 = new Scanner(file);
PrintWriter writer = new PrintWriter(outName, "UTF-8");
formatDocument(sc1, writer, map);
System.out.println("Good bye!");
stdIn.close();
}
public static Map readStyles(Scanner styleScanner, String fileName) {
Map<Integer, String> map = new HashMap<Integer, String>();
int index = 0;
while (styleScanner.hasNextLine()) {
String line = styleScanner.nextLine();
String[] data = line.split(" ");
map.put(index, data[0]);
index++;
map.put(index, data[1]);
index++;
map.put(index, data[2]);
index++;
map.put(index, data[3]);
index++;
}
return map;
}
public static void formatDocument(Scanner documentScanner, PrintWriter outputFileWriter,
Map styles) {
//creating Paragraph object
Paragraph para = new Paragraph();
while (documentScanner.hasNextLine()) {
String line = documentScanner.nextLine();
para.addWords(line);
}
//parsing map and creating parastyle objects
String[] data = (String[]) styles.values().toArray();
ParaStyle obj1 = new ParaStyle(data[0], Integer.parseInt(data[1]), Integer.parseInt(data[2]), Integer.parseInt(data[3]));
ParaStyle obj2 = new ParaStyle(data[4], Integer.parseInt(data[5]), Integer.parseInt(data[6]), Integer.parseInt(data[7]));
//calling method to get better paragraph
outputFileWriter.println(para.format(obj1) + " " + para.format(obj2));
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Paragraph {
private String words;
public Paragraph() {
this.words = "";
}
public void addWords(String moreWords) {
this.words += moreWords;
}
public String format(ParaStyle ps) {
if (ps == null) {
return words + " ";
}
String firstLineIndent = "";
String indent = "";
int lineLength = ps.getLineLength();
String[] tokens;
String pageText = "";
String line;
for (int i = 0; i < ps.getLeftIndent(); i++) {
indent += " ";
}
for (int i = 0; i < (ps.getLeftIndent() + ps.getFirstLineAdjustment()); i++) {
firstLineIndent += " ";
}
tokens = words.split(" ");
if (tokens.length == 0) {
return " ";
}
line = firstLineIndent + tokens[0];// assuming that line length will be
// always greater than
// firstLineIndent + tokens[0]
for (int i = 1; i < tokens.length; ++i) {
if ((line.length() + tokens[i].length() + 1) <= lineLength) {
line += (" " + tokens[i]);
} else {
pageText += (line + " ");
line = indent + tokens[i];
}
}
pageText += (line + " ");
return pageText;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.