Prep 2: Module 2
- Introduction to Choice and Iteration
- Roundtable: Translating Flow Diagrams into Code
- Exercise: Computing the max of two inputs
- Choice, continued
- Exercise: A bad swap
- Exercise: Heads or Tails?
- Exercise: Inches to feet
- Exercise: Divide two numbers carefully
- Exercise: A random die
- Iteration
- Exercise: Average of random values
- Exercise: Write some loops
- Roundtable: How many flips to get to 10 heads?
- The
forloop - Exercise: 100 runs of the 10 heads problem
- Exercise: N runs of the 10 heads problem
- Exercise: Average flips to get to 10 heads
- Conclusion
Introduction to Choice and Iteration
| Video | Description |
|---|---|
| Module Overview) | This video goes over what topics will be covered in this module. |
| Module Introduction | How do computers make choices? How do they repeat things? Let's find out! |
Roundtable: Translating Flow Diagrams into Code
| Video |
| Part 1 |
| Part 2 |
| Part 3 |
| Part 4 |
| Part 5 |
| Part 6 |
| Part 7 |
| Part 8 |
| Part 9 |
| Part 10 |
| Part 11 |
| Part 12 |
Exercise: Computing the max of two inputs
Part 1: Introduction
Introduction: Computing the max
Part 2: The Exercise
- Find and open the Max Java program in the exercises2 package of the exercises source folder.
- Add code as directed there to prompt the user for integer values for x and y.
- Use conditional execution to print the larger of the two values.
Part 3: Solution
Choice, continued
| Video | Description |
|---|---|
| Composition and Nesting | What happens when you use an if statement inside an if statement? |
| More Examples | More examples of using choice in Java |
Exercise: A bad swap
Part 1: Introduction
Part 2: The exercise
- Find and open the BadSwap Java program in the exercises2 package of the exercises source folder.
- Run the code if you like, providing various values for the inputs.
- Fill in the comments to explain why the provided code does not swap the values of x and y.
Part 3: Solution
Exercise: Heads or Tails?
Part 1: Introduction
Part 2: The Exercise
- Find and open the CoinFlip Java program in the exercises2 package of the exercises source folder.
- Complete the code so that it prints heads or tails on each run of the program, with equal probability.
Part 3: Solution
Exercise: Inches to feet
Part 1: Introduction
Part 2: The exercise
- Find and open the FeetInches Java program in the exercises2 package of the exercises source folder.
- Prompt the user for some number of inches.
- Produce output that corresponds the number of feet and any leftover inches when converted from the specified input.
-
Be sure your output:
- Uses the word feet for plural and foot for singular number of feet.
- Uses the word inches for plural and inch for singular number of feet.
For example:
- 13 inches will produce: 1 foot and 1 inch
- 17 inches will produce: 1 foot and 5 inches
- 28 inches will produce: 2 feet and 4 inches
Part 3: The solution
Exercise: Divide two numbers carefully
Part 1: Introduction
Introduction: Divide two numbers carefully
Part 2: The exercise
- Find and open the CarefulDivide Java program in the exercises2 package of the exercises source folder.
- Prompt the user for an integer numerator and denomiantor
- Compute the integer quoient of the inputs.
- However, if the denominator is 0, do not print the quotient. Instead, produce a suitable message about the result being undefined.
Part 3: Solution
Solution: Divide two numbers carefully
Exercise: A random die
Part 1: Introduction
Part 2: The exercise
- Find and open the Die Java program in the exercises2 package of the exercises source folder.
- As directed in the comments, write code so that your program prints an integer between 1 and 6, inclusively, with equal likelihood of each possible result.
Part 3: Solution
Iteration
Exercise: Average of random values
Part 1: Introduction
Introduction: Average of random values
Part 2: The exercise
- Find and open the RandomAverages Java program in the exercises2 package of the exercises source folder.
- As directed in the comments, write code so that your program computes the average of 1, 2, 3, …, 1000 values.
- Some questions are posed in comments. Write your answers in comments below those questions.
Part 3: The solution
Solution: Average of random values
Exercise: Write some loops
Part 1: Introduction
Introduction: Write some loops
Part 2: The exercise
- Find and open the Loops Java program in the exercises2 package of the exercises source folder.
- Follow the comments to write loops that produce the specified outputs.
- If you get stuck, watch enough of the solution video to see how to accomplish one task, and then try the next task on your own.
Part 3: The solution
Roundtable: How many flips to get to 10 heads?
| Video |
| Part 1 |
| Part 2 |
| Part 3 |
The for loop
Exercise: 100 runs of the 10 heads problem
Part 1: Introduction
Introduction: 100 runs of the 10 heads problem
Part 2: The exercise
- Find and open the Heads Java program in the exercises2 package of the exercises source folder.
- Using a for loop, run the experiment already in the Heads program 100 times.
Part 3: The Solution
Solution: 100 runs of the 10 heads problem
Exercise: N runs of the 10 heads problem
Part 1: Introduction
Introduction: N runs of the 10 heads problem
Part 2: The exercise
- Continue modifying the Heads Java program in the exercises2 package of the exercises source folder.
- Instead of running the experiment 100 times, prompt the user for a value N and run the experiment N times.
Part 3: The solution
Solution: N runs of the 10 heads problem
Exercise: Average flips to get to 10 heads
Part 1: Introduction
Introduction: Average flips to get to 10 heads
Part 2: The exercise
- Continue modifying the Heads Java program in the exercises2 package of the exercises source folder.
- With the experiment running N times, modify your program so that, at the end, it prints the average number of coin flips required to achieve 10 heads.
Part 3: Solution
Solution: Average flips to get to 10 heads