Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2015



ASSIGNMENT 1

The goal of this assignment is introduce you to programming with loops (such as the for-loop or while-loop) and conditions (such as the if-then-else). You will also learn how to use randomness in your program.


Part 1: Double or nothing
You are currently X dollars in debt to your bookie but currently have only Y dollars to your name. Create a program DoubleOrNothing.java to calculate the minimum number of times you would have to go double-or-nothing at the roulette table to pay your debt. Your program should take two command-line arguments X and Y. X and Y are floating-point numbers representing the amounts in dollars and cents (e.g. 2345.32). Assume you go "all-in" on every bet, you win every bet, and the casino allows bets of any value. Assume X > 0 and Y > 0. Here some example runs:
% java DoubleOrNothing 1000 100
After 1 bet(s), total: 200.0
After 2 bet(s), total: 400.0
After 3 bet(s), total: 800.0
After 4 bet(s), total: 1600.0
Number of bets: 4
Final amount: 1600.0

% java DoubleOrNothing 12345.67 13.00
After 1 bet(s), total: 26.0
After 2 bet(s), total: 52.0
After 3 bet(s), total: 104.0
After 4 bet(s), total: 208.0
After 5 bet(s), total: 416.0
After 6 bet(s), total: 832.0
After 7 bet(s), total: 1664.0
After 8 bet(s), total: 3328.0
After 9 bet(s), total: 6656.0
After 10 bet(s), total: 13312.0
Number of bets: 10
Final amount: 13312.0

% java DoubleOrNothing 0.99 0.25
After 1 bet(s), total: 0.5
After 2 bet(s), total: 1.0
Number of bets: 2
Final amount: 1.0

% java DoubleOrNothing 100 1000
Number of bets: 0
Final amount: 1000.0

% java DoubleOrNothing 1000.00 1000.00
Number of bets: 0
Final amount: 1000.0
In the above runs, what are the lines that start with the percent symbol? From now on, this is how we describe the input given to each example. After java comes the names of the class containing the main() method that will be run. After this comes any command line arguments. This is what you would type in if you wanted to run your program from the command line (e.g. a DOS prompt in Windows). We'll learn how to run things from the command line soon.

Do I really need to worry about strange cases like the last three? Yes. While these cases may seem silly in our imaginary problem, you should always pay attention to such things when developing software. Failure to handle such tricky cases can result in all sorts of bugs such as your program crashing or going into an infinite loop.

Do I need to use Math.random() or anything? No. You can assume you win every bet placed. There is no randomness in this program.

My program crashes if one or both of the arguments isn't a number (e.g. java DoubleOrNothing blah 3.14). Is that okay? Yes. We'll eventually learn how to handle such cases, but not for awhile.

Part 2: Pattern
Write a program Pattern.java that takes two integer command-line arguments N and M. You program should print an N by M "checkerboard" pattern of asterisks and periods. Each row should have 2N characters (alternating between asterisks and periods). There should be M rows of output. The first row should start with an asterisk, the second row should start with a period, and so on. If either N or M is an integer less than or equal to zero, nothing should be output. Here some example runs:
% java Pattern 4 4
*.*.*.*.
.*.*.*.*
*.*.*.*.
.*.*.*.*

% java Pattern 5 2
*.*.*.*.*.
.*.*.*.*.*
 
% java Pattern 0 2
 
% java Pattern 2 -2


Part 3: Baby simulator
A couple beginning a family decides to keep having children until they have at least one of each sex. It seemed like a good idea at the time.

a. BoyAndGirl. Write a program BoyAndGirl.java that simulates repeatedly having a child until there is at least one of each sex. Here are some example runs (note because of the random nature you are unlikely to get the same sequence of answers):
% java BoyAndGirl
Congratulations! You have 3 children.
% java BoyAndGirl
Congratulations! You have 6 children.
% java BoyAndGirl
Congratulations! You have 2 children.
Possible progress steps: b. BoysAndGirls. Write a program BoysAndGirls.java that takes an integer command-line argument T. In each of T independent experiments, simulate a couple having children until they have at least one of each gender. Use the results of the T experiments to estimate the average number of children the couple will have. Record and output the frequency counts for 2, 3, and 4 children, and also one count for 5 and above. Finally, output the most common number of children in a family (if there is a tie, print only the first most common number of children). As before, assume that the probability of having a boy or a girl is 1/2.
% java BoysAndGirls 2 Average: 6.0 children to get at least one of each sex. Number of families with 2 children: 0 Number of families with 3 children: 0 Number of families with 4 children: 0 Number of families with 5 or more children: 2 Most common number of children is 5 or more. % java BoysAndGirls 10 Average: 3.5 children to get at least one of each sex. Number of families with 2 children: 2 Number of families with 3 children: 3 Number of families with 4 children: 3 Number of families with 5 or more children: 2 Most common number of children is 3. % java BoysAndGirls 100 Average: 3.19 children to get at least one of each sex. Number of families with 2 children: 44 Number of families with 3 children: 24 Number of families with 4 children: 16 Number of families with 5 or more children: 16 Most common number of children is 2.
As T increases, we expect the average number of children per family to converge. Use BoysAndGirls to formulate a hypothesis as to what this average is. Run BoysAndGirls with T = 1, 10, 100, 100000 and 1000000 to watch it converge to a sufficiently accurate estimate. Include your experimental results and hypothesis in the header portion of BoysAndGirls.java.

Possible progress steps:
Extra credit: Zombie Apocalypse
Create a text-based zombie game called Zombie.java. The game is played starting on a 10x10 grid. You start in the upper-left corner. Your goal is to reach the safety of the mall in the lower-right corner without getting eaten.

         

Luckily it is early in the zombie apocalypse so you only have one zombie to worry about. Every turn you move north, south, east or west by typing 'n' 's' 'e' or 'w' respectively. If you type an invalid character or try and go off the grid, you lose your turn. If you are dumb enough to walk into the current position of the zombie, kiss your brain goodbye.

The zombie also moves north, south, east or west and must remain on the grid. The zombie cannot enter the mall. The zombie always chooses a valid move but chooses the direction at random (it hasn't learned to smell brains yet). If the zombie's move and your move result in the same location, say goodbye to your grey matter.

A level is completed when you reach the mall. There are nine levels and in each level the size of the grid is reduced by one (the final level is 2x2). You win if you complete all levels. Empty grid cells appear as '.', the zombie is '*', the mall is '#', and you are '!'.

How do I read in a number from the user? You need to use the StdIn.java class and place it in the same directory as your Zombie.java program. When you want your program to read in a command from the user, call the method StdIn.readString(). For an example program, see OrderProduct.java.

Extra-extra credit. There is plenty of scope for improvements such as: a smarter zombie, multiple zombies, obstacles on the grid, etc. The zombie-infected world is your oyster!

Submission. Submit all three programs DoubleOrNothing.java, Pattern.java, BoyAndGirl.java and BoysAndGirls.java via Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program. Be sure you include your results and hypothesis from Part 3b in the header of BoysAndGirls.java (this is worth points!). If you did the extra-credit, submit a single zip file containing all the files required to run your creation to the extra-credit dropbox.

Page last updated: August 16, 2016