Question
Please USE JAVA only Write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens
Please USE JAVA only
Write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total.
Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it is added to the integer accumulator. The accumulator keeps a running total of all the integers which have been seen so far. Likewise, there is an accumulator for floating point values. It starts at zero, but adds every floating point value which is seen. Finally, there is a string accumulator. It starts out as an empty string, but appends to the string every time another string token is read. Your class will need to be able to decide the type of each input token (hint: a Scanner has the methods hasNextInt(), hasNextDouble(), and hasNext()) as well as to keep track of the running totals (hint: you can use instance variables).
The only import statement you may use in this implementation is the Scanner class. Any data elements should be declared private, although this will not be graded. If you think it is necessary you may include helper methods of your own. The class should implement the following public methods:
- public InputSplitter() this constructor will initialize the three accumulators as well as the Scanner which is used for input.
- public void next() read the next token, and print its type and value. This method will use the input Scanner object which was initialized earlier to read the next token from the input. Depending on the type of the input token, method will print the value of the token in one of the following three formats:
integer: 1
double: 2.0
string: abc
In addition to the printed output, the value will be added (appended, in the case of a string) to the accumulator for the specific type. - public int getIntTotal() retrieves the total value summed in the integer accumulator.
- public double getDoubleTotal() retrieves the total value summed in the floating point accumulator.
- public String getStringTotal() retrieves the complete value concatenated in the string accumulater.
- public String toString() returns the current contents of each of the three accumulators as a single string in the following format:
integer: 1 double: 2.0 string: abc
-
Below is a sample run demonstrating how the class would function using the following input: 1 abc 2.0 3 def 4.0 6 7 xyz 5.5
Please make sure that it passes the following: https://repl.it/@micky123/NeglectedWoozyTruetype
InputSplitter inew InputSplitter); > System.out.println(i); integer: 0 double 0.0 string: > i.next); integer: 1 > System.out.println(i); integer: 1 double .0 string: > i.next); string: abc > i.next); double: 2.0 > System.out.println(i); integer: 1 double 2.0 string: abc > i.next); integer: 3 > System.out.println(i); integer: 4 double 2.0 string: abc > i.next); string: def > System.out.println(i); integer: 4 double 2.0 string: abcdef > i.next); double: 4.0 > System.out.println(i); integer: 4 double 6.0 string: abcdef > i.next); integer: 6 > i.next); integer: 7 > i.next); string: xyz > i.next); double: 5.5 > System.out.println(i); integer: 17 double 11.5 string: abcdefxyz System.out.println(i.getIntTotal()); System.out.println(i.getDoubleTotal)); System.out.println(i.getstringTotal)); 17 11.5 abcdefxyzStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started