Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this exercise is to practice with arrays and static methods. You\

ID: 3604893 • Letter: T

Question

The purpose of this exercise is to practice with arrays and static methods. You'll find that it contains several methods that have not yet been finished. For now, the methods contain only a placeholder return value so that the code will compile. Fill in your own implementation. The uniques() method is extra credit. Do not modify any other files in the project. Any HELP with this would be greatly appreciated!!

public class ArrayPractice {

/* sets every item in A[] to initialValue */
public static void initialize(int A[], int initialValue) {
return;
}

/* returns the average of the items in A
* Be careful: A[] is an array of int and the method returns
* double. What do we do to handle this? */
public static double average(int A[]) {
return 0.0;
}

/* returns the number of times that x appears in A[] */
public static int numOccurrences(int A[], int x) {
return 0;
}


/* returns the index of the first occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int find(int A[], int x) {
return -1;
}

/* Returns the index of the first occurrence of
* item within the first n elements of A[] or -1
* if item is not among the first n elements of A[] */
public static int findN(int A[], int item, int n) {
return -1;
}

/* returns the index of the last occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int findLast(int A[], int x) {
return -1;
}

/* returns the largest item found in A */
public static int largest(int A[]) {
return -1;
}

/* returns the index of the largest item found in A */
public static int indexOfLargest(int A[]) {
return -1;
}

/* returns the index of the largest odd number
* in A[] or -1 if A[] contains no odd numbers */
public static int indexOfLargestOdd(int A[]) {
return -1;
}

/* inserts n into A[] at A[index] shifting all */
/* the previous items one place to the right. For example */
/* if A is */
/* |---+---+---+---+---+---+---+---+---+---| */
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | */
/* |---+---+---+---+---+---+---+---+---+---| */
/* | 5 | 7 | 6 | 9 | 4 | 3 | 0 | 0 | 0 | 0 | */
/* |---+---+---+---+---+---+---+---+---+---| */

/* and we call insert(A, 15, 1), A then becomes */

/* |---+----+---+---+---+---+---+---+---+---| */
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | */
/* |---+----+---+---+---+---+---+---+---+---| */
/* | 5 | 15 | 7 | 6 | 9 | 4 | 3 | 0 | 0 | 0 | */
/* |---+----+---+---+---+---+---+---+---+---| */
/* the element in A[] that's in the right-most */
/* position is removed. */
/* */
/* if index < 0 or index >= A.length-1, the method */
/* does nothing */
public static void insert(int A[], int n, int index) {
return;
}

/* returns a new array consisting of all of the
* elements of A[] */
public static int[] copy(int A[]) {
return null;
}

/* Returns a new array consisting of all of the
first n elements of A[]. If n>A.length, returns a
new array of size n, with the first A.length elements
exactly the same as A, and the remaining n-A.length elements
set to 0. If n<=0, returns null. */
public static int[] copyN(int A[], int n) {
return null;
}

/* returns a new array consisting of all of the
* elements of A[] followed by all of the
* elements of B[]. For example, if
A[] is: {10,20,30} and
B[] is: {5, 9, 38}, the method returns the
array : {10,20,30,5,9,38} */
public static int[] copyAll(int A[], int B[]) {
return null;
}

/* reverses the order of the elements in A[].
* For example, if A[] is:
{10,20,30,40,50}, after the method, A[] would
be {50,40,30,20,10} */
public static void reverse(int A[]) {
return;
}

/* Extra credit:
*
* Returns a new array consisting of all of the
* elements of A, but with no duplicates. For example,
* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns
* the array {10,20,5,32,9,8} */
public static int[] uniques(int A[]) {
return null;
}

public static void main(String[] args) {
return;
}
}

Explanation / Answer

//source code//

package chegg;

public class ArrayPractice {

/* sets every item in A[] to initialValue */

  

public static void printArray(int A[])

{

int i=0;

for(i=0;i<A.length;i++)

System.out.print(A[i]+" ");  

System.out.println("");

}

public static void initialize(int A[], int initialValue) {

int i=0;

for(i=0;i<A.length;i++)

A[i]=initialValue;   

return;

}

/* returns the average of the items in A

* Be careful: A[] is an array of int and the method returns

* double. What do we do to handle this? */

public static double average(int A[]) {

int i=0;

double sum=0;

for(i=0;i<A.length;i++)

sum+=A[i];

return sum/A.length ;

}

/* returns the number of times that x appears in A[] */

public static int numOccurrences(int A[], int x) {

int i=0;

int counter=0;

for(i=0;i<A.length;i++)

{

if(A[i]==x)

counter++;

}

return counter;

}

/* returns the index of the first occurrence of

* x in A[] or -1 if x doesn't exist in A[] */

public static int find(int A[], int x) {

int i=0;

int index=-1;

for(i=0;i<A.length;i++)  

{

if(A[i]==x)

return i; //exiting from function by returning index of the element

}

return index;

}

/* Returns the index of the first occurrence of

* item within the first n elements of A[] or -1

* if item is not among the first n elements of A[] */

public static int findN(int A[], int item, int n) {

int i=0;

int index=-1;

for(i=0;i<n;i++)  

{

if(A[i]==item)

return i; //exiting from function by returning index of the element

}

return index;

}

/* returns the index of the last occurrence of

* x in A[] or -1 if x doesn't exist in A[] */

public static int findLast(int A[], int x) {

int i=0;

int index=-1;

for(i=A.length-1;i>=0;i--)  

{

if(A[i]==x)

return i; //exiting from function by returning index of the element

}

return index;

}

/* returns the largest item found in A */

public static int largest(int A[]) {

int i=0;

int largest=-1; //assuming all positive integer

for(i=0;i<A.length;i++)  

{

if(A[i]>largest)

largest=A[i]; //exiting from function by returning index of the element

}

return largest;

}

/* returns the index of the largest item found in A */

public static int indexOfLargest(int A[]) {

int i=0;

int largest=-1;

int largest_index=-1; //assuming all positive integer

for(i=0;i<A.length;i++)  

{

if(A[i]>largest)

{

largest=A[i]; //exiting from function by returning index of the element

largest_index=i;

}

}

return largest_index;

}

/* returns the index of the largest odd number

* in A[] or -1 if A[] contains no odd numbers */

public static int indexOfLargestOdd(int A[]) {

int i=0;

int largest=-1;

int largest_index=-1; //assuming all positive integer

for(i=0;i<A.length;i++)  

{

if(A[i]>largest && A[i]%2!=0)

{

largest=A[i]; //exiting from function by returning index of the element

largest_index=i;

}

}

return largest_index;

}

/* inserts n into A[] at A[index] shifting all */

/* the previous items one place to the right. For example */

/* if A is */

/* |---+---+---+---+---+---+---+---+---+---| */

/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | */

/* |---+---+---+---+---+---+---+---+---+---| */

/* | 5 | 7 | 6 | 9 | 4 | 3 | 0 | 0 | 0 | 0 | */

/* |---+---+---+---+---+---+---+---+---+---| */

/* and we call insert(A, 15, 1), A then becomes */

/* |---+----+---+---+---+---+---+---+---+---| */

/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | */

/* |---+----+---+---+---+---+---+---+---+---| */

/* | 5 | 15 | 7 | 6 | 9 | 4 | 3 | 0 | 0 | 0 | */

/* |---+----+---+---+---+---+---+---+---+---| */

/* the element in A[] that's in the right-most */

/* position is removed. */

/* */

/* if index < 0 or index >= A.length-1, the method */

/* does nothing */

public static void insert(int A[], int n, int index) {

int temp1,temp2=A[index];

for(int i=index;i<A.length-1;i++)

{

temp1=A[i+1];

A[i+1]=temp2;

temp2=temp1;

}

A[index]=n;

}

/* returns a new array consisting of all of the

* elements of A[] */

public static int[] copy(int A[]) {

int arr[] = new int[A.length];

for(int i=0;i<A.length;i++)

arr[i]=A[i];

return arr;

}

/* Returns a new array consisting of all of the

first n elements of A[]. If n>A.length, returns a

new array of size n, with the first A.length elements

exactly the same as A, and the remaining n-A.length elements

set to 0. If n<=0, returns null. */

public static int[] copyN(int A[], int n) {

int arr[] = new int[n];

for(int i=0;i<n;i++)

{

if(i<A.length)

arr[i]=A[i];

else

arr[i]=0;

}

return arr;

}

/* returns a new array consisting of all of the

* elements of A[] followed by all of the

* elements of B[]. For example, if

A[] is: {10,20,30} and

B[] is: {5, 9, 38}, the method returns the

array : {10,20,30,5,9,38} */

public static int[] copyAll(int A[], int B[]) {

int arr[] = new int[A.length + B.length];

for(int i=0;i<A.length+B.length;i++)

{

if(i<A.length)

arr[i]=A[i];

else

arr[i]=B[i-A.length];

}

return arr;

}

/* reverses the order of the elements in A[].

* For example, if A[] is:

{10,20,30,40,50}, after the method, A[] would

be {50,40,30,20,10} */

public static void reverse(int A[]) {

int i=0,temp1;

int j=A.length-1;

for(i=0;i<j;i++,j--)

{

temp1=A[i];

A[i]=A[j];

A[j]=temp1;

}

}

/* Extra credit:

*

* Returns a new array consisting of all of the

* elements of A, but with no duplicates. For example,

* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns

* the array {10,20,5,32,9,8} */

public static int[] uniques(int A[]) {

int arr[] = new int[A.length];

int i,j,index=0,flag;

for( i=0;i<A.length;i++)

{

flag=0;

for(j=0;j<index;j++)

{

if(A[i]==arr[j])

{

flag=1;

break;

}

}

if(flag!=1)

arr[index++]=A[i];

}

return copyN(arr,index); //taking the array till the index which is actual length

}

public static void main(String[] args) {

int A[]= new int[10];

initialize(A, 0);

insert(A, 4, 0);

insert(A, 6, 0);

insert(A, 4, 0);

insert(A, 3, 0);

insert(A, 5, 0);

insert(A, 3, 0);

System.out.println("After inserting those element");

printArray(A);

System.out.println("Average of the elements" + average(A));

System.out.println("no. of occ " + numOccurrences(A, 3));

System.out.println( find(A, 6));

System.out.println( findN(A, 4, 3));

System.out.println( findLast(A, 4));

System.out.println( largest(A));

System.out.println( indexOfLargest(A));

System.out.println( indexOfLargestOdd(A));

printArray(copy(A));

printArray(copyN(A,5));

int B[]={7,8,9,10};

printArray(copyAll(A,B));

reverse(A);

printArray(A);

printArray(uniques(A));

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote