Question

In: Computer Science

JAVA CODE // Write a method called example that will do several things. // It has...

JAVA CODE

// Write a method called example that will do several things.

// It has two integer array parameters of unknown varying lengths.

// It returns an integer array that contains each of the first array values

// divided by two and each of the second array values added to the number 100.

// It prints each value of the first array and then prints that value

// divided by two on a separate line. It then prints each value of the second

// array and that value added to 100 on a separate line. It then adds the

// values of the first array and prints the sum with an appropriate label and

// adds the values of the second array and prints the sum. It then returns the

// combined array.

Solutions

Expert Solution

import java.util.Arrays;
public class MyClass {
public static int[] example(int[] first, int[] second)
{
int[] result = new int[first.length+second.length];
int index = 0;
for(int x:first)
result[index++] = x/2;
for(int x:second)
result[index++] = x+100;
  
System.out.println("Values of the first array: ");
for(int x:first)
System.out.print(x+ " ");
  
System.out.println("\n\nValues of the first array/2: ");
for(int i=0; i<first.length; i++)
System.out.print(result[i]+ " ");
  
System.out.println("\n\nValues of the second array: ");
for(int x:first)
System.out.print(x+ " ");
  
System.out.println("\n\nValues of the second array+100: ");
for(int i=first.length; i<result.length; i++)
System.out.print(result[i]+ " ");
  
int sumFirst = 0;
for(int x:first)
sumFirst += x;
System.out.println("\n\nSum of the first array = " + sumFirst);
  
int sumSecond = 0;
for(int x:second)
sumSecond += x;
System.out.println("Sum of the second array = " + sumSecond);
  
return result;
}
public static void main(String args[]) {
System.out.println("\nArray returned is" + Arrays.toString(example(new int[]{10, 20, 30, 40}, new int[]{5, 6, 7, 8, 9})));
}
}

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT