CS 49J - Lab 3

Cover page image

Cay S. Horstmann

Lab Rules

Part 1. Array One-Liners

Suppose that str is a String, arr is an int[] array, and arr2 is a two-dimensional array of int[][], write one-liners to accomplish the following tasks. No loops!

Try them out in the Java REPL, jshell, or a small test program. Paste into the lab report what you tried. Look into the textbook, and the API docs of the String and Arrays classes.

  1. Get the middle element of the array arr (if the length is odd)
  2. Get an array of the two middle elements of the array arr (if the length is even)
  3. Sort the array arr.
  4. Get the smallest value from arr.
  5. Print the array arr. (Hint: arr.toString() doesn't work.)
  6. Print the array arr2.
  7. Make a copy of arr.
  8. Grow arr so that it becomes twice as long.
  9. Make an array of 1000 zeroes
  10. Make an array of 1000 ones. Remember—no loops!
  11. Check whether all entries of arr are zero.
  12. Get an array of all Unicode code points from str.
  13. Turn an array of Unicode code points into a string.
  14. Get an array of all words in str (e.g. "Mary had a little lamb" -> ["Mary", "had", "a", "little", "lamb"]). Hint: split.
  15. Make a string from an array of UTF-8 bytes. (You can get such an array by calling Files.readAllBytes(Paths.get("input.txt")).)

Ask if you need hints!

Part 2. Random Numbers

To get random numbers, create an object of type Random:

Random generator = new Random();

Then call

int n = generator.nextInt(numChoices);
double x = generator.nextDouble(); // 0 ≤ x < 1

to get random integers or floating-point numbers.

  1. Which package do you need to import? Look at the Random API. (Note: If you use jshell or javarepl, you can smugly answer “none”—they already import it for you.)
  2. How do you make a random integer between 1 and 6?
  3. How do you make a random floating-point number between -1 and 1?
  4. Check how random the random generator is. Write a test program (or a snippet in jshell) that makes 1,000,000 random numbers between 0 and 9 and checks how often each of them occurs. What is your program? Hint: Use an array of ten counters.
  5. In the preceding problem, what is the spread (i.e. largest counter - smallest counter)? What's a two-liner to get the answer?
  6. (optional) One way to compute pi is through random simulation. Generate random floating-point numbers x and y between -1 and 1. Check whether the point (x, y) lies within the unit circle, i.e. x2 + y2 ≤ 1. If so, call it a hit. Then the ratio hits/tries is approximately π /4 (because the square in which x and y are located has area 4.) Write a program that computes π from 1,000,000 tries.
  7. (optional) An easy way to produce random directory names or passwords is to make a random BigInteger and print it in base 36. Write a program that produces ten such random strings, using random big integers of 200 bits. Look up how in the BigInteger API. (Trivia fact: There are less than 2200 atoms on earth.) What is your code? (Can be snippets in jshell or a complete program.)
  8. (optional) Write a program that reads the number of strings and the number of bits from the command line. For example,
    java Passwords 10 200
    should produce the same result as the previous exercise.