Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

program will read two files: a style file and a document file. The style file will be small and will contain a series of lines that define different styles for paragraphs. The document file will contain the text of a series of paragraphs. The style file gives parameters for formatting each of the paragraphs 2 Requirements There are three main classes. The ParaStyle class just holds values for paragraph styles. Each style has a name and three integer values. "lineLength" specifies the total number of characters in each line, including any spaces for indentation and between words left Indent" specifies how many blank spaces appear on the left of the line before the printing characters start. "firstLineAdjustment" specifies how the first line is different from the ent in the number of blank spaces at the start of the line for each paragraph. In theory, there should be constraints on these three values. firstLineAdjustment can be negative and when you add it and leftIndent, the value should always be non-negative. leftIndent and lineLength must be nonnegative. lineLength for practical reasons should never be less than about 30 characters and leftIndent should never be higher than about 15. We will ignore these issues. In other words, you don't have to check that ParaStyle objects follow these rules. We're just telling you so that you know that a production system would probably have to do something about them The Paragraph class holds a String Gust one String, not an array of Strings It is designed so that the text of the paragraph can be read from a series of lines in a file, with the program adding each line to the Paragraph object as the line is read in. Once the Paragraph is complete, it will contain all of the text of the paragraph as a single line, with at least one space between each word. Then, it can be formatted by calling the format method. The format method requires a ParaStyle object and it returns a String with the paragraph broken into lines in the format specified by the ParaStyle The ParagraphDriver class reads a set of ParaStyle specifications from one file and a document that contains a series of Paragraphs from another file. It writes the paragraphs formatted correctly to an output file

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;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote