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
Create a new IntelliJ project.
Create a new Java class that will contain the
main
method.Within the
main
method, create an array of ten elements of typeint
.Use the class method
Math.random()
to fill the array with random values. Note thatMath.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 anint
. (Hint: use loop to fill up the array)Print out the content of your array - again use a loop. You should see 10 random numbers ranging from 0 to 99.
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
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 amain
method where you will experiment with yourPerson
class.In the
Person
class, declare an instance variable of typeint
for the person's age and another variable of typeString
for the person's name.Create a constructor to initialise both instance variables.
In the
PersonTest
class, within the main method, declare a variable calledpeople
that will be able to reference an array of objects of typePerson
. Create the array called people referencing as manyPerson
objects as there are names in the suppliedtestNames
String
array.Fill the array with
Person
objects. Use thetestNames
andtestAges
to help you create the Person object. Hint: use one loop to go through all three arrays.Print out the content of your
people
array - you’d need to use the instance methods to access the information.
Task 3 - Sorting
In the
bSortByAge()
method in the person class, use a bubble sort algorithm (as above) to sort thePerson
objects by age in ascending order.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()
).Now it’s time to sort the
Person
objects in the array by name. Instead of comparing the twoPerson
objects ages you will compare their names. Remember that classString
has a very usefulcompareTo
method that returns anint
. In thebSortByName()
method, simply copy thebSortByAge()
method and change theif
statement that does the comparison.Test that your sorting works in the
main
method.
Task 4 - Optional
As an alternative to make your
if
statement in thefor
loop shorter write acompareTo()
method in yourPerson
class that performs a similar function to theString
methodcompareTo()
. This method should take a single argument, which is a reference to aPerson
object and return anint
and only needs to be one line of code!Change the
if
statement of the for loop to use yourPerson
classcompareTo()
method.