Part I One Dimensional Arrays This lab will experiment with an array. The goal i
ID: 3770446 • Letter: P
Question
Part I One Dimensional Arrays This lab will experiment with an array. The goal is to gain some experience with using an array and to see how different array operations work within a loop. 1. Attached to the end of this lab assignment, is a copy of the file ArrayStuff.java. Examine this copy then copy this code into a new Java file named ArrayStuff.java, compile the code to check for any errors. (it should compile without errors). 2. Create a driver class named Lab8.java. This driver class will contain the main( ) method used to make calls to the ArrayStuff class. Create an instances of the ArrayStuff class in the main( ). Make sure you look over the Constructor in the ArrayStuff class before you create your new instance object in the main( ). (a) How many parameters does this constructor take in? Add a call to the printArray() method in your main( ) using an instance of the ArrayStuff class you created in the main( ). Compile the code to check for any errors. Run the program. (b) Describe the output. (c) How many elements are in the array? (d) List all the legal values for the array index. (e) Where do the values for the array come from? (f) How is the random function different from others we have used in other labs? Remember to put your answers in a comment box at the end of your Lab8.java file. 3. The first method you will add is one that prints the array in reverse order. Find the header for the method printReverseArray(). Remove the comments from the front of the header and from the front of the closing curly brace for this method. The body of this method can be a copy of the body of method printArray(); however, the loop index must start at array.length - 1 and decrement to 0. Copy the body of method printArray() and paste it as the body of printReverseArray(). Now change the header of the for loop to look like the following. for( int i = theArray.length - 1; i >= 0; i-- ) Add an appropriate method call to the main()method of Lab8, compile the program, fixing any errors you might have made, and run it. Describe the output. 4. The next method you will add is a method that will return the smallest value in the array. The algorithm for this method is the similar to many algorithms for arrays: Assume that the smallest element is in the first position, and assign this element to a local variable called smallest (smallest = theArray[0]). Using a loop, look at each element of the array, and, if the current element is smaller than the value in smallest, assign the current element to smallest. After examining each element in the array, return the value of smallest. Find the method minimum() and remove the comments from the header of the method and from the closing curly brace. Then add the following code. int smallest = theArray[ 0 ]; for( int i = 1; i < theArray.length; i++ ) if( theArray[ i ] < smallest ) smallest = theArray[ i ]; return smallest; Now add the following call to the main() method of Lab8, compile and run the program, and record the output. Is the output correct? System.out.println(“The smallest element: “ + testArray.minimum()); 5. The class ArrayStuff has a method called maximum() that is that is almost the same as minimum() except that it returns the largest element of the array. Find the header for maximum(), remove the comments, and fill in the body of the method. Note that you can use the majority of the body of the method minimum(), you will have to make just a few changes to make the minimum() method work. Add an appropriate method call in the main() method of Lab8, compile and run the program. What is the output for this method? 6. We will now fill in the body for the method sum(). This method adds together all the elements of the array. The algorithm is similar to those for minimum() and maximun(): Initialize a running total with the first element of the array; use a loop to look at each element of the array; add the current element to the running total; and return the running total when the loop ends. The code for the method is the following: int total = theArray[ 0 ]; for( int i = 1; i < theArray.length; i++ ) total += theArray[ i ]; return total; Add this method and an appropriate method call to your program. (a) Compile and execute the program, and record the output. (b) What does the operator += do? 7. Notice that the class ArrayStuff has a method called sort(). This is a method that will put the array in sorted order using a sorting algorithm called a selection sort. This sorting algorithm will need the methods minPosition() and swap() to work correctly.The method swap() takes two integer parameters, x and y. These parameters represent indices in the array, and swap() will exchange the elements of the array at these two positions. In order to make the exchange, we must use a third, temporary variable. A swap algorithm is a very common algorithm, and you should both understand what it does and be able to write it from memory. The body for swap() follows. int temp = theArray[ x ]; theArray[ x ] = theArray [ y ]; theArray[ y ] = temp; To test that swap() works correctly, in the main() method of Lab8, add a call to printArray(), then a call to swap( 1, 5 ), then another call to printArray(). Compare the before and after views of the array. If swap() is working correctly, the elements in positions 1 and 5 should be exchanged and the rest of the array should be the same. 7. The method minPosition() is similar to the method minimum(). However, minimum() returns the smallest element in the array, but minPosition() returns the index of the smallest element in the array. In addition, minPosition() takes an integer parameter, which is the starting value for the loop index. The code is: int min = start; for( int i = start + 1; i < theArray.length; i++ ) if( theArray[ i ] < theArray[ min ] ) min = i; return min; Test this method by using a call to printArray(), then a call to minPosition( 0 ). Examine the output to verify that minPosition() returned the correct information. 8. Once swap() and minPosition() are working correctly, the code for sort() can be filled in. The code for the sort() method is: int j; for( int i = 0; i < theArray.length - 1; i++ ) { j = minPosition( i ); swap( i, j ); } Notice that selection sort works by dividing the array into a sorted section (which is initially empty) and an unsorted section. It successively finds the smallest element in the unsorted section and moves it to the end of the sorted section. iteration, the sorted section is increased by one position and the unsorted section is decreased by one position. Add the code for the sort() method and then test the method. Test the sort() method by adding the following two lines to the main() method of Lab8. testArray.sort(); testArray.printArray(); Examine the output visually. Is the array sorted from smallest to largest? 9. When all your methods are working correctly, comment out the method calls that you added to the main() method of Lab8 and replace your code with the following code in your main ( ). Compile and test the program: ArrayStuff test = new ArrayStuff( 20, 1000 ); System.out.println("The array is:" ); test.printArray(); System.out.println("The array in reverse is:"); test.printReverseArray(); System.out.println("The sum of the elements is: " + test.sum() ); System.out.println("The smallest element is: " + test.minimum() ); System.out.println("The largest element is: " + test.maximum() ); System.out.println("Location of smallest element: " + test.minPosition(0)); test.sort(); System.out.println(" The sorted array is:"); test.printArray(); System.out.println("That's all folks!");
import java.io.*;
import java.util.*;
public class ArrayStuff3
{
private int[] theArray;
public ArrayStuff3( int max, int random_max)
{
theArray = new int[max];
Random generator = new Random();
for( int i = 0; i < theArray.length; i++ )
theArray[i] = Math.abs(generator.nextInt()) % random_max + 1;
}
//&&&&&&&&&& PUBLIC METHODS &&&&&&&&&&&
public void printArray()
{
for( int i = 0; i < theArray.length; i++ )
{
System.out.print("theArray[" + i + "] = " + theArray[i] + " " );
if( i % 5 == 4 ){
}
System.out.println();
}
//public void printReverseArray()
//{
//-----------------> your code goes here <------------------------
//}
//public int minimum()
//{
//-----------------> your code goes here <------------------------
//}
//public int maximum()
//{
//-----------------> your code goes here <------------------------
//}
//public int sum( )
//{
//-----------------> your code goes here <------------------------
//}
//public void sort()
//{
//-----------------> your code goes here <------------------------
//}
//public int minPosition( int start )
//{
//-----------------> your code goes here <------------------------
//}
//public void swap( int x, int y )
//{
//-----------------> your code goes here <------------------------
//}
}}
Explanation / Answer
The ArrayStuff.java has been included here.
import java.util.*;
public class ArrayStuff
{
public int[] theArray;
public ArrayStuff( int max, int random_max)
{
theArray = new int[max];
Random generator = new Random();
for( int i = 0; i < theArray.length; i++ )
theArray[i] = Math.abs(generator.nextInt()) % random_max + 1;
}
public void printArray()
{
for( int i = 0; i < theArray.length; i++ )
{
System.out.print("theArray[" + i + "] = " + theArray[i] + " " );
/* if( i % 5 == 4 ){
}
System.out.println();*/
}
}
public void printReverseArray()
{
for( int i = theArray.length - 1; i >= 0; i-- )
System.out.println(theArray[i]);
}
public int minimum()
{
int small= theArray[0];
for(int i=0;i<theArray.length;i++){
if(small>theArray[i]){
small=theArray[i];
}
}
return small;
}
public int maximum()
{
int large= theArray[0];
for(int i=0;i<theArray.length;i++){
if(theArray[i]>large)
large=theArray[i];
}
return large;
}
public int sum()
{
int sum=0;
for(int i=0;i<theArray.length;i++){
sum=sum+theArray[i];
}
return sum;
}
public void sort()
{
int j;
for( int i = theArray.length - 1; i > 0; i-- ) {
j = minPosition( i );
swap( i, j );
}
}
public int minPosition( int start ){
int first=0;
for(int j = 1; j <= start; j ++) //locate smallest element between positions 1 and i.
{
if( theArray[ j ] < theArray[ first ] )
first = j;
}
return first;
}
public void swap( int x, int y )
{
int temp=0;
temp = theArray[x]; //swap smallest found with element in position i.
theArray[x]= theArray[y];
theArray[y]= temp;
}
}
The Driver class has been listed below:-
public class Lab8 {
public static void main(String [] args){
ArrayStuff test = new ArrayStuff(20,100);
System.out.println("the Array is: ");
test.printArray();
System.out.println(" ");
System.out.println(" The array in the reverse is :");
test.printReverseArray();
System.out.println(" The sum of the elements in the list is "+test.sum());
System.out.println(" The smallest element is: " + test.minimum() );
System.out.println(" The largest element is: " + test.maximum() );
System.out.println(" Location of smallest element: " + test.minPosition(0));
test.sort();
System.out.println(" The sorted array is :");
test.printArray();
System.out.println(" That's all folks!");
}
}
Questions and Answers:-
A) The constructor defined above in the ArrayStuff class have 2 parameters which takes first parameter to decide the size and the other to allocate the values into the array.
B)Now when we call the printArray(), which have the return type as void. It will simply print the array of whatever values it has generated from the random function.
Therefore the output in the each case is different each time we run the program again.
C)There are alltogether 20 elements in the array because, when we call the constructor from the main program we have called by passing the value of 20.
D)The values that an array can have is from 0-19.
E)The vallues of the array come form the Random function that have been used in the ArrayStuff class constructor.
F)Random function is different because it will always generate the values randomly, as the system wants in this case,
while in case of manually entering it, we can make it the same.
I have attached the sample output for the program here.As the class use the Random function that is why each time the program executes the values in the array will be different.
Sample Output 1
the Array is:
theArray[0] = 70
theArray[1] = 38
theArray[2] = 21
theArray[3] = 51
theArray[4] = 72
theArray[5] = 46
theArray[6] = 51
theArray[7] = 64
theArray[8] = 96
theArray[9] = 44
theArray[10] = 100
theArray[11] = 58
theArray[12] = 47
theArray[13] = 78
theArray[14] = 13
theArray[15] = 51
theArray[16] = 95
theArray[17] = 14
theArray[18] = 87
theArray[19] = 37
The array in the reverse is :
37
87
14
95
51
13
78
47
58
100
44
96
64
51
46
72
51
21
38
70
The sum of the elements in the list is 1133
The smallest element is: 13
The largest element is: 100
Location of smallest element: 0
The sorted array is
:
theArray[0] = 100
theArray[1] = 96
theArray[2] = 95
theArray[3] = 87
theArray[4] = 78
theArray[5] = 72
theArray[6] = 70
theArray[7] = 64
theArray[8] = 58
theArray[9] = 51
theArray[10] = 51
theArray[11] = 51
theArray[12] = 47
theArray[13] = 46
theArray[14] = 44
theArray[15] = 38
theArray[16] = 37
theArray[17] = 21
theArray[18] = 14
theArray[19] = 13
That's all folks!
Sample Output 2:
the Array is:
theArray[0] = 42
theArray[1] = 3
theArray[2] = 82
theArray[3] = 33
theArray[4] = 60
theArray[5] = 10
theArray[6] = 94
theArray[7] = 97
theArray[8] = 18
theArray[9] = 23
theArray[10] = 94
theArray[11] = 6
theArray[12] = 100
theArray[13] = 97
theArray[14] = 81
theArray[15] = 73
theArray[16] = 77
theArray[17] = 14
theArray[18] = 25
theArray[19] = 36
The array in the reverse is :
36
25
14
77
73
81
97
100
6
94
23
18
97
94
10
60
33
82
3
42
The sum of the elements in the list is 1065
The smallest element is: 3
The largest element is: 100
Location of smallest element: 0
The sorted array is
:
theArray[0] = 100
theArray[1] = 97
theArray[2] = 97
theArray[3] = 94
theArray[4] = 94
theArray[5] = 82
theArray[6] = 81
theArray[7] = 77
theArray[8] = 73
theArray[9] = 60
theArray[10] = 42
theArray[11] = 36
theArray[12] = 33
theArray[13] = 25
theArray[14] = 23
theArray[15] = 18
theArray[16] = 14
theArray[17] = 10
theArray[18] = 6
theArray[19] = 3
That's all folks!
Please remember that in this case the sorted output is in the descending order.To make it in the ascending order we have to loop from i=0 to Array length.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.