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

Write a program that will filter a list of non-negative integers such that all d

ID: 3804199 • Letter: W

Question

Write a program that will filter a list of non-negative integers such that all duplicate values are removed. Integer values will come from standard input (the keyboard) and will range in value from 0 up to 2, 147, 483, 647. Input will be terminated by the special value, -1. Once the input is read in you should output (to the screen) the list of numbers as a sorted list (smallest to largest) with one value listed per line where all duplicates have been removed. The primary difficulty with this program is that there are an enormous number of input values and an expected large number of duplicate numbers. A sample run of the program is displayed below: You may find it useful to use input/output redirection while testing your program so that you can have large input files. Recall that from the command line you can use the command: my_program.exe output.txt to redirect standard input so that it reads from the file input instead and redirects standard output so that it outputs to the file output instead of the screen. You can also write a program to generate input for your main program and you can avoid the input file altogether by using a pipe. The | character (shift backslash on most keyboards) will take the output of one program and use it as input for another program: my_number_generator.exe | my_program.exe > output .txt will run the my_number_generator.exe program and pipe all of the output from that program as input to the my_program.exe executable and then finally take the output from that program and redirect it to a file named output.

Explanation / Answer

Scernario 1:Input from Console

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class TESTING {




public static void main(String args[]) {
  
Set<Integer> set = new HashSet<Integer>();
try {
  
Scanner in = new Scanner(System.in);
int i = in.nextInt();
do{ if(i ==-1){

TreeSet sortedSet = new TreeSet<Integer>(set);
System.out.println("The sorted list is:");
Iterator iterator=sortedSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}

return ;
}
else{

set.add(i);
}
}while(i==-1);
  
}
catch(Exception e) {}
}
}

************************************************************************************************************

Scenario 2:File I/O

import java.io.*;

import java.util.*;


public class CopyFile {

public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
Set<Integer> set = new HashSet<Integer>();

try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");

int c;
while ((c = in.read()) != -1) {
set.add(c);   

}
TreeSet sortedSet = new TreeSet<Integer>(set);
  
Iterator iterator=sortedSet.iterator();
    while (iterator.hasNext()){
       out.write( iterator.next() + " ");
       }
         
  


}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote