Arrays


Arrays

ArraysEtc.java
(uses Bunny.java)
Array2DDemo.java

 

Deitel ch 7-8


Classwork


Create a project using the usual naming convention and add a class with a main.  Do all the following in the class with main.  Remember to make methods in the class with main static.

Part A

Add a method shortestFinder which takes an array of Strings using a variable length parameter list and returns the index of the shortest (Strings have a .length() method).

Test this from main with both arrays and comma separated lists.

Add a method makeMultTable, which takes an int highest as parameter and returns a 2D array of ints. It should fill out the array it returns as a multiplication table going from 0*0 to 0 * highest in the first row, and from highest * 0 to highest * highest on the last row.

Add a method totalSum which takes a 2 dimensional array of ints grid and returns the sum of all the ints in the array. Use foreach loops.

Add a method findMax which takes a 2D array of ints and returns the indices of the largest value as an array of 2 ints. It should work even if the array is jagged.

Add a void method diceRecord which will model rolling 2 dice a given number of times, timesToRoll.  In the method, create a 6x6 2D array of ints.  In a loop that runs timesToRoll times, generate two random numbers representing rolls of six sided dice, and use the 2D array to keep track of how often each combination came up (e.g., every time you roll a certain combination add 1 to the corresponding position in the array ).

After the loop, search through the array to find and report which combination of dice came up most often (if more than one had the max, it doesn't matter which you report)  (don't forget that humans think dice start at 1).  (hint: do you have a tool to do this already?)

Test all these methods from main.