Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have a code here and I wanted to know if it was implemented correctly. The language is Java and the Problem is below

Hi, I have a code here and I wanted to know if it was implemented correctly. The language is Java and the Problem is below my code.

import java.util.ArrayList;

import java.util.List;

public class ReArrange {

public List fullJustify(String[] words, int maxWidth) {

ArrayList result = new ArrayList();

if (words == null || words.length == 0) {

return result;

}

int count=0;

int last=0;

for (int i=0;i

count = count + words[i].length();

if (count+i-last > maxWidth) {

int wordsLength = count - words[i].length();

int spaceLength = maxWidth - wordsLength;

int eachLen = 1;

int extraLen = 0;

if (i-last-1 > 0) {

eachLen = spaceLength /(i-last-1);

extraLen = spaceLength % (i-last-1);

}

StringBuilder sb = new StringBuilder();

for (int k=last;k

sb.append(words[k]);

int ce = 0;

while (ce

sb.append(" ");

ce++;

}

if (extraLen > 0) {

sb.append(" ");

eachLen--;

}

}

sb.append(words[i-1]);

while (sb.length()

sb.append(" ");

}

result.add(sb.toString());

last = i;

count = words[i].length();

}

}

StringBuilder sb = new StringBuilder();

for (int i=last;i

count = count + words[i].length();

sb.append(words[i] + " ");

}

sb.append(words[words.length-1]);

while (sb.length()

sb.append(" ");

}

result.add(sb.toString());

return result;

}

public static void main(String[] args) {

ReArrange t = new ReArrange();

String[] words1 = {"This", "is", "an", "example", "of", "text", "justification."};

String[] words2 = {"What","must","be","acknowledgment","shall","be"};

String[] words3 = {"Science","is","what","we","understand","well","enough","to","explain",

"to","a","computer.","Art","is","everything","else","we","do"};

System.out.println(t.fullJustify(words1, 16));

System.out.println(t.fullJustify(words2, 16));

System.out.println(t.fullJustify(words3, 20));

}

}

image text in transcribed

image text in transcribed

HW1 Implement MyArrayList and MyLinkedList using Mylist interface and MyAbstractList as defined in Java Collection-Framework. (15 points)l For the following problem use your own created MyArraylist or MyLinkedList if needed. (5 points) Given an array of words and a width maxWictth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.T You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces"'when necessary so that each line has exactly maxWidth characters.I Extra spaces between words should-be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.I For the last-line of text, it should be left justified and no extra space is inserted between words.Il Note: .-A word is defined as a character sequence consisting of non-space characters only. . Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.I . The input array words 'contains at least one word. Example 1: Input:9 words["This", "is", -"an" "example", "of", "text", "justification."]9 Output:9 [9 ."This , o, ois , o , oan", 1 oof.text",9 o, o"justification.. og 19 Example 2: Input:9 words Output:9 [9 . "What.must. .be", o, "shall-be...." 19 Explanation: Note that.the last.line is. "shall be" instead of. "shall..be",9 . . . . . . . . . . . . because the-last-1 ine-must-be-1eft-justified instead-of-fully-justified. I ..........Note that the second.line is also-left-justified becase it contains only one word.9 Example 3: Input:9 words["Science", "is", "what", "we", "understand", "well", "enough", "to", "explain",g Output: [9 . "Science 4s-What . we",9 nderstand , o , o , owell" ,9 o. "enough to explain to",9 a. ecomputer Art is",9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions