Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA ! A few simple questions involving JAVA COLLECTIONS FRAMEWORK.. THANKS! These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,

IN JAVA ! A few simple questions involving JAVA COLLECTIONS FRAMEWORK.. THANKS!

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

  1. [5 marks] Read the input one line at a time until you have read all n lines and imagine these lines are numbered 0,...,n-1. Next output lines floor(n/2),...,n-1 followed by lines 0,..,.floor(n/2)-1.
  2. [5 marks] Read the input one line at a time and output the current line if and only if it is smaller than any other line read so far. (Here, smaller is with respect to the usual order on Strings, as defined by String.compareTo().
  3. [5 marks] Read the input one line at a time and output only the last 9999 lines in the order they appear. If there are fewer than 9999 lines, output them all. For full marks, your code should be fast and should never store more than 9999 lines.
  4. [5 marks] Read the input one line at a time and output the current line if and only if it is a duplicate of some previous line.
  5. [5 marks] Read the input one line at a time. When you are done, output all the lines in case-insensitive sorted order.
  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.
  7. [5 marks] Read the input one line at a time and output the current line if and only if you have already read at least 1000 lines greater than the current line and at least 1000 lines less than the current line. (Again, greater than and less than are with respect to the ordering defined by String.compareTo().)

CLASSES (Part 0 is an example)

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

THIS IS TEMPLATE FOR EVERY PART

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

List some problems associated with risk tolerance questionnaires.

Answered: 1 week ago

Question

identify current issues relating to equal pay in organisations

Answered: 1 week ago