The Menu For this problem, the menu should be roughly equivalent to the following:
Welcome to the new, improved math menu!
Your options are
(0) Printing and summing sequences
(1) RAW power!
(2) Integrating Prof. Saeta's physics function
(3) Graphing sin(x)
(4) Finding the max and min of positive numbers
(9) Quit
Which option would you like?
Looping with the Menu In this problem, users should be
able to continue choosing options from the menu until they choose option #9,
which should quit the program. If a user types an invalid option, the program
should print a warning message and then continue by printing the menu and
offering the choice again. Here is a skeleton design for this kind of menu:
while (true)
{
// print menu and get input
if (userinput == 9)
{
H.pl("See you later!");
break; // this breaks out of the while loop
}
switch (userinput) ... // handle all of the other menu options
}
Option 0 For this option, print the following sequences and their sums using for or while loops. Your code should compute and print one of the sequence elements with each loop iteration and sum up those elements within the loop (no adding them up by hand, for example!). For the last sequence, you do not need to print the sum, and in this case it will be easiest to print the sequence digit-wise with "nested" for loops! Please format your output as shown (so the graders can read it easily!): include a space between each term in the sequence and include a blank line before the next one.
Sequences to print:
10 15 20 25 30 35 40 45 50 55 60 65
sum: 450
11 22 33 44 55 66 77 88 99 110 121
sum: 726
1 2 4 8 16 32 64 128 256 512 1024 2048
sum: 4095
0 3 9 12 36 39 117 120 360 363 1089
sum: 2148
1 22 333 4444 55555 666666 7777777 88888888
Option 1 This option should work exactly the same as it
did in Homework #3, problem #2, i.e., it should prompt the user for a
double, named b and an int, named p. It
should then print out the value of
.
The difference is that your code may not use the built-in Math.pow function in java. Instead, it should compute the power as repeated multiplication using a for or a while loop.
Details: You may assume that the user will type in only nonnegative integer values for p. Also, you should assume that anything, even 0, to the zero power is equal to 1.0. Your output should be a double.
Option 2 For this option, your code should prompt the user for
Hint: You may want to use a for loop that starts like this:
for ( double x=0.0 ; x<Math.PI/2 ; x = x+Math.PI/2000 )
Option 3 For this option, your program should produce the graph of the function sin(x) between x=0 and x=30 (x is in radians). To do this, you will need to extend some of the graphics code you used in Hw2Pr2 (the rectangle-drawing program), specialized to handle the graphics:
case 3: // if you're using a switch statement
{
double xmin = 0, xmax = 30, ymin = -2, ymax = 2; // coordinate limits
GrCanvas graph = G.createCanvas(); // create the window
graph.setOrigin(GrCanvas.ORIGIN_LL); // put (0,0) in the lower left
graph.updateCoords(xmin,xmax,ymin,ymax); // set the coordinates
graph.add(new GrLine(xmin,0,xmax,0,Color.green)); // the x axis
graph.add(new GrLine(0,ymin,0,ymax,Color.green)); // the y axis
// here you need a for loop to create the sine curve...
// hint: add lots of very small lines...
graph.repaint(); // this refreshes the graphics
break; // breaks out of case 3
}
You can find a working version of this code from the file-download
page to experiment with.
Option 4 This option should prompt the user to type any number of positive integers as input. The user signfifies the end of their numerical input by typing a "delimiting" 0. Then, your program should print both the largest and smallest of all the positive integers the user typed (it should not print 0 as the smallest positive integer!). You may assume that the user will type at least one positive integer, that only positive integers (strictly greater than zero) will be entered before the 0 delimiter, and that none of the numbers will be bigger than 1 billion (1000000000).
Hint: Create two variables, named min and max before you start a while (true) loop to get the user's input. With each new value check to see if you need to update the min and max or if you need to quit. As with the last homeowork, you should not use the Math.min or Math.max functions in your code this week (rather, exercise your ability to use if and appropriate comparisons to do this). When the loop has ended, print out the final min and max.
An example run Here is a single example run of the program:
Initializing the GUI ...
Welcome to the new and improved math menu!
Choose one of these options:
(0) Sequences and their sums
(1) RAW power!
(2) Integrating Prof. Saeta's crazy physics function
(3) Graphing sin(x)
(4) Finding the max/min
(9) Quit
Your choice: 1
Please input a double for a base: 5.0
Please input a nonnegative int for a power: 3
5.0 to the 3 power is 125.0
< menu here >
Your choice: 2
Please input a double for L: 10.0
Please input a double for a: 0.1
The integral is 6.357
< menu here >
Your choice: 5
Type as many positive integers as you like,
followed by a zero. I'll print the max and min.
4
10
42
1000
41
9875
3
20
0
The max was 9875
The min was 3
< menu here >
Your choice: 7
7 is not a valid option!
< menu here >
Your choice: 9
I'll count the nanoseconds until we meet again...
Submission Be sure to submit your
CS5App.java file under homework 4, problem 1.
In this problem, you'll be writing a program to simulate the game show "Let's make a deal" and its host, Monty Hall.
A situation that arose in each episode of the show went as follows: an audience member was asked to stand up and pick one of three curtains. (We'll call them Curtain #1, Curtain #2, or Curtain #3.) Behind one of these curtains was a car (or another suitably exciting prize), while behind the other two curtains were booby prizes of some sort -- we'll use Spam for this purpose.
After the contestant chose a curtain, Monty Hall would not immediately open it to see what they had won. Instead, he would open one of the two curtains not chosen to reveal a can of Spam -- of course, he would never reveal the car.
Thus, two curtains remained closed. Behind one was the car, and behind the other was the second can of Spam.
Then Monty would pose a new choice to the contestant: the contestant could stay with his/her initial curtain choice or switch to the other closed curtain. Whichever of the two remaining curtains was chosen by the contestant's choice to stay or switch, the contestant received what was behind it.
In this problem, you are to build a program that plays the role of Monty Hall, and the user will play the role of the contestant. Thus, your program should
Here is a single sample run as a guide to an appropriate interface.
Note that this single program run includes more than one of the games:
Welcome! I have an opportunity for you...
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
You may choose a curtain (1, 2, or 3): 1
All right, you've chosen curtain #1.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
Behind curtain #3 was a can of Spam!
A good thing you didn't choose that one, I guess.
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): switch
You win! Behind curtain #2 was the car!
Would you like to play again? yes
Welcome! I have an opportunity for you...
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
You may choose a curtain (1, 2, or 3): 2
All right, you've chosen curtain #2.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
Behind curtain #1 was a can of Spam!
A good thing you didn't choose that one, I guess.
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): stay
You get the Spam -- depending on your tastes, that might not be so bad...
However, the car was behind curtain #3.
Would you like to play again? no
Hints You may it find it helpful to plan out what variables you will need to handle all of the information in this program. As a starting point, here are the variables I used:
int wc, the randomly-chosen winning curtain int uc, the user's initial curtain choice int rc, the revealed curtain (not wc or uc) String swOrSt, the user's final choice to "switch" or "stay" String again, the user's "yes" or "no" to determine if they play again while loop. I would recommend a while (true) loop with an
appropriate break.
Be sure to submit this assignment's CS5App.java file under
homework 4, problem 2.
In this problem, you'll be using Problem #2 in order to empirically determine whether it is better to switch or stay when offered the choice by Monty Hall.
What this problem asks you to do is run the simulation of Problem #2 1000 times. Because a user will not be willing to actually play 1000 times, you will have to program the user's responses, rather than getting them from input. Your program should assume that the user always chooses curtain 1 and always switches when Monty Hall provides that opportunity. Then, your program should keep track of how many times (out of 1000) the choice to "switch" actually wins the grand prize. This type of simulation is called Monte Carlo simulation.
To start this problem, you may want to copy-and-paste your code from Problem
#2 into your new, Hw4Pr3 CS5App.java file. Then, remove all of
Monty Hall's banter (the printing statements) from the program except for one
simple "you win" or "you lose" statement at the very end, when the final outcome
is determined.
Replace all of the input statements with assignment statements -- have the "user" (now, in fact, being simulated by the program) always choose Curtain #1 initially and always choose switch as the choice.
Then, introduce two new variables (of type int) that will keep
track of how many times this simulated user wins the grand prize and how many
times the simulated user loses.
Enclose this simulation in a loop that will execute it 1000 times, keeping
track of how many wins and losses occur. You may want to replace the large
while loop with a for loop to do this.
After the 1000 runs of the simulation, print out the results as follows:
Totals out of 1000:
Wins: the number of wins
Losses: the number of losses
Here is an abridged example run, as a guide to a reasonable interface: You win!
You lose.
...989 more omitted...
You win!
You lose.
You win!
You win!
You win!
You win!
You lose.
You win!
You lose.
Totals out of 1000:
Wins: 682
Losses: 318
Be sure to submit your CS5App.java file under homework 4,
problem 3.
Part 1 The extra credit problem for this week (10%) is simply to write a program that prints the last sequence of Problem #1, option #0 using only a single for or while loop. The original intent of that last sequence:
1 22 333 4444 55555 666666 7777777 88888888
in Problem #1 is to use two for loops with one nested
within the other to produce the sequence. However, it is possible to compute the
terms using only a single loop without nesting. As in the original problem, no "workarounds"
where you don't actually compute the sequence term-by-term are permitted.
Part 2 The second extra credit (up to 10%) is to expand your program with graphics and sound capabilities. You may choose whatever you like, but for a concrete challenge, add a graphical interface to Problem #2, the original Let's Make a Deal Problem. You can use images, text and rectangles to create curtains, as demonstrated in the ExampleGraphics file link next to Hw4ExCr.zip on the download page. Creativity, as always, is welcome.
Important notes: