Question

The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly:

/**
 * DESCRIPTION OF PROGRAM HERE
 * @author YOUR NAME HERE
 * @author PARTNER NAME HERE
 * @version DATE HERE
 *
 */
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ArrayExercises {

    /**
     * Given a random number generator and a length, create a new array of that
     * length and fill it from left to right with random numbers between 1 and
     * 6.
     *
     * @param rnd
     *            Random number generator to draw numbers from
     * @param len
     *            size of array to create
     * @return array containing random numbers of length len
     */
    public static int[] randomArray(Random rnd, int len) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return null;
    }

    /**
     * Given an array of ints, returns the sum of the values. The array can be
     * of any length.
     *
     * @param array
     *            array to total
     * @return sum of the elements in the array
     */
    public static int sumArray(int[] array) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return 0;
    }

    /**
     * Given two arrays of integers of the same size, interleaves the contents
     * of the two arrays with an element of arr1 followed by an element of arr2.
     * For example, if arr1 = < 1, 2, 3 > and arr2 = < 4, 5, 6 >, the array
     * returned by this method should be < 1, 4, 2, 5, 3, 6 >. The behavior of
     * this method is undefined for array parameters of different lengths.
     *
     * @param arr1
     *            first array to interleave
     * @param arr2
     *            second array to interleave
     * @return array of length arr1.length + arr2.length with contents
     *         interleaved as described above.
     */
    public static int[] mergeArrays(int[] arr1, int[] arr2) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return null;
    }

    /**
     * Simple test program to test the methods given above. Uncomment the
     * relevant portions as you complete the methods above.
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a random seed: ");
        int seed = Integer.parseInt(input.nextLine());
        Random rnd = new Random(seed);
        int[] randomArray1 = randomArray(rnd, 6);
        System.out.print("First random array: ");
        System.out.println(Arrays.toString(randomArray1));
        //        int sum1 = sumArray(randomArray1);
        //        System.out.println("The total is: " + sum1);
        int[] randomArray2 = randomArray(rnd, 6);
        System.out.print("Next random array: ");
        System.out.println(Arrays.toString(randomArray2));
        //        int sum2 = sumArray(randomArray2);
        //        System.out.println("The total is: " + sum2);
        //        int[] merged = mergeArrays(randomArray1, randomArray2);
        //        System.out.print("The arrays merged are: ");
        //        System.out.println(Arrays.toString(merged));
        input.close();
    }

}

When your code runs, it should be able to produce the following transcript - note that with the same seed you should get EXACTLY this transcript:

Enter a random seed: 33
First random array: [4, 4, 6, 6, 5, 2]
The total is: 27
Next random array: [2, 3, 3, 6, 3, 1]
The total is: 18
The arrays merged are: [4, 2, 4, 3, 6, 3, 6, 6, 5, 3, 2, 1]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
* DESCRIPTION OF PROGRAM HERE
* @author YOUR NAME HERE
* @author PARTNER NAME HERE
* @version DATE HERE
*
*/
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ArrayExercises {

   /**
   * Given a random number generator and a length, create a new array of that
   * length and fill it from left to right with random numbers between 1 and 6.
   *
   * @param rnd
   * Random number generator to draw numbers from
   * @param len
   * size of array to create
   * @return array containing random numbers of length len
   */
   public static int[] randomArray(Random rnd, int len) {
       int arr[] = new int[len];

       for (int i = 0; i < len; ++i)
           arr[i] = rnd.nextInt(6) + 1;

       return arr;
   }

   /**
   * Given an array of ints, returns the sum of the values. The array can be of
   * any length.
   *
   * @param array
   * array to total
   * @return sum of the elements in the array
   */
   public static int sumArray(int[] array) {
       int sum = 0;
       for (int i = 0; i < array.length; ++i)
           sum = sum + array[i];
       return sum;
   }

   /**
   * Given two arrays of integers of the same size, interleaves the contents of
   * the two arrays with an element of arr1 followed by an element of arr2. For
   * example, if arr1 = < 1, 2, 3 > and arr2 = < 4, 5, 6 >, the array returned by
   * this method should be < 1, 4, 2, 5, 3, 6 >. The behavior of this method is
   * undefined for array parameters of different lengths.
   *
   * @param arr1
   * first array to interleave
   * @param arr2
   * second array to interleave
   * @return array of length arr1.length + arr2.length with contents interleaved
   * as described above.
   */
   public static int[] mergeArrays(int[] arr1, int[] arr2) {
       int arrnew[] = new int[2 * arr1.length];
       int j = 0, k = 0;
       boolean flag = true;
       for (int i = 0; i < arrnew.length; ++i) {
           if (flag)
               arrnew[i] = arr1[j++];
           else
               arrnew[i] = arr2[k++];
          
           flag = !flag;
       }

       return arrnew;
   }

   /**
   * Simple test program to test the methods given above. Uncomment the relevant
   * portions as you complete the methods above.
   *
   * @param args
   */
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a random seed: ");
       int seed = Integer.parseInt(input.nextLine());
       Random rnd = new Random(seed);
       int[] randomArray1 = randomArray(rnd, 6);
       System.out.print("First random array: ");
       System.out.println(Arrays.toString(randomArray1));
       int sum1 = sumArray(randomArray1);
       System.out.println("The total is: " + sum1);
       int[] randomArray2 = randomArray(rnd, 6);
       System.out.print("Next random array: ");
       System.out.println(Arrays.toString(randomArray2));
       int sum2 = sumArray(randomArray2);
       System.out.println("The total is: " + sum2);
       int[] merged = mergeArrays(randomArray1, randomArray2);
       System.out.print("The arrays merged are: ");
       System.out.println(Arrays.toString(merged));
       input.close();
   }

}

====================================

SEE OUTPUT

OCCU11 U OLLEIŲ LIU as described above. 64 OOORRNO public static int[] mergeArrays(int[] arri, int[] arr2) { int arrnew[] = n

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array...

    Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /**  Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • 8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...

    8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT