Discussion Section 6: General Questions and Arrays

Due: 10pm Tuesday February 27

Purpose: In this discussion section there will be no quiz. Instead, we will spend the first 10-20 minutes to go over general questions you might have about Java. After that you will write a simple program to practice defining and using arrays of integers and objects.



You will write a Java class TestArray. Inside this class you will define a method called arraySum which takes as an argument an array of integers and returns the sum of the values of all the elements of the array. The signature of the method is the following:

static int arraySum(int[] anArray)

Inside the main method, the user should be asked to provide the preferred size for an integer array that is about to be created. The elements of the array should be initialized to randomly generated integer numbers from 1 to 10. You should then call arraySum to calculate the sum of all the elements of that array and return it. After the call, you should print the elements of the array and their sum.

After this, inside the main method you should define the following array of String objects:

String[] words = {"Car", "Hi", "Bag", "High", "Bat"};

You should then create a new String named message and you should set its value to be the concatenation of the last letter of each of the words of the words array.
Of course, this should be done programmatically, which means that you should use a loop to iterate over the elements of the words array and also use the appropriate String methods to extract the last letters of the words and create the value of message. Finally, you should print the value of message.

Your program should be internally correct (sound logic) and externally correct (following Java style guideline).