Edited code:
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Subset {
public static void main(String[] args) {
int sampleSizek = Integer.parseInt(args[0]);
String[] samplearr = new String[sampleSize];String[k];
int i = 0;
//String initializestr; the sample from reservoir
while((str=stdin.readString())!=null && i < samplearr.length){
sample[i++]arr[i++] = StdIn.readString();str;
// StdIn : a class in the provided JAR // System.out.println(str);
}
try{
for(; ;(str=stdin.readString())!=null; i++){
int randomIndexr = (int)(Math.random()*(i + 1));
if(randomIndexr < sampleSizek){
sample[randomIndex]arr[r] = StdIn.readString();str;
}
//System.out.println(str);
}
}catch(RuntimeException e){
// do nothing}
}catch(RuntimeException e){ // do nothing }
for(String s : samplearr){
System.out.print(s + " ");
}
System.out.println("\n");
}
}
StdIn.readString():
class stdin{
private stdin(){}
private static Scanner sc = new Scanner(new BufferedInputStream(System.in));
public static String readString() {
return scannersc.next();
}
}
Edit: to To clear out confusion , iI am posting what the apiAPI and the instructor demands.:
Subset client. Write a client program Subset.java that takes a command-line integer k,
reads in a sequence of N strings from standard input using StdIn.readString(),
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once.
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to
at most N in the worst case, where N is the number of strings on standard input.
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
public static void main(String[] args)
}
Subset client. Write a client program Subset.java that takes a command-line integer k,
reads in a sequence of N strings from standard input using StdIn.readString(),
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once.
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to
at most N in the worst case, where N is the number of strings on standard input.
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
public static void main(String[] args)
}