You are on page 1of 5

Additional Exercises (Primitive Type Arrays)

Question 1:
Create the following application class called ArrayStuff by following the UML class diagram
and class description table.

ArrayStuff

+ main(String[]): void
+ displayNumbers(int[]): String
+ countEven(int[]): int
+ sortArray(int[]): void
+ determineSameDigits(int): boolean

Static method Description


main(String[]) Complete the following steps in the main() method:
 Declare and initialize and array called theNumbers that will store 15 int
numbers.
 Populate the theNumbers array with random numbers ranging from 10 to
99, including 10 and 99.
 Invoke the displayNumbers() method to get a concatenated String of the
array values. And display the concatenated String.
 Invoke the countEven() method to determine the number of even number in
the array. Display this value.
 Invoke the sortArray() method to sort the numbers of the array and display
the highest number of the array.
 Run through the theNumbers array and invoke the determineSameDigits()
method to determine if the number in the array have similar digits. You
need to count the numbers with similar digits and display this count.
displayNumbers(int[]) The method will receive an array of int values.
It will create a concatenated String, with one value per line. This concatenated
String will then be returned by the method.
countEven(int[]) The method will receive an array of int values.
It will then determine how many of the numbers are even and return this count.
sortArray(int[]) The method will be used to sort the parameter array elements in descending
order making use of the Bubble Sort Technique.
Remember that arrays are passed by reference. Meaning whatever changes you
make to the parameter array in the method, will reflect in the array from where
it was called.
determineSameDigits(int) The method will receive a two‐digit number as a parameter.
It will then determine of the two digits of the number are the same and return
true if it is, otherwise return false if it is not.
Example:
55 is the same (true)
But 36 is not the same (false)
Question 2:
This question is about parallel arrays. We have the following arrays:
studentNumbers of type String:

Elements 123456 456789 123789

surnames of type String:

Elements Steyn Baloyi Gadebe

names of type String:

Items Francois Albert Mpho Precious Moses Guilty

Take note that a student can have more than one name, but it is still one element in the
array. The names are separated by a space. You will have to look at the split() method of the
String class.
Make use of one for loop to print the information of each student to look like this:

Student number:
Surname:
Initials:
Question 3:
Create an application called CoronaStats as shown in the UML class diagram and explained
in the description table. The application will be used to look at the Corona Virus statistics in
the provinces in South Africa.

CoronaStats
+ main(String[]): void
+ recoveryRate(String, String[], int[], int[]): double
+ infectionsLessThan(int, int[]): int
+ lowestInfections(String[], int[]): String

Method Description
main(String[]) Complete these steps in the main method:
 Declare and populate the provinces array that stores the names
of the 9 provinces of South Africa ("Limpopo", "Mpumalanga",
"Gauteng", "North West", "Northern Cape", "Free State",
"Kwazulu Natal", "Western Cape", "Eastern Cape").
 Declare and populate the infections array that is a parallel array
to the provinces array that stores the number of infections for
each province (8562, 6687, 16589, 3874, 1005, 8333, 15578,
16368, 13698).
 Declare and populate the recoveries array that is a parallel array
to the provinces and infections arrays that stores the number of
recoveries for each province (8000, 6300, 14500, 3200, 850,
7956, 14975, 15974, 12879).
 Let the user enter the name of the province for which to
determine the recovery rate.
 Invoke the recoveryRate() method with the required arguments
and display the recovery rate of the specified province as a
percentage.
 Generate a random number in the range of 5000 to 15000 to be
used as the value to determine the number of provinces with
infections less than this value.
 Invoke the infectionsLessThan() method with the required
arguments and display the number of provinces with infections
less than the specified value.
 Invoke the lowestInfections() method with the correct
arguments and display the province with the least number of
infections.
recoveryRate(String, String[], int[], int[]) The method is used to determine and return the recovery rate of a
specified province.
The method will receive as parameters the specified province name as
well three parallel arrays.
Determine as a percentage the recovery rate of the specified province
and return this value.
infectionsLessThan(int, int[]) This method will count and return the number of provinces that have
infections less than the specified value.

The method will receive as parameters the specified value to compare


with as well as the array with all the infections.
lowestInfections(String[], int[]) Making use of the Bubble Sort Technique, you must find the province
with the lowest number of infections and return the name of this
province.

The method will receive as parameters the names as well as the


infections arrays.

You might also like