In: Computer Science
IN JAVA LANGUAGE!!!
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.
Q1) 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.
Q2) ] 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().)
CODE FORMAT:
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 {
/**
* @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
}
/**
* 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-1
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part3 {
/**
* @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
ArrayList<String> list = new
ArrayList<>();
FileInputStream filestream;
String path = "data.txt";
int min=0;
int max=0;
try {
filestream = new FileInputStream(path);
BufferedReader br = new BufferedReader(new
InputStreamReader(filestream));
String str = br.readLine();
while (str != null) {
list.add(str);
str =
br.readLine();
}
br.close();
} catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(list.size()<=9999) {
for(String str :
list) {
System.out.println(str);
}
}
else {
int len =
list.size();
int i =
len-9999;
for(int
j=i;j<len;j++)
System.out.println(list.get(j));
}
}
/**
* 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);
}
}
}
Screenshot
Question-2
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part3 {
/**
* @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
ArrayList<String> list = new
ArrayList<>();
FileInputStream filestream;
String path = "data.txt";
int min=0;
int max=0;
try {
filestream = new FileInputStream(path);
BufferedReader br = new BufferedReader(new
InputStreamReader(filestream));
String str = br.readLine();
while (str != null) {
list.add(str);
min=0;
max=0;
for(int
i=0;i<list.size()-1;i++) {
if(str.compareTo(list.get(i))>0)
max++;
else
if(str.compareTo(list.get(i))<0)
min++;
}
if(min>=1000 &&
max>=1000)
System.out.println(str);
str =
br.readLine();
}
br.close();
} catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 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);
}
}
}
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful