In: Computer Science
CIS 1068 Assignment 6
practice with static methods and arrays
Implement each of the following functions and write a basic main() function that tests each.
public class ArrayPractice { /* sets every item in the array A references to initialValue */ public static void initialize(int A[], int initialValue) { ; } /* * returns the average of the items in the array A references. Be careful: the array contains int * but 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 the array A references */ public static int numOccurrences(int A[], int x) { return 0; } /* * returns the index of the first occurrence of x in the array A references or -1 if x doesn't * exist in the array */ 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 the array A[] * references or -1 if item is not among the first n elements of the array */ public static int findN(int A[], int item, int n) { return -1; } /* * returns the index of the last occurrence of x in the array A references or -1 if x doesn't * exist in the array */ public static int findLast(int A[], int x) { return -1; } /* returns the largest item found in the array A references */ public static int largest(int A[]) { return 0; } /* returns the index of the largest item found in the array A references */ public static int indexOfLargest(int A[]) { return 0; } /* * returns the index of the largest odd number in the array A references or -1 if the array * contains no odd numbers */ public static int indexOfLargestOdd(int A[]) { return -1; } /* * returns a new array consisting of all of the elements of A[] */ public static int[] copy(int A[]) { return null; } /* * Returns a reference to a new array consisting of all of the first n elements of A[]. If * n>A.length, returns a reference to 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 reference to an array consisting of all of the elements of the array A references * that are odd. If there are no odd integers in the array, the function returns null. */ public static int[] copyOdds(int[] A) { return null; } /* removes and returns the item at index x shifting all elements at */ /* indices > x one position to the left and filling in a 0 at the */ /* right-most position in the array. */ /* if x is an invalid index, returns -1. */ /* For example, if before we call function with x = 2, */ /* the the array is: */ /* |----+----+----+----+----+----+----+----+----+-----| */ /* | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | */ /* |----+----+----+----+----+----+----+----+----+-----| */ /* after the function finishes, the array is: */ /* |----+----+----+----+----+----+----+----+-----+---| */ /* | 10 | 20 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 0 | */ /* |----+----+----+----+----+----+----+----+-----+---| */ /* and the function returns 30 */ public static int remove(int[] A, int x) { return -1; } /* shifts all elements of the array A references one position to the left, */ /* removing the first element and filling in 0 from the right hand side. */ /* For example, if before we call the function the the array is: */ /* |----+----+----+----+----+----+----+----+----+-----| */ /* | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | */ /* |----+----+----+----+----+----+----+----+----+-----| */ /* after the function finishes, the array is: */ /* +----+----+----+----+----+----+----+----+-----|----| */ /* | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 0 | */ /* +----+----+----+----+----+----+----+----+-----|----| */ public static void shiftLeft(int[] A) { ; } /* * returns true if A is in sorted ascending order and false otherwise */ public static boolean isSortedAscending(int[] A) { return false; } /* Returns the number of items in the array that A references starting at index x that are in */ /* ascending sorted order. */ /* For example, if the array is: */ /* |----+----+---+---+---+---+----+----+----| */ /* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | */ /* |----+----+---+---+---+---+----+----+----| */ /* | 10 | 11 | 5 | 3 | 9 | 6 | 18 | 37 | 40 | */ /* |----+----+---+---+---+---+----+----+----| */ /* and x is 0, the function return 2, because 10 and 11 are in sorted order. */ /* If x is 5, the function returns 4, because 6, 18, 37, and 40 are in sorted order. */ /* * If x is 2, the function returns 1. * */ public static int sortedAscendingRun(int[] A, int x) { return -1; } /* * 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[]) { ; } /* * 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; } }
Please do this in Java, need it ASAP!
Java program (modified)
public class ArrayPractice {
/* sets every item in the array A references to initialValue */
public static void initialize(int A[], int initialValue) {
for(int i=0;i<A.length;i++)
A[i] = initialValue;
}
/*
* returns the average of the items in the array A references. Be careful: the array contains int
* but the method returns double. What do we do to handle this?
*/
public static double average(int A[]) {
int sum = 0;
for(int i=0;i<A.length;i++)
sum += A[i];
return (double)sum/A.length; //typecasting result to double which tells the compiler to treat them as doubles
}
/* returns the number of times that x appears in the array A references */
public static int numOccurrences(int A[], int x) {
int count = 0;
for(int i=0;i<A.length;i++)
if(A[i]==x)
count++; //counting if found match
return count;
}
/*
* returns the index of the first occurrence of x in the array A references or -1 if x doesn't
* exist in the array
*/
public static int find(int A[], int x) {
for(int i=0;i<A.length;i++)
if(A[i]==x)
return i; //returns index if present else returns -1
return -1;
}
/*
* Returns the index of the first occurrence of item within the first n elements of the array A[]
* references or -1 if item is not among the first n elements of the array
*/
public static int findN(int A[], int item, int n) {
for(int i=0;i<(n>A.length?A.length:n);i++)
if(A[i]==item)
return i;
return -1;
}
/*
* returns the index of the last occurrence of x in the array A references or -1 if x doesn't
* exist in the array
*/
public static int findLast(int A[], int x) {
for(int i=A.length-1;i>-1;i--) //traversing in reverse order and finding element if not found returning -1
if(A[i]==x)
return i;
return -1;
}
/* returns the largest item found in the array A references */
public static int largest(int A[]) {
int large = A[0];
for(int i=1;i<A.length;i++)
if(large<A[i])
large = A[i];
return large;
}
/* returns the index of the largest item found in the array A references */
public static int indexOfLargest(int A[]) {
int l = 0;
for(int i=1;i<A.length;i++)
if(A[l]<A[i])
l = i;
return l;
}
/*
* returns the index of the largest odd number in the array A references or -1 if the array
* contains no odd numbers
*/
public static int indexOfLargestOdd(int A[]) {
int l = -1;
for(int i=0;i<A.length;i++)
if(A[i]%2==1 && (l==-1 || A[l]<A[i])) //if the number is odd and if either if we found an odd for the first time (i.e l==-1) or we found that odd in l index is less than odd no in i index
l = i;
return l;
}
/*
* returns a new array consisting of all of the elements of A[]
*/
public static int[] copy(int A[]) {
int[] New = new int[A.length];
for(int i=0;i<A.length;i++)
New[i] = A[i];
return New;
}
/*
* Returns a reference to a new array consisting of all of the first n elements of A[]. If
* n>A.length, returns a reference to 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) {
if(n<=0)
return null;
int[] New = new int[n];
int i;
for(i=0;i<(n<A.length?n:A.length);i++) //(n<A.length?n:A.length) returns the lowest among them
New[i] = A[i];
for(;i<n;i++)
New[i] = 0;
return New;
}
/*
* returns a reference to an array consisting of all of the elements of the array A references
* that are odd. If there are no odd integers in the array, the function returns null.
*/
public static int[] copyOdds(int[] A) {
int odds= 0;
for(int i=0;i<A.length;i++) //first calculating the total odd numbers count and declaring dynamic memory and copying
if(A[i]%2==1)
odds++;
if(odds==0)
return null;
int[] New = new int[odds];
int j=0;
for(int i=0;i<A.length;i++)
if(A[i]%2==1)
{
New[j] = A[i];
j++;
}
return New;
}
/* removes and returns the item at index x shifting all elements at */
/* indices > x one position to the left and filling in a 0 at the */
/* right-most position in the array. */
/* if x is an invalid index, returns -1. */
/* For example, if before we call function with x = 2, */
/* the the array is: */
/* |----+----+----+----+----+----+----+----+----+-----| */
/* | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | */
/* |----+----+----+----+----+----+----+----+----+-----| */
/* after the function finishes, the array is: */
/* |----+----+----+----+----+----+----+----+-----+---| */
/* | 10 | 20 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 0 | */
/* |----+----+----+----+----+----+----+----+-----+---| */
/* and the function returns 30 */
public static int remove(int[] A, int x) {
if(x<0 || x>A.length)
return -1;
int ele = A[x], i;
for(i=x;i<A.length-1;i++)
A[i] = A[i+1];
A[i] = 0;
return ele;
}
/* shifts all elements of the array A references one position to the left, */
/* removing the first element and filling in 0 from the right hand side. */
/* For example, if before we call the function the the array is: */
/* |----+----+----+----+----+----+----+----+----+-----| */
/* | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | */
/* |----+----+----+----+----+----+----+----+----+-----| */
/* after the function finishes, the array is: */
/* +----+----+----+----+----+----+----+----+-----|----| */
/* | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 0 | */
/* +----+----+----+----+----+----+----+----+-----|----| */
public static void shiftLeft(int[] A) {
int i;
for(i=0;i<A.length-1;i++)
A[i] = A[i+1];
A[i] = 0;
}
/*
* returns true if A is in sorted ascending order and false otherwise
*/
public static boolean isSortedAscending(int[] A) {
for(int i=0;i<A.length-1;i++)
if(A[i]>A[i+1]) //if next element is smaller than current element returning false
return false;
return true;
}
/* Returns the number of items in the array that A references starting at index x that are in */
/* ascending sorted order. */
/* For example, if the array is: */
/* |----+----+---+---+---+---+----+----+----| */
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | */
/* |----+----+---+---+---+---+----+----+----| */
/* | 10 | 11 | 5 | 3 | 9 | 6 | 18 | 37 | 40 | */
/* |----+----+---+---+---+---+----+----+----| */
/* and x is 0, the function return 2, because 10 and 11 are in sorted order. */
/* If x is 5, the function returns 4, because 6, 18, 37, and 40 are in sorted order. */
/*
* If x is 2, the function returns 1.
*
*/
public static int sortedAscendingRun(int[] A, int x) {
if(x<0 || x>A.length)
return -1;
int i;
for(i=x+1;i<A.length;i++) //traversing until not sorted
if(A[i-1]>A[i])
break;
return i-x; //and returning the differences of the indices
}
/*
* 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[] New = new int[A.length+B.length];
int i=0;
for(;i<A.length;i++)
New[i] = A[i];
int n = i; // n is equal to A.length
for(i=0;i<B.length;i++)
New[n+i] = B[i];
return New;
}
/*
* 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[]) {
for(int i=0;i<A.length/2;i++)
{
int temp = A[A.length-1-i]; //swapping elements
A[A.length-1-i] = A[i];
A[i] = temp;
}
}
/*
* 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[]) {
if(A==null)
return null;
int count=1, i, j;
for(i=1;i<A.length;i++) //calculating the total unique elements and declaring a dynamic array and copying elements based on the same logic
{
for(j=0;j<i;j++)
if(A[i]==A[j])
break;
if(j==i)
count++;
}
int[] New = new int[count];
New[0] = A[0];
int k=0;
for(i=1;i<A.length;i++)
{
for(j=0;j<i;j++)
if(A[i]==A[j])
break;
if(j==i)
New[k++] = A[i];
}
return New;
}
/*
Prints the Array contents to the screen
*/
public static int print(int A[])
{
if(A==null)
return -1;
for(int i=0;i<A.length;i++)
System.out.print(A[i]+" ");
System.out.println();
return 0;
}
/*
Main Method testing remaining all methods
*/
public static void main(String args[]) //main method to test all the methods
{
int[] init = new int[6];
initialize(init, 5);print(init);
int[] A = { 10, 11, 5, 5, 3, 9, 6, 18, 37, 40, 5 };
print(A);
System.out.println("Average of above array: "+average(A));
System.out.println("No of times 5 repeated in above array: "+numOccurrences(A, 5));
System.out.println("First occurence of 3 in the above array: "+find(A, 3));
System.out.println("First occurence of 5 in the first 5 elements: "+findN(A, 5, 5));
System.out.println("Last occurence of 5 in the above array: "+findLast(A, 5));
System.out.println("Largest of all in above array: "+largest(A));
System.out.println("Index of largest of all in above array: "+indexOfLargest(A));
System.out.println("Index of largest odd of all in above array: "+indexOfLargestOdd(A));
int[] cp = copy(A);
print(cp);
cp = copyN(A, 6);
print(cp);
cp = copyOdds(A);
print(cp);
System.out.println("Removing 3rd indexed element of above array and printing it: "+remove(A, 3));
print(A);
shiftLeft(A);
print(A);
System.out.println("Is the above array sorted: "+isSortedAscending(A));
System.out.println("How many sorted from index 4: "+sortedAscendingRun(A, 4));
int[] B = {38,443,76,190};
print(copyAll(A, B));
reverse(A);
print(A);
print(uniques(A));
}
}
Java program Screenshots
Output:
Don't forget to save the java file as ArrayPractice.java
Each and everything is explained clearly in the comment section of the code.
Thank you! Hit like if you like my work.