The goal of this lab is to write a class PlayWithScanner that uses
a Scanner object to receive input from the user and acts on the
input. The program works as follows:
The program creates a Scanner object from the keyboard
The program prompts the user to enter an input String and
receives it by way of the nextLine method of the Scanner
object. This way the user is permitted to include a
whitespae or a tab in the input String.
Using a for-loop the program prints each letter of the input
String one character per line along with their position value.
The program creates a String that is the reverse of the input
String; that is, the String in which the letters of the input
String appear in the reverse order, and prints the reverse on
the screen. This String can be created using a for-loop.
The program asks the user to enter a position value and
receives it using nextInt and prints on the screen the prefix
of the input String ending at the position as well as the
suffix of the String starting from that position.
The program executes nextLine of the Scanner to read off the
new line.
The program prompts the user to enter a pattern and receives
it using the nextLine method and then counts the number of
times in which the pattern the user has entered appears in
the input String. This can be accomplished by producing all
the suffixes of the input String and checking whether they
start with the pattern. By using a counter variable and adds
1 to the counter each time the test passes the counter shall
be increased. Normally we would use an if-statement in this
situation, but this is something we are YET to study. So, we
will resort to a mathematical method for converting the return
value of the indexOf method to 0/1 as follows:
Consider the quantity diffSq defined as
the square of diff.
This square is 0 if patttern appears at
position and a strictly positive value
otherwise.
Consider the quantity diffSqSign defined as
the Math.signum of the square.
Then this quantity
is 0.0 if the square is 0 and 1.0 otherwise.
If we take the integer part of the signum value, by attaching
(int) in front of it, you get 0 if the
pattern appears at the position in question and 1 oterwise.
If you subtract this quantity from 1, you can swap between 0
and 1, and obtain the desired value.