Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello there. I need some help with programming.Please look at instructions below. Instructions: The 8 problems here correspond to the 8 Java classes Part1.java, ...,

Hello there. I need some help with programming.Please look at instructions below.

Instructions:

The 8 problems here correspond to the 8 Java classes Part1.java, ..., Part8.java provided in the assignment skeleton. Part0.java is a sample program that reads data one line at a time from some input source and writes data to an output destination. You should use this as a basis for code.

1) Read the input one line at a time and write each line to the output if it is not a duplicate of some previous input line. Take special care so that a file with a lot of duplicate lines does not use more memory than what is required for the number of unique lines.

2) Read each of the lines of the input and output them in sorted order. Duplicate lines should be printed only once. Take special care so that a file with a lot of duplicate lines does not use more memory than what is required for the number of unique lines.

3) Read each of the lines of the input and output them in sorted order. Duplicate lines should be printed the same number of times they occur in the input. Take special care so that a file with a lot of duplicate lines does not use more memory than what is required for the number of unique lines.

4) Read the first 50 lines of input and then write them out in reverse order. Read the next 50 lines and then write them out in reverse order. Do this until there are no more lines left to read. If the number of lines is not a multiple of 50 then the last batch of lines to be reverses will be however many there are (between 1 and 49).

In other words, your output will start with the 50th line, then the 49th, then the 48th, and so on down to the first line. This will be followed by the 100th line, followed by the 99th, and so on down to the 51st line. And so on.

Your code should never have to store more than 50 lines at any given time.

5) Read the whole input one line at a time. Then output all lines sorted by length, with the shortest lines first. In the case where two lines have the same length, resolve their order using the usual "sorted order".

6) Read the entire input one line at a time and then output the even numbered lines (starting with the first line, line 0) followed by the odd-numbered lines. The order even lines (and odd lines) should be the same as they appeared in the input.

7) Read the input one line at a time. At any point after reading the first 42 lines, if some line is blank (i.e., a string of length 0) then output the line that occurred 42 lines prior to that one. For example, if Line 242 is blank, then your program should output line 200. This program should be implemented so that it never stores more than 43 lines of the input at any given time.

8) Read the entire input one line at a time and randomly permute the lines before outputting it.

Note: You shouldn't modify the contents of any line. The order of the lines will be modified. For a given input file, running the program multiple times should result in different output orders each time (with very high probability).

Also,iI have posted a Efficiency.java file below. You can use it to generate a moderately sized sample input file and then run all 8 of your programs with it. Here is how you do it (this is using Windows CMD.exe)

I have included two runs to see if the timing are different.

C:\Users\2402>javac Efficiency.java

C:\Users\2402>java Efficiency gen

C:\Users\2402>java Efficiency

Execution time: 0.7071423440000001

Execution time: 0.568024842

Execution time: 1.245898592

Execution time: 0.559175858

Execution time: 1.200164759

Execution time: 0.618761047

Execution time: 0.191140516

Execution time: 1.233576012

C:\Users\2402>java Efficiency

Execution time: 0.610975061

Execution time: 0.6044656220000001

Execution time: 1.172255998

Execution time: 0.521582475

Execution time: 1.563037865

Execution time: 0.538084423

Execution time: 0.19203694000000002

Execution time: 1.0954405900000002

------------------------------------------------------------------------------------------------------

Part0.java

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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part1.java

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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

-----------------------------------------------------------------------------------------------------------------------------

Part2.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part3.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part4.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part5.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part6.java

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 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part7.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Part8.java

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

/**

* 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: " + 1e-9 * (stop - start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------

Efficiency.java

import 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.io.File;

import java.io.FileNotFoundException;

public class Efficiency{

static String[] a = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "", "K", "L", "M"};

static void genExampleInput(String outFile){

try{

int size = a.length;

PrintWriter output = new PrintWriter(new FileWriter(outFile));

for(int h=0; h<3; h+=1){

for(int i=0; i

for(int j=0; j

for(int k=0; k

for(int l=0; l

for(int m=0; m

if(Math.random() < 0.01) output.println("");

output.println( a[i] + a[j] + a[k] + a[l] + a[m]);

}

}

}

}

}

}

output.close();

}catch(Exception e){

System.out.println(e);}

}

public static void main(String[] args) throws IOException{

if( args.length == 1){

if(args[0].equals("gen") ){

genExampleInput("sample_input.txt");

}else{

System.out.println("Usage: java Efficiency [gen]");

}

return;

}

Part1.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part2.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part3.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part4.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part5.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part6.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part7.main(new String[]{"sample_input.txt", "sample_output1.txt"});

Part8.main(new String[]{"sample_input.txt", "sample_output1.txt"});

}

}

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

Step: 3

blur-text-image

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

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

Psychological, financial, and career counseling.

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago