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

Create a class called StreamStats with one constructor and two methods construct

ID: 3665405 • Letter: C

Question

Create a class called StreamStats with one constructor and two methods

constructor

Given an InputStream input for which you will process some statistics

Methods

read the entire InputStream supplied in the constructor. While reading the stream determine what types of input you are consuming

number of booleans in the stream

number of doubles in stream

number of ints in the stream

number of Strings (any token that does not classify as boolean, double or int)

example: "This is a 2 true 3.14 13.45"

one boolean

two doubles

one int

three Strings

return a vector of statistics in the stream seen in the stream. [# of boolean, # of double, # of int, # of String] (the order is the alphabetical order the types.)

if consume() has not been invoked, the vector returned should be all zeros.

Example: the vector returned in the example above would be [1,2,1,3]

NOTES

Look at the Scanner class to see how to check if the stream still has input, and then to see what kind of input is available.

Tokens that do not fit into boolean, int, or double are classified as Strings.

Make sure that your code checks for ints before doubles, for example, 5000 is an int but can fit into a double.

Explanation / Answer

StreamStats.java


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;

public class StreamStats {
   InputStream inputStream = null;
   int stringCount = 0;
   int doubleCount = 0;
   int intCount = 0;
   int booleanCount = 0;
   int countStatus[] = new int[4];
   public StreamStats(InputStream is) throws IOException{
       this.inputStream = is;
       consume();
      
      
      
   }
   public void consume() throws IOException{
       // read it with BufferedReader
           BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
           Scanner scan = new Scanner(br);
           String decimalPattern = "([0-9]*)\.([0-9]*)";
           String integerPattern = "[0-9]";
           while(scan.hasNext()){
               Object obj = scan.next();
               boolean doubleMatch = Pattern.matches(decimalPattern, obj.toString());
               boolean intMatch = Pattern.matches(integerPattern, obj.toString());
               if("true".equals(obj) || "false".equals(obj)){
                   booleanCount++;
               }
               else if(doubleMatch){
                   doubleCount++;
               }
               else if(intMatch){
                   intCount++;
               }
               else {
                   stringCount++;
               }
           }
           System.out.println("Boolean : "+booleanCount+" Double : "+doubleCount+" Integer :"+intCount+" String :"+stringCount);
           countStatus[3] = stringCount;
           countStatus[2] = intCount;
           countStatus[1] = doubleCount;
           countStatus[0] = booleanCount;
   }
   public int [] stats(){
       return countStatus;
   }

   public static void main(String arg[]) throws IOException{
       String str = "";
       Scanner scan = new Scanner(System.in);
       System.out.println("Please enter the string");
       str = scan.nextLine();
       // convert String into InputStream
       InputStream is = new ByteArrayInputStream(str.getBytes());

       StreamStats obj = new StreamStats(is);
       int statistics[] = obj.stats();
       System.out.println(Arrays.toString(statistics));
   }
}

Output:

Please enter the string
This is a 2 true 3.14 13.45
Boolean : 1 Double : 2 Integer :1 String :3
[1, 2, 1, 3]

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