Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create a new IntelliJ project.

  2. Create a new Java class that will contain the main method.

  3. Within the main method, create an array of ten elements of type int.

  4. Use the class method Math.random() to fill the array with random values. Note that Math.random() returns a value between 0.0 and 1.0, so you may need to multiply this number by some factor (i.e. 100) and cast the result to an int. (Hint: use loop to fill up the array)

  5. Print out the content of your array - again use a loop. You should see 10 random numbers ranging from 0 to 99.

  6. Try to sort your array. As you may know, there are a number of different sorting algorithms that we could use. While it is not very efficient with large arrays, the simplest one to understand is called the bubble sort. The bubble sort gets its name from the way large values tend to move towards the top of the array as multiple passes are made though the array. The number of passes required is equal to the number of elements in the array minus one. To implement the bubble-sort algorithm requires two nested loops. Basically, you need to compare each element in the array with those above it. If the value of the element is greater than any of them, it must be swapped with the lesser one.

...