please write this in java language! thank you! The main task of the assignment i
ID: 3756259 • Letter: P
Question
please write this in java language! thank you!
The main task of the assignment is to write a method, public static void format( String text, int width, int option ) This method generates the output. The three parameters represent the following: text is the String that represents the text to be printed. . width is an int value that represents the width. option is an int value representing the option (1, 2, or 3) In processing the input, text, the method format assumes that there is no tab-stop appearing text and that text ends with a newline. To ensure these two properties, at the start, the methood modifies text as follows: . If it does not end with the newline (which can be checked with endsWith of the class String the method attaches a newline to text Using replace of the class String, it generates a new in which each occurrence of tab-stop is replaced with " ", and stores it in text To generate the output, the method format uses two StringBuilder objects, lineBuilder and wordBuilder. lineBuilder is for building each line of the output, where the "words" appearing in the same line are appended to lineBuilder with one ' ' in betweenExplanation / Answer
public static void format(String text, int width, int option) { if (text == null) { return; } if (!text.endsWith(" ")) { text += " "; } text = text.replace(" ", " "); StringBuilder lineBuilder = new StringBuilder(); StringBuilder wordBuilder = new StringBuilder(); for (int i = 0; i 0) { if(lineBuilder.length() == 0) { lineBuilder.append(wordBuilder.toString()); } else { if(lineBuilder.length() + wordBuilder.length() + 1Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.