Question

In: Computer Science

Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....

Instruction

This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order on Strings, as defined by String.compareTo(s). Part 0 in the assignment is an example specification and solution.

Part0

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Part0 {
  
   /**
   * Read lines one at a time from r. After reading all lines, output
   * all lines to w, outputting duplicate lines only once. Note: the order
   * of the output is unspecified and may have nothing to do with the order
   * that lines appear in r.
   * @param r the reader to read from
   * @param w the writer to write to
   * @throws IOException
   */
   public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
Set<String> s = new HashSet<>();

for (String line = r.readLine(); line != null; line = r.readLine()) {
s.add(line);
}

for (String text : s) {
w.println(text);
}
   }

   /**
   * The driver. Open a BufferedReader and a PrintWriter, either from System.in
   * and System.out or from filenames specified on the command line, then call doIt.
   * @param args
   */
   public static void main(String[] args) {
       try {
           BufferedReader r;
           PrintWriter w;
           if (args.length == 0) {
               r = new BufferedReader(new InputStreamReader(System.in));
               w = new PrintWriter(System.out);
           } else if (args.length == 1) {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(System.out);              
           } else {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(new FileWriter(args[1]));
           }
           long start = System.nanoTime();
           doIt(r, w);
           w.flush();
           long stop = System.nanoTime();
           System.out.println("Execution time: " + 10e-9 * (stop-start));
       } catch (IOException e) {
           System.err.println(e);
           System.exit(-1);
       }
   }
}

Question 6

[5 marks] Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output.

Template code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Part6 {
  
   /**
   * Your code goes here - see Part0 for an example
   * @param r the reader to read from
   * @param w the writer to write to
   * @throws IOException
   */
   public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
       // Your code goes here - see Part0 for an example
   }

   /**
   * The driver. Open a BufferedReader and a PrintWriter, either from System.in
   * and System.out or from filenames specified on the command line, then call doIt.
   * @param args
   */
   public static void main(String[] args) {
       try {
           BufferedReader r;
           PrintWriter w;
           if (args.length == 0) {
               r = new BufferedReader(new InputStreamReader(System.in));
               w = new PrintWriter(System.out);
           } else if (args.length == 1) {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(System.out);              
           } else {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(new FileWriter(args[1]));
           }
           long start = System.nanoTime();
           doIt(r, w);
           w.flush();
           long stop = System.nanoTime();
           System.out.println("Execution time: " + 10e-9 * (stop-start));
       } catch (IOException e) {
           System.err.println(e);
           System.exit(-1);
       }
   }
}

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Part6 {

    /**
     * Your code goes here - see Part0 for an example
     *
     * @param r the reader to read from
     * @param w the writer to write to
     * @throws IOException
     */
    public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
        List<String> list = new ArrayList<>();
        String line;
        boolean found;
        while ((line = r.readLine()) != null) {
            found = false;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).endsWith(line)) {
                    found = true;
                }
            }
            if (!found) {
                w.println(line);
            }
            list.add(line);
        }
    }

    /**
     * The driver. Open a BufferedReader and a PrintWriter, either from System.in
     * and System.out or from filenames specified on the command line, then call doIt.
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            BufferedReader r;
            PrintWriter w;
            if (args.length == 0) {
                r = new BufferedReader(new InputStreamReader(System.in));
                w = new PrintWriter(System.out);
            } else if (args.length == 1) {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(System.out);
            } else {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(new FileWriter(args[1]));
            }
            long start = System.nanoTime();
            doIt(r, w);
            w.flush();
            long stop = System.nanoTime();
            System.out.println("Execution time: " + 10e-9 * (stop - start));
        } catch (IOException e) {
            System.err.println(e);
            System.exit(-1);
        }
    }
}

Related Solutions

Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList are the same data structure but implement data storage in different ways. Classes, that implement Map interface in Java collections framework are used for storing what type of data? Declare and instantiate a list of elements of type String. Name this list myArray. what type of data structure is Stack? LinkedList data structure in Java collections is implemented as doubly linked lists. In PriorityQueue...
java code: adds a new regular task, delete a task , show all tasks, and show...
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks. I also need a UML diagram for the code update: The "task" is like something in a to do list. example) task 1. name: get carrots important: no completed: yes. you can run my code as an example if needed
For this problem, you will be asked to write some code to accomplish a particular task...
For this problem, you will be asked to write some code to accomplish a particular task given the code fragment below. Each task may depend on the tasks that came before it. Your code must be syntactically correct. None of your operations for this problem may affect S::m_unique. class S { public: S(int init, int size, int id) :m_num(init), m_size(size) {                m_unique = new int[m_num]; for (int i = 0; i < m_num; i++)       m_unique[i] = id;...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a select statement to show the invoicelineitemdescriptions that have the total invoicelineitemamount >1000 and the number of accountno is >2. 10. Write a select statement that returns the vendorid, paymentsum of each vendor, and the number of invoices of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number...
Think about how the structure of tasks affect leaders' behaviors. They are task autonomy, task feedback,...
Think about how the structure of tasks affect leaders' behaviors. They are task autonomy, task feedback, task structure, and task interdependence. It also sees most problems, as defined by Ronald Heifitz, as either technical or adaptive. Describe how you as a leader (or another leader who you've observed) used this analysis in effectively dealing with an organizational challenge. Which of the variables related to the problem's definition and how was it handled, either as a technical problem or an adaptive...
An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False...
An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False 2 points    QUESTION 2 All components of an array must be of the same type (all int, all double, all string, etc.). True False 2 points    QUESTION 3 A group of objects defined from the same class will all have different properties and different methods. True False 2 points    QUESTION 4 Each instance of an object from a particular class is...
Use Java GUI create following: Task 1: A basic UI with a button and a TextField,...
Use Java GUI create following: Task 1: A basic UI with a button and a TextField, when you press the button, set the button text to the current text field contents. Task 2: set the text field text to the mouse coordinates when that same button is pushed.
What are some of the benefits for using IT Service management framework for businesses?
What are some of the benefits for using IT Service management framework for businesses?
Your Tasks Step 1 Write the comments in the SnapCracklePop.java using the java doc style in...
Your Tasks Step 1 Write the comments in the SnapCracklePop.java using the java doc style in zyBooks 3.8. See TODOa to TODOh.   Step 2 Complete the class definition, variable declarations, constructor, and all the methods by reading the instructions in SnapCracklePop.java. See TODO1 to TODO7. (DONE I NEED HELP WITH STEP 1) 1 //TODOa complete this javadoc comment 2 /** 3 * [The class description] 4 * @author [Your Name] 5 * @version 1.0 6 */ 7 8 //TODO1: declare...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT