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

The mission of this assignment is to realize a simple paragraph justifier. As in

ID: 663679 • Letter: T

Question

The mission of this assignment is to realize a simple paragraph justifier. As in the earlier assignment, your program keeps reading lines of text image using getline(). After reading a paragraph, it adjusts all the lines into an instructed width, so that it can show the words evenly spread out and fit in a straight edge at both margins (similar to "align full" option in a Microsoft word document).

This can be relatively easily done as we are dealing with a mono-space font set (not a proportional font set) shown on a command-line-type console. As an example, consider a line containing 5 words and 30 characters altogether. If this line needs to be justified into a 40-character width, the remaining10 spaces needs to be spread out between the words. Assume that the first word of the next line has more than 9 characters, that is, the word in the next line cannot be placed in the tail of this line so as to consume the extra spaces.

In this case, 2 spaces are placed between the first 4 words, followed by 4 spaces prior to the last word (tail adjustment), or 3 spaces are placed between the first 4 words, followed by a space prior to the last word (even adjustment).

The requirements of this assignment are:

program keeps reading lines of text until reading an empty line

program then reads a width for the read paragraph

program then justifies the paragraph based on tail adjustment

program then shows the result in a bounding box

program allows to adjust the paragraph by going back to 2

program ends when it reads 0 as a new width

Extra points (2 points) will be considered for even adjustment implementation.

Here is a hint to realize this mission - lines of words, say vector<string> words, is a straightforward conversion as implemented in the previous assignment and posted lecture example. Now, user types some width for justification. Let this width be W. The mission is to fill i-words in one line of this W. Note i > 0, i.e., every line must have at least one word. The logic will be:

let { w1, w2, ..., wi } be a collection of words. Then, w1.length() + w2.length() + ... + wi.length() is the total length of this collection

since we need at least one space between these i words, we need i - 1 spaces at least, and therefore, we need Wmin = w1.length() + w2.length() + ... + wi.length() + (i - 1) characters, which must be less than or equal to W

your first loop must identify this i and Wmin by going through vector<string> words you created

in the second loop, you simply create a line by adding w1 through wi-1 by placing a space between the two consecutive words

before placing the last word wi, you need to place W - Wmin spaces because this number is the excess spaces to fill in for justification (tail adjustment)!

There is no miracle or hidden trick to deal with these operations.The left-hand side of the next example shows tail adjustment while the right-hand side shows even adjustment:


























  Enter text, empty return will quit the input  > Every photo, every edit, every album now lives   > in your iCloud Photo Library, easily viewable   > and consistent on all your devices.   > Automatically. The all-new Photos app makes   > it simpler than ever to find and rediscover     > your favorite photos. And you can make every   > shot look even better immediately after you've   > taken it with powerful new editing tools.  >  > Enter the width of text: 25  |-------------------------|  |very photo, every   edit,|  |every album now lives  in|  |your     iCloud     Photo|  |Library, easily  viewable|  |and  consistent  on   all|  |your             devices.|  |Automatically.        The|  |all-new Photos app  makes|  |it simpler than ever   to|  |find and rediscover  your|  |favorite photos. And  you|  |can make every shot  look|  |even  better  immediately|  |after  you've  taken   it|  |with powerful new editing|  |tools.                   |  |-------------------------|  Enter the width of text: 30  |------------------------------|  |very photo, every edit,  every|  |album now lives in your iCloud|  |Photo Library, easily viewable|  |and  consistent  on  all  your|  |devices.  Automatically.   The|  |all-new Photos app makes    it|  |simpler than ever to find  and|  |rediscover    your    favorite|  |photos. And you can make every|  |shot    look    even    better|  |immediately after you've taken|  |it with powerful new   editing|  |tools.                        |  |------------------------------|  Enter the width of text: 0  
   Enter text, empty return will quit the input  >  Every photo, every edit, every album now lives  > in your iCloud Photo Library, easily viewable   >  and consistent on all your devices.   > Automatically. The all-new Photos app makes   > it simpler than ever to find and rediscover     >  your favorite photos. And you can make every   > shot look even better immediately after you've   > taken it with powerful new editing tools.  >  > Enter the width of text: 25  |-------------------------|  |Every  photo, every edit,|  |every  album now lives in|  |your     iCloud     Photo|  |Library,  easily viewable|  |and   consistent  on  all|  |your             devices.|  |Automatically.        The|  |all-new  Photos app makes|  |it  simpler  than ever to|  |find  and rediscover your|  |favorite  photos. And you|  |can  make every shot look|  |even  better  immediately|  |after   you've  taken  it|  |with powerful new editing|  |tools.                   |  |-------------------------|  Enter the width of text: 30  |------------------------------|  |Every photo, every edit, every|  |album now lives in your iCloud|  |Photo Library, easily viewable|  |and  consistent  on  all  your|  |devices.   Automatically.  The|  |all-new  Photos  app  makes it|  |simpler  than ever to find and|  |rediscover    your    favorite|  |photos. And you can make every|  |shot    look    even    better|  |immediately after you've taken|  |it  with  powerful new editing|  |tools.                        |  |------------------------------|  Enter the width of text: 0  

Explanation / Answer

import java.util.Scanner;
import java.util.Vector;

public class paragraph {

   public static void main(String[] args) {

            int width=1;
            while(width!=0)
            {
               Vector<String> text=new Vector<String>();
               Scanner sc = new Scanner(System.in);
               int i = 0;
               String line;
               System.out.println("Please insert text:");
               while (!(line = sc.nextLine()).equals("")){
                    text.add(line);
                    i++;      
               }
              
              
               System.out.print("Enter the width of the text:");
               width=sc.nextInt();
               String paragraph="";
               for (int j = 0; j < text.size(); j++) {
                   paragraph=paragraph+text.get(j)+" ";
                   }
                    System.out.println(paragraph);
                   String[] c=splitStringEvery(paragraph, width);
                   for (int j = 0; j < c.length; j++) {
                   System.out.println(c[j]);
               }
  
            }
   }
                    public static String[] splitStringEvery(String s, int interval) {
                        int arrayLength = (int) Math.ceil(((s.length() / (double)interval)));
                        String[] result = new String[arrayLength];

                        int j = 0;
                        int lastIndex = result.length - 1;
                        for (int i = 0; i < lastIndex; i++) {
                            result[i] = s.substring(j, j + interval);
                            j += interval;
                        } //Add the last bit
                        result[lastIndex] = s.substring(j);

                        return result;
                    }
  

}

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