Ask Question

In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than 50 characters). 2. The program will read the sentence and put it into an array of characters. 3. Then, it creates one thread for each character in the sentence. 4. The goal of the program is to capitalize each letter that has an odd index. The given program actually does this, but lacks the synchronization of the threads, so the output is not correct. You will need to provide the synchronization using mutex locks. Specifically, you are to (1) declare the mutex lock, (2) initialize the mutex lock, (3) lock and unlock the mutex lock at an appropriate location that results in the code working as expected, and (4) destroy the mutex lock. Be sure to place the mutex locks so that your program works correctly every time. Do not remove code or functions - you are to add the synchronization pieces only.

+3
Answers (1)
  1. 18 April, 12:14
    0
    The code is given below,

    import java. io. File;

    import java. io. FileInputStream;

    import java. util. ArrayList;

    import java. util. List;

    import java. util. Scanner;

    public class SentenceUtilsTest {

    public static void main (String[] args) {

    List sList = new ArrayList ();

    try{

    File file = new File (args[0]);

    Scanner scanner = new Scanner (new FileInputStream (file));

    while (scanner. hasNextLine ()) {

    String sent = scanner. nextLine ();

    if (sent. trim (). length () > 0) {

    SentenceUtils sUtils = new SentenceUtils (sent);

    sList. add (sUtils);

    }

    }

    System. out. println ("File that was read:" + args[0]);

    System. out. println ("File contains " + sList. size () + " sentences./n");

    System. out. println ("Sentences reports:/n");

    for (int line = 0; line < sList. size (); line++) {

    System. out. println ("Sentences " + line + "; ");

    System. out. println (sList. get (line). getSentence ());

    SentenceUtils lineText = sList. get (line);

    String[] tokens = lineText. getTokens ();

    for (int id = 0; id < tokens. length; id++) {

    System. out. println (id+":"+tokens[id]);

    }

    String[] shingles = lineText. getShingles ();

    for (int id = 0; id < shingles. length; id++) {

    if (shingles[id]! = null) {

    System. out. print (shingles[id]+" ");

    }

    }

    System. out. println ("/n");

    }

    }catch (Exception ex) {

    ex. printStackTrace ();

    }

    }

    }

    package sentenceutils;

    import java. util. ArrayList;

    import java. util. List;

    public class SentenceUtils {

    private String sentence;

    private String[] tokens;

    private String[] shingles;

    public SentenceUtils (String s) {

    this. sentence = s;

    generateTokens ();

    generateShingles ();

    }

    private void generateShingles () {

    List shinglesList = new ArrayList ();

    for (int index=0; index < sentence. length () - 1; index++) {

    shinglesList. add (sentence. charAt (index) + ""+sentence. charAt (index+1));

    }

    shingles = new String[sentence. length () ];

    shinglesList. toArray (shingles);

    }

    private void generateTokens () {

    tokens = sentence. split (" ");

    }

    public String getSentence () {

    return sentence;

    }

    public String[] getTokens () {

    return tokens;

    }

    public String[] getShingles () {

    return shingles;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than 50 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers