Question

In: Computer Science

CIS 1068 Assignment 6 practice with static methods and arrays Implement each of the following functions...

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!

Solutions

Expert Solution

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.


Related Solutions

You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with arrays and array parameters. Arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot operator ( .), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get...
Coding in Java Assignment Write the following static methods. Assume they are all in the same...
Coding in Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a...
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT