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...

  • Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform...

    Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • 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...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • 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...

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