Introduction |
Please print a copy of this assignment, read it carefully, and
highlight material you think will be useful to you while you
are working on the program or submitting it.
This programming assignment is designed to ensure that you know how to write programs that combine the standard control structures in Java: blocks, ifs, for loops, and break statements (to terminate for loops). You will also continue gaining experience with the more basic Java features: declarations and expression statements that use arithmetic, relational, logical, textual, and state-change operators -including lots of logical/boolean expressions. Finally, you will practice writing, testing, and debugging programs using iterative enhancement: a divide-and-conquer technique for building and testing programs a bit at a time. You will write three programs in this assignment. This time, your programs must check whether some (not all) inputs are valid (and reprompt the user for them -or discard them- if they are not). As always, you can check the behavior of your programs against mine by downloading my executable zip file Program #2 Executables and unzipping it. See Program #1) for details on how to run these executables on both PCs and in Eclipses (PCs and Macs). Remember, you can run these programs, but not examine their source (Java) code. Copy the input/output form of the executable programs in the programs that you write: use exactly the same prompts and messages. For the Darts and MontyHall programs, there are executable versions for the kernel and each enhancement. For your information, I am listing below the number of lines in my solution programs. These programs are formated in the standard way. I am counting only lines with code (even if the only code on the line is a brace that closes a block); but I am not counting blank lines norlines filled with comments. My "darts" program is 24 lines; my "increasing" program is 30 lines; my "monty hall" program is 51 lines. Your programs might be smaller, and they might be larger; but if your program starts going over 2-3 times the size of mine, you might want to rethink it (or come get some help). Please declare all variables to have their natural type: if some variable always stores integral values, declare it to be an int. If later in the program you need to use it as a double, cast it there. Please follow the instructions below for each program: finish each enhancement before continuing to the next one (including printing whatever messages it displays in the console, copied exactly). Please use the infinite for loop and if/break statements to write all loops; when you are finished, you may simplify these loops. As we improve our programming abilities, we should improve our programming style too (which will be part of the grading criteria for upcoming programming assignments). In each of these programs, please pay particularly close attention to the following style principles (discussed in the lecture on Coding Style).
To work on this assignment, create one Java project (call it Program2) and create three new Java classes in it (as you did for one class in Program #0). Each class will contain a program that you will write to solve one problem; name the classes Darts, Increasing, and MontyHall. Write, run, and debug each class/program as you did in Program #1. When you finish each part, submit its .java file. Only one programmer of the pair should dropoff the programs: the same one for each part. It doesn't matter which of the pair submits, but that person should submit all the parts. Of course, each program should contain both student names (in the comment: the same one you cut, pasted, ane filled in at the top of each program in Program #1). |
Calculating π with Darts |
Write a program that performs the following tasks.
To simulate throwing darts, write an expression that returns a double in the range -1.0 to +1.0 that calls the Math.random method, which returns a result between 0.0 and 1.0, and use this expression when generating the x and y coordinates. Doing so ensures that both coordinates are within the square. Use multiplication and addition/subtraction to linearly "grow" and "shift" the range of the Math.random function to the desired range for each coordinate. Use a boolean expression to determine whether or not the coordinates of the dart are inside or outside the circle that is inscribed in the square (remember some geometry). Design, code, test, and debug this program using iterative-enhancement, as 6 mini-projects. Test each project to ensure that it is correct before proceeding to the next enhancement. This is the same methodology that we will use for larger programs; so, it is a good idea to practice this technique here, where the program is small, even if you can write the entire program all at once. Later, we will discuss this technique in greater depth. Before starting to write your program, run my executable a few times to familiarize youself with its input and output.
Hand in ONLY THE FINAL ENHANCEMENT of the program: the one meeting the complete specifications, with intermediate output statements removed. Test it by throwing 10; 100; 1,000; 10,000; 100,000, 1 million, and 10 million darts (try a few billion, if you have a fast computer). Also, try the same number of darts more than once and observe that the computed value changes, because the darts are thrown randomly each time. Typically, the more darts thrown, the better the answer approximates the true value of π (pi). |
Increasing Sequences |
Write a program that performs the following tasks.
We will use two int variables to keep track of the previous value entered and the current value entered, to determine if the two values in the sequence is increasing. Before entring a new "current" value, the old "current" value must be stored in the previous variable (see step 3 below). This idiom is useful in many programs. Design, code, test, and debug this program using iterative-enhancement, as 7 mini-projects. Test each project to ensure that it is correct before proceeding to the next enhancement. This is the same methodology that we will use for larger programs; so, it is a good idea to practice this technique here, where the program is small, even if you can write the entire program all at once. Later, we will discuss this technique in greater depth. Before starting to write your program, run my executable a few times to familiarize youself with its input and output. Test your program (and mine) by entering the values specified below. You can use short input sequences to test the early enhancements to ensure that they work correctly; you must use longer input sequences to test the latter enhancements. Don't just use my test inputs; construct your own test inputs as well.
Test your program (and mine) on short and long sequences of numbers, with different patterns of increasing and decreasing values. Think of boundary cases, for example where the first two values entered aren't increasing (different from all the examples above) and verify that your program computes the right answers. |
Monty Hall Let's Make a Deal |
In the 1970s, a popular TV game show was "Let's Make A Deal", whose host was
Monty Hall.
A typical contestant would be shown three doors: behind one door was a
valuable prize; behind the other two doors were "goats" (worthless prizes).
The contestant would first pick a door.
Then, Monty would often show them a goat prize behind one of the other doors;
no matter what door the contestant picked, there was always one other door
with a goat prize behind it for Monty to show them.
Next, Monty would often ask the contestant whether or not he/she wanted to
keep the door they originally chose, or switch to the other door (the one
not shown by Monty).
Is there an advantage to staying with your door? Is there an advantage to switching doors? Are both options equally good (in terms of how likely the contestant will win the valuable prize)? You could watch many shows and keep statistics on what strategy the contestant used and whether he/she won or lost with that strategy. Or, we can write a program that can simulate very many games, to determine if there is a best strategy, and what it is. Although such a computer simulation will tell you which strategy is best, it won't tell you why that strategy is best. You may also want to use your reasoning powers to deduce the correct answer before running my/your program, but this problem is notoriously difficult to solve correctly. Marilyn vos Savant (she writes the column, "Ask Marilyn", in Parade Magazine; she supposedly has the highest IQ of anyone in the US) discussed this problem in her column in the early 1990s and it generated a huge volumne of mail, many from PhDs in mathematics who (incorrectly) disagreed with her analysis. If you do a web search, you'll find lots written about the Monty Hall problem. But before you do, try to come up with your own solution to this problem. In fact, it appears in the following clip from the movie 21; the student supplies the answer but does not justify the solution. You can also see various episodes of Let's Make a Deal on You Tube. What is interesting here is that different people can each effectively argue that their solution is correct (because the problem is subtle, and many wrong solutions sound right). Once someone has formulated a solution, it is difficult to convince them that they are wrong. Yet, we can write a simple computer program to determine which, if any, strategy is best: even given such evidence, people have a hard time "giving up" on an incorrect solution. So, you will write a program that prompts the user for the strategy to use (switch to the remaining door or stay with the door you chose originally), the number of games to play (accept only numbers greater than 0), and whether to output a detailed trace of the program; then it simulates playing that many games, keeping track of how often a player with the chosen strategy wins. The program prints the statistics it collects (the number of times the player won and lost) at the end: from these numbers, you can deduce which strategy is best (if there really is a significant difference).
To simulate choosing doors, a random door is chosen as the "good prize" door,
and a random door is chosen as the "contestant's door", and then Monty
chooses the door to expose: it can't be either the good prize or chosen
door.
Cut/paste the following code after the public class MontyHall
but before public static void main(String[] args)
When you need to choose a random door, call this method as getRandomDoor() and store the result of each call in a variable (e.g., prizeDoor, chosenDoor, exposedDoor) for use in subsequent statements in the program. Math.Random() returns a value in [0,1) the expression, before it is converted to an int has a value in [1,4)]. By converting to an int, either 1, 2, or 3 is the result (it truncates down, and 4) means all numbers up to but not including 4 can be generated), each with equal probability. Design, code, test, and debug this program using iterative-enhancement, as 7 mini-projects. Test each project to ensure that it is correct before proceeding to the next enhancement. This is the same methodology that we will use for larger programs; so, it is a good idea to practice this technique here, where the program is small. Soon, we will discuss this technique in greater depth. Before starting to write your program, run my executable a few times to familiarize youself with its input and output. Run the executable using a detailed trace, so you can better understand this program as well.
|