One Dimensional Array

In this blog post I will share my answer to the programming activity in Chapter 8 of Java Illuminated Fourth Edition.

0

This is the class diagram of the program. The BarChart class mainly focus on the graphics and presentation of the array. The ArrayPractice1 class includes the exercises, and it also uses the BarChart object. Here are the exercises.

Exercise #1

Prints the array to the console with elements separated by a space. The instance variable arr is the integer array to be printed.

Answer:

public void printArray( )
{
    for ( int i = 0;i < arr.length; i++ )
    {
        System.out.print (arr[i] + ” “);
        animate( i );
    }
}

Explanation:

According to the question, I need to print every element in the array. The only way that we can go through the elements in the array is by using index. I used a for loop and use “i” as the index. I print out the element of index “i” and add a space after it. After that, i is incremented so that the next element will be printed.

Output:

Picture1

Exercise #2

Sets all the elements in the array to parameter value. The instance variable arr is the integer array to be processed.

Answer:

for ( int i = 0;i < arr.length; i++ )
{
    arr[i] = value;
    animate( i );
}

Explanation:

Using another for loop, I assign the integer “value” to every element in the array. If, in the future, we need to assign different value to the array, we can use the index “i” the differentiate the values.

Output:

2

Exercise #3

Counts number of elements equal to parameter value. The instance variable arr is the integer array to be processed.

Answer:

int fre = 0;
for (int i = 0; i< arr.length ; i++){
    if (arr[i] == value){
        fre ++;
    }
    animate( i , fre );
}
return fre;

Explanation:

To count the frequency, we need to declare an integer variable outside the for loop so that when the loop ends the value will not be deleted. Then, for each time the element equals the value, the frequency variable increments.

Output:

3

Exercise #4

Finds and returns the minimum value in arr. The instance variable arr is the integer array to be processed.

Answer:

int min = arr[0];
for (int i = 0; i < arr.length ; i++){
    if (arr[i] <= min){
        min = arr[i];
    }
animate( i, min );
}
return min;

Explanation:

To find the minimum value, we need to first declare a variable that holds the min value. Then, in the for loop, overtime we find a value smaller than the min value, we update the min variable by assigning the value of that element into it. The for loop will go through the array and compare every value with the current minimum one.

Output:

4

Conclusion: Real Life Implication

The array is very useful since we often need to analyse data and present data in a organised way. These exercises help me to familiarise myself with one dimensional array and the methods related to it. In real life, we can use one dimensional array in statistic, and use this program to present the statistic. Also, we can use array to search in a big bunch of data, and also to present a large amount of data efficiently. We can also incorporate array in GUI, which enables us to present the array visually.

 

Leave a comment

Website Built with WordPress.com.

Up ↑