Main Page

Previous Next

Exercises

  1. Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values between 0.0 and 100.0. Display the names of each month along with the corresponding decimal value. Calculate and display the average of the 12 decimal values.

  2. Write a program to create a rectangular array containing a multiplication table from 1x1 up to 12x12. Output the table as 13 columns with the numeric values right aligned in columns. (The first line of output will be the column headings, the first column with no heading, then the numbers 1 to 12 for the remaining columns. The first item in each of the succeeding lines is the row heading, which ranges from 1 to 12.)

  3. Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order. Display the sorted list of words. You could use a simple sorting method called the bubble sort. To sort an array into ascending order the process is as follows:

    Starting with the first element in the array compare successive elements (0 and 1, 1 and 2, 2 and 3, and so on).

    If the first element of any pair is greater than the second, interchange the two elements.

    Repeat the process for the whole array until no interchanges are necessary. The array elements will now be in ascending order.

  4. Define an array of ten String elements each containing an arbitrary string of the form "month/day/year", for example,"10/29/99" or "12/5/01". Analyze each element in the array and output the date represented in the form 29th October 1999.

  5. Write a program that will reverse the sequence of letters in each word of your chosen paragraph from Exercise 3. For instance, "To be or not to be." would become "oT eb ro ton ot eb."

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor