Assignment 5: Can’t spell functions without fun

Due Thursday, October 20, before midnight

The goals of this assignment are:

• Write programs containing multiple functions

• Work with loops, conditionals, and variables

credit: Jeffrey Knerr

In the file called `Menu.java` implement a static function called `menu` that has the following function signature

``````// Given a list of Strings (user options), displays the strings in a numerical
// param opts (String[]): list of String representing all options
// return (int): the id of the option chosen by the user. id is in range [0, opts.length-1].

This function should work with any list of string options. For example, if the list of options is `["yes","no"]`, your function will display a simple menu with two options:

`````` 1. yes
2. no``````

and then ask the user to choose one of the options (1 or 2). Similarly, if the list of options is `["sit","stay","bark"]`, your function will display:

`````` 1. sit
2. stay
3. bark``````

Your function needs to work for any size String[] array (2 options, 5 options, 27 options, etc), and should return an integer in the range `[0, opts.length]`.

Furthermore, your function needs to check that the user entered a valid number. You should re-use your `IsInteger` function and use it to test whether the user provides a value within the valid range.

Once you know you have an integer, make sure the user enters a number from the displayed menu, asking again if they enter a number too high or too low. Once the function gets a valid number, it should return that number as an integer to `main()`.

Here’s an example of the full program, with the user entering invalid integers at first. Note how the function continues to ask again and again until the user enters a valid choice (hint: use a `while` loop for that part).

``````$javac Menu.java CheckInput.java$ java Menu
1. yes
2. no

You chose: 1``````

In `Menu.java`, use the following simple `main()` function to test your menu function.

``````public static void main(String[] args) {
String[] opts = {"yes","no"};
System.out.println("You chose: " + choice);
}``````

Once your function is working, please add at least one additional option ("maybe"?) to `opts` in `main()` to make sure your function works with more than two options.

``````$java Menu 1. yes 2. no 3. maybe your choice -->`````` Patterns credit: Jeffrey Knerr This program will use various functions to draw patterns in the terminal window. We’ll write and test each function as we go, then use the `menu(opts)` function from above to allow the user to pick which function to run. Start by writing and testing each of the following functions (in `Patterns.java`). Block Implement the `block` function according to the following specification ``````// Draw an n by n block to the console // param n (int): the size of the block // param ch (char): the character to fill the block public static void block(int n, char ch)`````` This function takes a number (`n`) and a character (`ch`) and prints a block of nxn characters. For example, calling `block(5,'X')` would display this to the terminal: ```XXXXX XXXXX XXXXX XXXXX XXXXX``` and calling `block(10,'Q')` would display a 10x10 block of Qs. In `Patterns.java` add your `block(n,ch)` function and call it with a simple `main()` function to test that it works: ``````public static void main(String[] args) { int n = Integer.parseInt(args[0]); block(n,'X'); }`````` Triangle Implement the `triangle` function according to the following specification ``````// Draw a right triangle with height n to the console // param n (int): the height of the triangle // param ch (char): the character to fill the shape public static void triangle(int n, char ch)`````` Similar to the `block()` function, this function takes a number and a character, and displays a triangle pattern. Calling `triangle(7,'Y')` would display this: ```Y YY YYY YYYY YYYYY YYYYYY YYYYYYY``` In `Patterns.java` add your `void triangle(int n,char ch)` function and call it from `main()` to test that it works. Reverse Implement the `reverse` function according to the following specification ``````// Draw a reverse right triangle with height n to the console // param n (int): the height of the triangle // param ch (char): the character to fill the shape public static void reverse(int n, char ch)`````` Similar to the `triangle()` function, this function takes a number and a character, and displays a reverse triangle pattern. Calling `reverse(5,'P')` would display this: ``` P PP PPP PPPP PPPPP``` In `Patterns.java` add your `void reverse(int n,char ch)` function and call it from `main()` to test that it works. Diamond Implement the `diamond` function according to the following specification ``````// Draw a diamond height 2*n to the console // param n (int): the half-height of the diamond // param ch (char): the character to fill the shape public static void diamond(int n, char ch)`````` Last one! Calling `diamond(5,'J')` would display this: ``` JJ JJJJ JJJJJJ JJJJJJJJ JJJJJJJJJJ JJJJJJJJJJ JJJJJJJJ JJJJJJ JJJJ JJ``` Notice that, for the above example, the first 5 lines just look like the output of `reverse()` and `triangle()` put together. You can’t call them together to make those 5 lines, but you can use code similar to what’s in those functions to display the first 5 lines. Then the bottom 5 lines can be done with a separate `for` loop. In `Patterns.java` add your `void diamond(int n, char ch)` function and call it from `main()` to test that it works. Menu Finally, call your `int menu(String[] opts)` function from the previous program to `Patterns.java`. Then rewrite `main()` to allow the user to pick which pattern to draw, or to quit the program. If the user decides to display a pattern, pick the number (`n`) and the character (`ch`) using the random library. Make the number from 5-15, and the character an uppercase letter from A-G.  To compute a random number, choose an integer from 0 to 25 inclusive and then add it to the character `a`. Your final program should allow the user to continue selecting patterns to draw, until they select the "quit" option. Here’s one possible run of the final program: ``````$ javac Patterns.java Menu.java CheckInput.java
\$ java Patterns
1. block
2. triangle
3. reverse
4. diamond
5. quit

CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC

1. block
2. triangle
3. reverse
4. diamond
5. quit

F
FF
FFF
FFFF
FFFFF
FFFFFF
FFFFFFF
FFFFFFFF
FFFFFFFFF
FFFFFFFFFF
FFFFFFFFFFF

1. block
2. triangle
3. reverse
4. diamond
5. quit

BBBBBB
BBBBBB
BBBBBB
BBBBBB
BBBBBB
BBBBBB

1. block
2. triangle
3. reverse
4. diamond
5. quit

GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG
GGGGGGGGG

1. block
2. triangle
3. reverse
4. diamond
5. quit

A
AA
AAA
AAAA
AAAAA
AAAAAA
AAAAAAA
AAAAAAAA
AAAAAAAAA
AAAAAAAAAA
AAAAAAAAAAA
AAAAAAAAAAAA
AAAAAAAAAAAAA
AAAAAAAAAAAAAA
AAAAAAAAAAAAAAA

1. block
2. triangle
3. reverse
4. diamond
5. quit

 You can call `javac *.java` to compile all the java files in your current directory.
1. The programs, `CheckInput.java`, `Menu.java` and `Patterns.java`
1. Copy your files to your dropbox, into the folder called `A05`.