Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer question correctly and show the result of the working code. The ac

ID: 3889432 • Letter: P

Question

Please answer question correctly and show the result of the working code. The actual and demo code is provided .

This assignment is about using the Java Collections Framework to accomplish some basic text-processing 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).

Que:

Please answer question correctly and show the result of the working code. The actual and demo code is provided .

This assignment is about using the Java Collections Framework to accomplish some basic text-processing 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).

Que:

Please answer question correctly and show the result of the working code. The actual and demo code is provided .

This assignment is about using the Java Collections Framework to accomplish some basic text-processing 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).

Que:Read the whole input one line at a time and output each line if it is not a prefix of some previous line. (One string s is a prefix of another string t if t can be written as t=sx for some string x. For example, s='help' is a prefix of t='helpful' because 'helpful' = 'help' + 'ful'). Take care not to waste memory, so that your code stores the fewest number of lines possible. Hint: Consider what happens when you compare 'help' and 'helpful' using the usual ordering on Strings.

Actual code

package comp2402a1;

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 Part9 {

/**
* 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);
}
}
}

Demo code

package comp2402a1;

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);
}
}
}

Explanation / Answer

Given below is the completed program . Please make sure to pass the input correctly.

There are 2 ways.

1. You save the input lines a file and pass the file name as command line argument to the program

2. You simply run the program without command line arguments. In this case, you can type lines on console and to stop entering more lines, press enter and in hte newline press Ctrl+D

The output shown below is by using command line argument i.e passing the filename to program.

Hope it helps. Please don't forget to rate teh answer if it helped. Thank you very much.

package comp2402a1;

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.SortedSet;

import java.util.TreeSet;

public class Part9 {

/**

* 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 {

SortedSet<String> set = new TreeSet<String>();

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

boolean prefix = false;

//check if the line is a prefix of any of previous lines

for(String s : set)

{

if(s.startsWith(line)) //check if the string s begins with line

{

prefix = true;

break;

}

else if(s.compareTo(line) > 0) //already reached a line that is greater than the search

break;

}

if(!prefix)// if line is not prefix of any previous , then add to set

set.add(line);

}

for (String text : set) {

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);

}

}

}

input file: input.txt

helpful
help
richer
comedy
starter
rich
come
greater
great
star

output

comedy
greater
helpful
richer
starter
Execution time: 0.01877484