Question

23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...

23.1 Append to Oversize Array

Java Help

Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit.

Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the method.

Code//

import java.util.Arrays;
import java.util.Scanner;

public class AppendOversize
{

   public static void main(String[] args)
   {
       // Do not edit this method
       Scanner keyboard = new Scanner(System.in);
       final int SIZE = 20;
       int[] array = new int[SIZE];
       int[] append = new int[SIZE];
      
       // Read in the first array
       int value = keyboard.nextInt(); // priming read
       int index;
       for(index=0; index<array.length && value != -1; ++index)
       {
           array[index] = value;
           value = keyboard.nextInt(); // priming read
       }
       keyboard.nextLine(); // get the carriage return out of there
      
       // Get the size from the number of values put in the first array
       int arraySize = index;
      
       // Read in the second array
       value = keyboard.nextInt(); // priming read
       for (index=0; index<append.length && value != -1; ++index)
       {
           append[index] = value;
           value = keyboard.nextInt(); // priming read
       }
       int appendSize = index;
      
       arraySize = appendOversize(array, arraySize, append, appendSize);
       System.out.println("Array is " + Arrays.toString(array) + " size is " + arraySize);
       keyboard.close();
   }
  
   public static int appendOversize(int[] array, int size, int[] toAppend, int toAppendSize)
   {
      
       // Put your code here

      
       return 0; // edit this line

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE FUNCTION IS BELOW:-

public static int appendOversize(int[] array, int size, int[] toAppend, int toAppendSize) {

        if (size == array.length) { // if the size parameter is equal to the length of the array

            return size; // that means the array is full and therefore returning that size

        } else {

            int i = 0;

            while (size < array.length && i < toAppendSize) { // else

                array[size++] = toAppend[i++]; // untill the array is less than the length

                                  // append array is not appended to the full the array will be appended

            }

        }

        return size;

    }

public static int appendoversize(int[] array, int size, int[] toAppend, int toAppendSize) {/ if (size == array.length) { // i

PS E:\java> javac text.java PS E:\java> java text.java 1 2 3 4 5 6 7 8 9 10 99 -1 11 12 13 45 56 6 5 65 21 65 -1 Array is [1,

FOR ANY OTHER QUERY PLEASE COMMENT.

Add a comment
Know the answer?
Add Answer to:
23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...
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
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a...

    Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       final int NUM_VALS = 4;       int[] hourlyTemp = new...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • 7.2.3: Printing array elements with a for loop. Write a for loop to print all elements...

    7.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) Also note: If the submitted code tries to access an invalid...

  • in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first...

    in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first three elements of array run Times. Follow each statement with a newline. Ex: If runTime = (800,775, 790, 805, 808) print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5 element array. See 'How to Use zyBooks". Also note: If the submitted code tries to access an invalid array...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

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