Question

In: Computer Science

IN JAVA LANGUAGE!!! These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...

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

Solutions

Expert Solution

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


Related Solutions

The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and...
The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and Java IOStream. As mentioned at the beginning of our Collection’s class that the basis of java interfaces is mainly based on "TREES". However, one of these interfaces forms a different "TREE". Describe that particular interface. [1 Marks]. Why do we need to declare a type of object within each interface in the collection packages by "<E>" for example: <Integer, boolean, float, etc>”. [1 Marks]....
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
1. A double-ended queue, or deque, is a data structure consisting of a list of items...
1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x): insert item x on the back end of the queue addToFront(x): insert item x on the front end of the queue getBack(): returns the element on the back end of the queue getFront(): returns the element on the front end of the queue removeBack(): remove the back item from the queue removeFront(): remove the front item...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
The questions involve the data set for asking prices of Richmond townhouses obtained on 2014.11.03. For...
The questions involve the data set for asking prices of Richmond townhouses obtained on 2014.11.03. For your subset, the response variable is: asking price divided by 10000: askpr=c(55.8, 48.5, 57.8, 108.8, 50.5, 53.9, 33.7, 53.8, 78.8, 51.99, 47.9, 45.99, 77.8, 52.4, 58.68, 47.8, 59.8, 48.8, 57.8, 40.9, 68.5, 79.8, 68.8, 65.99, 40.8, 73.9, 54.98, 51.68, 73.8, 81.9, 86.8, 41.99, 50.8, 25.9, 58.8, 26.99, 62.9, 55.2, 54.8, 71.99, 56.8, 79.99, 74.8, 61.5, 68.8, 50.8, 53.8, 57.5, 44.8, 65.8) The explanatory variables...
The questions involve the data set for asking prices of Richmond townhouses obtained on 2014.11.03. For...
The questions involve the data set for asking prices of Richmond townhouses obtained on 2014.11.03. For your subset, the response variable is: asking price divided by 10000: askpr=c(65.8, 41.99, 54.8, 44.8, 50.8, 50.5, 54.98, 81.9, 48.5, 51.99, 26.99, 108.8, 57.8, 79.99, 33.7, 55.8, 40.8, 56.88, 46.8, 79.8, 53.8, 45.99, 40.9, 62.9, 48.8, 65.99, 58.39, 57.8, 50.8, 78.8, 68.8, 86.8, 54.8, 68.5, 58.68, 52.4, 51.68, 68.5, 59.8, 57.5, 68.8, 58.8, 53.9, 61.5, 47.9, 47.8, 77.8, 25.9, 60.8, 74.8) The explanatory variables...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT