Question

Need help with java code, this is all in a method 1) 3 different arrays are...

Need help with java code, this is all in a method

1) 3 different arrays are brought in

2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in

3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays

hopefully I made sense

sample input/output

int [ ] arrayWithLargestValues = new int [ ] // this has to be set to the size of largest array

int [ ] array1 = { 2, 30, 40, 1}

int [ ] array2 = { 100, 10}

int [ ] array3 {13 , 12 , 10 , 19, 28}

arrayWithLargestValues = createArrayWithLargestValues(array1, array2, array3);

output:

An Array with the largest values taken from the 3 arrays has 5 (array3) elements.

the elements in the array are 100 ,40 ,30 , 28, 19

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

The above problem can be solved in the steps mentioned below-

STEP 1- First we will define a method createArrayWithLargestValues(int array1[], int array2[], int array3[]). This method takes in elements of array1, array2 and array3 and stores all these elements in a list. Then it sorts that list in descending order. And then that number of elements are picked from list that is the size of resulting array and these number are stored in the final Array to be returned by function.

STEP 2- Now in the main method, we declare three arrays with values, Find out the size of array and store it. Using this value create an array of this size which will be out result array.

STEP 3- Then the above function is called and the array returned by that function is stored .

STEP 4- The output is displayed as shown in question.

JAVA CODE-

import java.util.*;
public class Main
{
//class variable to store the size of resulting array
//because it has to be used in main() method as well as createArrayWithLargestValues() method
private static int sizeArrayResult;

//function createArrayWithLargestValues()
//takes in all the three arrays
//return the resulting array as mentioned in question
public static int[] createArrayWithLargestValues(int array1[], int array2[], int array3[]){
//declare an array that would be returned as answer
int [] finalArr = new int[sizeArrayResult];
//create a list to store all the elements of all 3 arrays
List<Integer> list = new ArrayList<Integer>();
  
//add elements of array1 to list
for(int i=0;i<array1.length;i++){
list.add(array1[i]);
}
//add elements of array2 to list
for(int i=0;i<array3.length;i++){
list.add(array3[i]);
}
//add elements of array3 to list
for(int i=0;i<array2.length;i++){
list.add(array2[i]);
}
  
//sort the list in descending order
//use sort method present in Collections class in java, use reverseOrder() to sort in descending
Collections.sort(list, Collections.reverseOrder());
  
//finalArr has the top (sizeArrayResult) elements of list
for(int i=0;i<sizeArrayResult;i++){
finalArr[i] = list.get(i);
}
//return the finalArr
return finalArr;
}
  
//main method
   public static void main(String[] args) {
//declare and intialize array1 elements
int [ ] array1 = { 2, 30, 40, 1};
//declare and intialize array2 elements
int [ ] array2 = { 100, 10};
//declare and intialize array3 elements
int [ ] array3 = {13 , 12 , 10 , 19, 28};
  
//find and store the length of each array
int size1 = array1.length;
int size2 = array2.length;
int size3 = array3.length;
  
String nameOfLargestArray = ""; //to store the name of the array with largest size
  
//find out the size of largest of all 3 arrays using nested if-else statements
if(size1 > size2){
if(size1 > size3){
sizeArrayResult = size1;
nameOfLargestArray = "array1";
}
else{
sizeArrayResult = size3;
nameOfLargestArray = "array3";
}
}
else{
if(size2 > size3){
sizeArrayResult = size2;
nameOfLargestArray = "array2";
}
else{
sizeArrayResult = size3;
nameOfLargestArray = "array3";
}
  
}
  
//declare array arrayWithLargestValues of size sizeArrayResult(size of largest length array)
int [ ] arrayWithLargestValues = new int [sizeArrayResult];
//call createArrayWithLargestValues() method and store the returned Array
arrayWithLargestValues = createArrayWithLargestValues(array1, array2, array3);
  
//print the message and contents of arrayWithLargestValues as Output
System.out.println("An Array with the largest values taken from the 3 arrays has " + sizeArrayResult + "("+
nameOfLargestArray+ ") elements.");
System.out.print("Elements of array are : ");
for(int i=0;i<sizeArrayResult;i++){
System.out.print(arrayWithLargestValues[i] + " ");
}

  
   }
}

IMAGE OF CODE-

OUTPUT-

If this answer helps, please give an up vote and feel free to comment for any query.

Add a comment
Know the answer?
Add Answer to:
Need help with java code, this is all in a method 1) 3 different arrays are...
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
  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0,...

    i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0, -56, 4, -7 and array2-19, 12, -36, -2, 12 3. Write the code to form and display another array array3 in which each element is the sum of numbers in the position in both arrays. Use pointers and functions: get array0. process arrays0 and show array0 (50 points) 2. Enter a positive 5-digit integer via the keyboard. Display each digit and fol- lowed by...

  • C++ File I/O Create a program that will work for all "infile.txt" of similar structure: The...

    C++ File I/O Create a program that will work for all "infile.txt" of similar structure: The first line will contain two integers. The first integer is the number of elements in array1, and the second integer the number of elements in array2. The second line will contain all the values of array1, and the third line will contain all values of array2. All numbers are separated by a space and all lines separated by a newline character. "infile.txt" has three...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last in...

    using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...

  • Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can...

    Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can i contact you? -------------------------- Program 1 - WRITE ALL THE CODE in ONE FILE...   MyArrayPtrArith1.cpp Step 1 - Define the class Write a class, myArrayClass, that creates an integer array. * Add an 'arraySize' variable, initialize to zero ( default constructor function ) * Create int * ptrArray ( default constructor set to NULL ) * Add a default constructor ( see above for...

  • c++ please help Create two arrays of 20 elements, called myArray1 and myArray2 Create a function...

    c++ please help Create two arrays of 20 elements, called myArray1 and myArray2 Create a function randBetween that returns an integer between 2 integer inputs (int randBetween(int min, int max)); Fill the myArray1 with the random values. Loop thru the myArray1 and find the difference between the element i and element i + 1; put that difference in array2 example: array1 = {1, 3, 4, 5} array2 ends up having {-2, -1, -1} Loop thru myArray1 and find the min...

  • 1- Complete all the blanks in the function f1() below such that it implements the operations...

    1- Complete all the blanks in the function f1() below such that it implements the operations described in the comments. You may only fill in the blanks and not add or alter other code. Also complete main(). // f1 copies the last *half* of vec's values into a new array pointed to 'arr'. // 'arr' is garbage coming in and must be modified by this function so the // caller can then use the produced array. The values copied from...

  • I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small...

    I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small progressive parts, with the intention of developing the skill of working with arrays. Do each part separately. Include in your submission the code and output for Part I, then the code and output for Part 2, etc. into a Word document. Specification: Part 1. Write a main function that declares an array of 10 int’s. Assign each element in the array a value between...

  • please write me this program in C language Review UW files from the Internet can contain...

    please write me this program in C language Review UW files from the Internet can contain viruses. Unless you need to edit, it's safer to stay in Protected View View View - Word Terme MOHAMMED MUNTHER MOH Enable Editing Task # 3: Consider two integer arrays x and y of same size n with values: Xo, X1, X3,..., Xe1 and yo, yi, ya..... Yn-1 Write a function merge that receives array x and y and their size. It then returns...

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