CS 49J - Lab 3

Cay S. Horstmann
Lab Rules

- Work with a buddy (the person sitting next to you)
- Post lab report to Canvas
- Plain text
- Need not be literary masterpiece
- Both you and your buddy submit, but you can submit the same if you like
- Share your results with the class
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.
- Get the middle element of the array
arr
(if the length is odd)
- Get an array of the two middle elements of the array
arr
(if the length is even)
- Sort the array
arr
.
- Get the smallest value from
arr
.
- Print the array
arr
. (Hint: arr.toString()
doesn't work.)
- Print the array
arr2
.
- Make a copy of
arr
.
- Grow
arr
so that it becomes twice as long.
- Make an array of 1000 zeroes
- Make an array of 1000 ones. Remember—no loops!
- Check whether all entries of
arr
are zero.
- Get an array of all Unicode code points from
str
.
- Turn an array of Unicode code points into a string.
- Get an array of all words in
str
(e.g. "Mary had a little lamb" -> ["Mary", "had", "a", "little", "lamb"]). Hint: split
.
- 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.
- 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.)
- How do you make a random integer between 1 and 6?
- How do you make a random floating-point number between -1 and 1?
- 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.
- In the preceding problem, what is the spread (i.e. largest counter - smallest counter)? What's a two-liner to get the answer?
- (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.

- (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.)
- (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.