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

JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
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.
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.
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
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)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT