DOC PREVIEW
Princeton COS 126 - Loops

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COS126 Conditionals and Loops Activity - Section 1.31. Look up Program 1.2.5. RandomInt.java (p.33)Web Exercise 1.3.1. Write RollDie.java to simulate the toss of a fair six-sided die.Use casting to get a random integer, as in RandomInt.java./***/public class RollDie {public static void main(String[] args) {// how many sides on the die?_____________ SIDES = ___________________;// roll should be 1 through SIDESint roll = ________________________________________________________// print result___________________________________________________________________}}2. Look up Program 1.3.1. Flip.java (p.49)Web Exercise 1.3.2. Write RollLoadedDie.java to simulate the toss of a loaded six-sided die where where the 1, 2, 3, 4, and 5 appear with probability 1/8 and 6 appearswith probablity 3/8. Use if - else as in Flip.java (but there will be more choices)./***/public class RollLoadedDie {public static void main(String[] args) {// generate random double in the range [0.0, 1.0)_________________ r = __________________________________________;// compute the roll with desired probabilitiesint roll;if (________________________________________) roll = _______;else if (else if (else if (else if (else// print the roll}}13. Find PowersOfTwo.java in your book or on the website. It uses a while loop. Rewriteit to use a for loop. (answer on p. 57)/***/public class PowersOfTwo {public static void main(String[] args) {}}4. Write FiniteSum.java which takes one integer command-line argument N, and printsthe sum (1 + 2 + ... + N ). (Answer on p.57)/****/public class FiniteSum {public static void main(String[] args) {}}• Recommended Exercises: 1.3.7, 1.3.13,


View Full Document

Princeton COS 126 - Loops

Download Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?