Problem 1

CS170
Homework 5 - Due: Friday 11:59 pm, 10/31
Section 003
Fall 2014
• INFORMATION
This is the fifth homework for CS170 (Section 003) and it covers nested loops and
methods. Read the instructions carefully. In this homework you will have to write
Java programs. It is not on paper homework.
• PREPARATION
Start the terminal and type the following commands (press Enter at the end of each
line to execute the command)
1. Create a hw5 directory inside your cs170 directory
mkdir ~/cs170/hw5
2. Copy the file you will need
cp /home/cs170003/share/hw5/* ~/cs170/hw5
3. Move into the hw5 directory
cd ~/cs170/hw5
4. List the content of the current directory
ls
5. You should see two files: CoinFlipper.java and InvertedWords.java
Problem 1
(40 points) Write a program which will simulate a coin flip with the following options:
• Option 0
Basic coin flip n times. This flip should print the result in the following form:
TTHTTHHT (n times), where H - Head, T - Tail.
• Option 1
Biased coin flip, where biased point is specified by user. So, for example when the
biased point is 0.8, then all randomized values from 0 through 0.8 will be Head (or
Tail), all numbers from 0.8 through 1 will be Tail (or Head).
Also, the biased point has to be in range h0, 1i, otherwise method will print an
appropriate message and will not perform a flip.
• Option 2
Basic coin flip with specified T and H characters. So, for n toses and for ”X” as a T
and ”O” as a H, example results will be: XOXXOOOXXXOX (n times)
In the starter code four methods have been declared for you:
public
public
public
public
static
static
static
static
void
void
void
void
CoinFlip(int option)
SimpleFlip (int times)
BiasedFlip (int times, double bias)
PersonalizedFlip (int times, char H, char T)
Fill these methods with statements. In a method CoinFlip(int option) Depending on
the option passed to the method (0, 1 or 2), one of the three coin flip options
should be done (simple, biased or personalized), so depending on the option one
1
Homework 5 - Due: Friday 11:59 pm, 10/31
Fall 2014
another method will be called. Your job here is to fill all these four methods
in with statements. You have to prompt user for a number of tosses and other data
depending on the option (for biased flip, ask for biased point, and for specified T and H
characters, ask for these characters).
In other words, method CoinFlip(int option) is a wrapper, which calls one of the another
methods depending on the option.
As an example, when you call CoinFlip(1), method CoinFlip() should prompt for a number
of tosses (integer), for a biased point (double), then calls a method responsible for a biased
coin flip (so, call will look similar to this: BiasedFlip(times, biasedPoint);) and then called
method (BiasedFlip) will print the results.
Sample outputs
• For basic coin flip:
How many times to flip: 10
THTTHTTTHH
• For Biased coin flip:
How many times: 10
Enter a bias: 0.9
TTTTTTHTTT
• For basic coin flip with specified T and H characters:
How many times: 10
Enter a symbol for Head: X
Enter a symbol for Tail: O
OXXOXXOXOO
• For Biased coin flip:
How many times: 20
Enter a bias: 2.0
Bias not in h0; 1i range!
• For biased coin flip:
How many times: 8
Enter a bias: 0.25
HHTHHHHH
• For incorrect option (not in h0; 2i range):
This option is not allowed!
Hints:
• DO NOT change method declarations (parameters)!
• Your work here is to fill in those four methods with statements.
• Use Math.random() to get a random number that is greater than or equal to 0.0 and
less than 1.0.
2
Homework 5 - Due: Friday 11:59 pm, 10/31
Fall 2014
Problem 2
(60 points) Write a program which will prompt user for a string and for set of words.
Program should perform searching all inverted entered words in the given string and print
all positions where words have been found (including position, word and inverted word).
Words are inverted in such a way:
• For word: abc, inverted: cba
• For word: following, inverted: gniwollof
• For word: Karma, inverted: amraK
As an example, assume that user enters ”This is my string which I would like to check with
a string” as a string and the following set of words: ”ekil si gnirts”. Then, you take first
word (”ekil ”), invert this word to ”like” and perform a search in a string. ”like” is found, so
you print inverted word ”ekil ”, word ”like” and position where it has been found ”position
32”. Then you take the next word ”si ”, again invert it to ”is” and again perform searching
in a string. Program then should find this word at position 5. Next, you take ”gnirts”,
invert it and search. Program should find two ”string” words in a string: at position 11 and
52. Below you can find a sample outputs.
Sample outputs
• java InvertedWords
Enter a string: This is my string which I would like to check with string
Enter words: ekil si gnirts
Found inverted word ’ekil’ which is ’like’ at position 32
Found inverted word ’si’ which is ’is’ at position 5
Found inverted word ’gnirts’ which is ’string’ at position 11
Found inverted word ’gnirts’ which is ’string’ at position 51
• java InvertedWords
Enter a string: Checking for string. Two words: string and string
Enter words: gnirts This siht
Found inverted word ’gnirts’ which is ’string’ at position 32
Found inverted word ’gnirts’ which is ’string’ at position 43
• java InvertedWords
Enter a string: This is test where none of the provided words will be found.
Enter words: test Is dnuof
• java InvertedWords
Enter a string: word word word word word word word word
Enter words: is si bread drow
Found inverted word: ’drow’ which is ’word’ at position 0
Found inverted word: ’drow’ which is ’word’ at position 5
Found inverted word: ’drow’ which is ’word’ at position 10
Found inverted word: ’drow’ which is ’word’ at position 15
Found inverted word: ’drow’ which is ’word’ at position 20
Found inverted word: ’drow’ which is ’word’ at position 25
3
Homework 5 - Due: Friday 11:59 pm, 10/31
Fall 2014
Found inverted word: ’drow’ which is ’word’ at position 30
Found inverted word: ’drow’ which is ’word’ at position 35
• java InvertedWords
Enter a string: The full name of CS is Computer Science. Computer Science rocks!
Enter words: retupmoC lluf ehT
Found inverted word ’retupmoC’ which is ’Computer’ at position 23
Found inverted word ’retupmoC’ which is ’Computer’ at position 41
Found inverted word ’lluf’ which is ’full’ at position 4
Found inverted word ’ehT’ which is ’The’ at position 0
IMPORTANT NOTE: Please note that in the second example, only two last string occurences are printed. It’s because the first one is: ”string.” with dot character right after
the word. The word is found if and only if right before and after this word there is a space
character (or after the word it is end of the string not ended with any special character such
as dot or comma)
Hints:
• Iterate through set of words, inverting each of them and then perform a search on the
entire string.
• Remember about keeping a word and inverted word, you will need them when the
search will find something.
• Note that program is case-sensitive. item != Item
• You can assume that all words are separated by one space character, but special
characters such as ”.”, ”,” or ”!” could appear, but there is no more than one space
length ”hole” between words. Example sentence: This is an example sentence, which
is used to present you the idea of input for this assignment. Special characters could
appear, and they will appear.
• Set of words are separated by one space character.
• Use nested loops.
Grading
Your grade will be determined based on the correctness of your programs as well as program style. Program style includes such things as comments, variable/method names, and
readability.
Submission
1. Make sure you signed the Honor Code statement at the top of files that you submit.
Failure to do so will result in a 0 for the assignment.
2. Only your last submission will be graded. Scores will be assigned based on the last
submission only.
4
Homework 5 - Due: Friday 11:59 pm, 10/31
Fall 2014
3. Submit your work using the following command. You need to be in your ˜/cs170/hw5
directory when you issue it. Then type the following commands:
/home/cs170003/turnin CoinFlipper.java hw5a
/home/cs170003/turnin InvertedWords.java hw5b
4. Your homework is not turned unless the above commands are successful (you will get
a ”success” message when turn in was successful).
5