CS 149: Introduction to Programming
James Madison University, Spring 2019 Semester

Homework 7: Arrays and References

Objectives

Note: CodingBat is a free site of live problems to build skill in Java and/or Python. It was created by Nick Parlante, who is Computer Science lecturer at Stanford. If you create an account, CodingBat will automatically save your progress online.

For this homework, you might find (on CodingBat's site) the Java Arrays and Loops page useful.

Exercise 7.1   CodingBat Array-2

This exercise is based on the infamous "FizzBuzz" interview question apparently used to screen applicants for programming jobs. Years ago, there was a blog post that went viral when the author claimed "the majority of comp sci graduates can't [solve FizzBuzz]." Many tech blogs responded in disbelief (see, e.g., this one), and there was a flurry of discussion all over the Internet. See if you can prove them wrong!

Before you begin, take a look at the Array-1 problems. Many of them can be solved with one line of code, and none of them require loops. You might want to solve several of them as a warm-up to this exercise.

The Array-2 problems are more a bit more challenging, because they each require iterating over an array (with one loop). For this exercise, solve the following problems:

Complete these problems online first, until you get them 100% correct. Then download the BatArray2.java source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.

Exercise 7.2   Basic Statistics

When students look at their homework or exam scores, they often want to know how they did compared to the rest of the class. Professors usually release statistics like mean and standard deviation to summarize the range of scores. For this exercise, implement the following two methods in a source file named Statistics.java:

Be sure to test your solution multiple times with different inputs. For best results, create a JUnit test case named StatisticsTest. Or you can just create a Test.java file with a main method. Either way, don't submit to Autolab without testing!

Exercise 7.3   Word/Line Count

The GNU/Linux operating system comes with many built-in utilities for getting real work done. For example, imagine you had to analyze thousands of files as part of a digital forensics investigation. One utility you might use is the wc command, which prints the newline, word, and byte counts for a given file. For example, if a file contained the text "This is the first line.\nThis is the second.", the wc command would print 1 9 43 (i.e., 1 newline character, 9 words, 43 total characters).

For this exercise, you will implement a similar utility. Create a new class named WordCount that has a single method named analyze that takes a string parameter named text and returns an array of three integers. This method should count the number of newlines, words, and characters in the given string. (The return value is an array of these three counts.) Note that a "word" is any sequence of characters separated by whitespace.

Hint: You can use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

Exercise 7.4   Acronym Translate

Working in a technical field can be confusing because of all the acronyms. For example, CPU means Central Processing Unit, HDMI means High-Definition Multimedia Interface, and SMART means Self-Monitoring Analysis and Reporting Technology. Wouldn't it be nice if there was a way to translate all these acronyms automatically?

Create a new file named Translate.java that contains the following method:

public static String expandAll(String[] acronyms, String[] definitions, String text)

This method should replace every instance of an acronym in the text with its corresponding definition. For example, expandAll({"JMU", "CS"}, {"James Madison University", "Computer Science"}, "JMU CS rocks!") would return the string "James Madison University Computer Science rocks!".

You may assume that the two arrays will be the same length. You may also assume that the text will be reasonable English with correct grammar. In particular, there will be only one space between each word. Each sentence will either end with a space or a newline character. Punctuation not at the end of the sentence will always be followed by whitespace (i.e., you should be able to handle "Hi, Mom" but you won't have to handle "Hi,Mom").

Be careful not to replace acronyms in the middle of a word. For example, the string "CS uses MACS!" should expand to "Computer Science uses MACS!", not "Computer Science uses MAComputer Science!". You might find it helpful to use a Scanner to process the string one word at a time. Don't forget to test your code thoroughly before submitting to Autolab!

Exercise 7.5   Lottery Numbers

Ladies and gentlemen, welcome to the CS lottery! People from all over the world are buying tickets that may be redeemed for fabulous prizes at the JMU bookstore. There's just one problem: we need a way for generating the winning numbers. That's where you come into the story.

Create a new Java class named Lottery that has the following method:

public static int[] generatePicks(int size, int lowValue, int highValue, long seed)

This method will create an array of the requested size and fill it with random numbers between lowValue and highValue (inclusive). To generate the numbers, initialize a Random object with the given seed. Note that doing so causes the "random" numbers to be predicable so that we can test your solution.

For each position in the array, call Random.nextInt() to generate a number in the appropriate range. There's just one caveat: no number may be used more than once. If the number you generate is already in the array, you'll need to call Random.nextInt() again to get a different number. You may assume that size will be 3 or greater, and it will be no larger than the range from lowValue to highValue.

In the end, the array should be sorted in ascending order. For example, generatePicks(4, 10, 15, 0) should return the array {10, 11, 14, 15}, even though the numbers were generated in a different order. Use the Arrays.sort() method before returning the final result.