1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
irakobra [83]
3 years ago
12

We are building a word processor and we would like to implement a "reflow" functionality that full-justifies text.Given a maximu

m line width and an array containing lines of text, re-flow the text to fit the new width. Each line should be have the exact specified width. If any line is too short, insert spaces between words as equally as possible until it fits.
Computers and Technology
1 answer:
adoni [48]3 years ago
6 0

Answer:

The code is given below in Java with appropriate comments

Explanation:

Reflow is an function which helps you to justify your functionality

You need to input a string array and the max width and you will get the

output in a list(string).

public List<String> Reflow(String[] input, int width) {

   List<String> result = new ArrayList<String>();

 

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

       return result;

   }

 

 

   int count=0;

   int last=0;

   ArrayList<String> list = new ArrayList<String>();

   for(int i=0; i<input.length; i++){

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

 

       if(count+i-last>width){

           int wl = count-input[i].length();

           int sl = maxWidth-wl;

           int each = 1;

           int el = 0;

 

           if(i-last-1>0){

               each = sl/(i-last-1);

               el = sl%(i-last-1);

           }

 

           StringBuilder sb = new StringBuilder();

 

           for(int k=last; k<i-1; k++){

               sb.append(input[k]);

 

               int ce = 0;

               while(ce<each){

                   sb.append(" ");

                   ce++;

               }

 

               if(el>0){

                   sb.append(" ");

                   el--;

               }

           }

 

           sb.append(input[i-1]);//last input in the line

           //if only one word in this line, need to fill left with space

           while(sb.length()<width){

               sb.append(" ");

           }

 

           result.add(sb.toString());

 

           last = i;

           count=input[i].length();

       }

   }

 

   int lastLen = 0;

   StringBuilder sb = new StringBuilder();

 

   for(int i=last; i<input.length-1; i++){

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

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

   }

 

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

   int d=0;

   while(sb.length()<maxWidth){

       sb.append(" ");

   }

   result.add(sb.toString());

 

   return result;

}

You might be interested in
An item such as a smart card is an example of the _______ form of authentication.
natka813 [3]
<span>An item such as a smart card is an example of the something you have form of authentication.
A smart card is very similar to credit card because of its shape and size, however inside it is totally extraordinary. As a matter of first importance, it has an inside - an ordinary charge card is a basic bit of plastic. Within a smart card for the most part contains an inserted microchip.
</span>
5 0
3 years ago
An if statement must always begin with the word “if.” True False python
MrMuchimi

Answer:

true

Explanation:

6 0
3 years ago
Read 2 more answers
What is microsoft excel​
Andrei [34K]

Answer:

A spreadsheet program included in Microsoft Office suit of application. Spreadsheets present tables of value arranged in rows and columns that can be manipulated mathematically using both basic and advanced functions.

6 0
3 years ago
Read 2 more answers
What acronym describes networked devices that contain microcomputers but are not thought of as computing devices, such as refrig
vivado [14]
The acronym RFID (Radio Frequency Identification) describes networked devices that contain microcomputers but are not thought of as computing devices, such as refrigerators, automobile components, light bulbs, and industrial control devices. RFIDs are  battery-powered sensors that gather and transmit data to a reading device. Some sensor based technologies are  scanning electron microscopes, LiDAR,radar, GPS, x-ray, sonar, infrared and seismic.



5 0
3 years ago
350 square feet requires 1 gallon of paint. Assign gallons_paint with the amount of paint required for wall_area. Sample output
Marina CMI [18]
Here's a solution in node.js. Can be easily transcribed to other languages:

var paint_per_sqf = 1/350;
var wall_area = 250.0;
var gallons_paint = wall_area * paint_per_sqf;

console.log(wall_area.toFixed(1) + " square feet wall will need:");
console.log(gallons_paint.toFixed(12) + " gallons of paint");


4 0
2 years ago
Other questions:
  • A drivers touches a cars steering wheel on a hot day which term refers to the way heat is transferredd to the drivers hand
    6·2 answers
  • Behaving in an acceptable manner in a workplace environment is referred to as workplace etiquette.
    12·2 answers
  • A firewall is either software or dedicated hardware that exists between the __________ being protected.
    13·1 answer
  • What is the best web browser to use?
    9·2 answers
  • Sarah, a computer user, tells James, a computer technician, that the network she is connected to is running too slowly. Which of
    5·1 answer
  • What is the name of the process of checking the client's production environment to ensure software and hardware compatibility wi
    5·2 answers
  • 5. Write the name of the tab, command group, and icon you need to use to access the
    11·2 answers
  • Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bu
    13·1 answer
  • 2
    6·1 answer
  • A friend asks you to look over the code for an adventure game and help figure out why it won’t work. Which of these options is s
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!