/
07 - Strings and Arrays - Exercise

07 - Strings and Arrays - Exercise

Overview

The objective of this exercise is to practice manipulating arrays of primitives and object references.

Task 1 - Array of Primitives

  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.

for (int i = 0; i < nums.length - 1; ++i) { for (int j = i + 1; j < nums.length; ++j) { if (nums[i] > nums[j]) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } }

Can you use the above snippet and sort your array of ten integers?

Task 2 - Array of Object References

 

 

  1. Download the two files above and paste them into your project. The class called Person contains a skeleton code - constructor, two instance methods and two class methods. The other class contains a main method where you will experiment with your Person class.

  2. In the Person class, declare an instance variable of type int for the person's age and another variable of type String for the person's name.

  3. Create a constructor to initialise both instance variables.

  4. In the PersonTest class, within the main method, declare a variable called people that will be able to reference an array of objects of type Person. Create the array called people referencing as many Person objects as there are names in the supplied testNames String array.

  5. Fill the array with Person objects. Use the testNames and testAges to help you create the Person object. Hint: use one loop to go through all three arrays.

  6. Print out the content of your people array - you’d need to use the instance methods to access the information.

Task 3 - Sorting

  1. In the bSortByAge() method in the person class, use a bubble sort algorithm (as above) to sort the Person objects by age in ascending order.

  2. In the main method, after displaying the initial contents of the array, call your bSortByAge() method. Then display the new contents of the array. (You could move the display code into a new class method called, say, displayArray()).

  3. Now it’s time to sort the Person objects in the array by name. Instead of comparing the two Person objects ages you will compare their names. Remember that class String has a very useful compareTo method that returns an int. In the bSortByName() method, simply copy the bSortByAge() method and change the if statement that does the comparison.

  4. Test that your sorting works in the main method.

Task 4 - Optional

  1. As an alternative to make your if statement in the for loop shorter write a compareTo() method in your Person class that performs a similar function to the String method compareTo(). This method should take a single argument, which is a reference to a Person object and return an intand only needs to be one line of code!

  2. Change the if statement of the for loop to use your Person class compareTo() method.

 

Related content