Question

Complete the following program based on the comments inside. Important note: You are only allowed to...

Complete the following program based on the comments inside. Important note: You are only allowed to use the new operator in solving this problem. (i.e. Don't use predefined array functions such as append(), concat(), etc. in solving this problem) Important Note 2: Your program should work the same if the sizes of first_array and second_array are changed. (i.e. make use of the .length variable to get the length of the arrays) void setup () { int [] first_array = {11, 12, 13, 14}; int [] second_array = {22, 33, 44}; int [] third_array; //Write code here so that the third array contains all //elements of the second array and first array, starting //with the elements of the first array. //So in this case, after your code runs, the third array //would contain: 11, 12, 13, 14, 22, 33, 44 }

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


import java.util.ArrayList;
import java.util.Scanner;


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class MainClass {
  
public static void setup()
{
int[] first = {11,12,13,14};
int[] second = {22,33,44};
int len = first.length+second.length;
  
int[] third = new int[len];
  
int i = 0;
int k = 0;
  
while(i<first.length)
{
third[k] = first[i];
k++;
i++;
}
  
i = 0;
  
while(i<second.length)
{
third[k] = second[i];
k++;
i++;
}
  
System.out.print("Content of third array is : ");
  
for(i=0;i<third.length;i++)
System.out.print(third[i] + " ");
}
  
public static void main(String[] args) {
  
setup();
}
}

OUTPUT:

run: Content of third array is 11 12 13 14 22 33 44 BUILD SUCCESSFUL (total time: 1 second)

Add a comment
Know the answer?
Add Answer to:
Complete the following program based on the comments inside. Important note: You are only allowed to...
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
  • Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the...

    Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the main method creates two arrays, each with 10 int slots. main initializes the first array to randomly generated integers. It then calls a method named copy, passing it the two arrays. Your copy method copies the contents of the first array to the second array. It then returns to main. main then displays the contents of both arrays. b) Write a program in which...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Answer with simple java code and comments This homework deals with the problem of partitioning an...

    Answer with simple java code and comments This homework deals with the problem of partitioning an array. We'll define partitioning to mean arranging the elements of an array around a certain "pivot, " element such that all elements to the left of the pivot are strictly less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. To simplify things a little bit, we'll assume that the pivot element is...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • c/c++ This is a simple debugging question. The program (as written) tries to add up the...

    c/c++ This is a simple debugging question. The program (as written) tries to add up the elements of two arrays and store the results into a third array. The idea is that every element of the third array equals the sum of the two corresponding (i.e same index or position in the array) elements in the first two arrays. There is a syntax error, because the way it is written is not the correct way to do it. You are...

  • Given below is a partially completed C program, which you will use as a start to...

    Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint:...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1.   Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...

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

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