Methods

Create a class named TestMethods that declares each of the methods below and tests each of the methods rigorously in the main method.

  1. Write a method named sayHi that when called prints to the console the string “Hello”
  2. Write a method named doubleInt that has a single parameter that holds an integer.  The method returns a value that is twice the value that was passed into the method.
  3. Write a method named difference that has two integer parameters.  The method returns the difference of the two integers passed into the method.
  4. Write a method named max that has a variable number of integer parameters.  The method returns the value of the larger argument passed into the method.
  5. Write a method named initArray that has two integer parameters named k and v.  The method returns an integer array of size k and initializes all of elements in the array to the value v.
  6. Write a method named sumArrays that takes two integer arrays with the same length named arr1 and arr2 as arguments and returns a new array named temp of the same length.  For each index i, temp[i] = arr1[i] + arr2[i].
  7. Write a method named elementExists that takes an integer named k and array of integers named arr as an arguments and returns true if the value stored in k is found in the array named arr, and returns false otherwise.
  8. Write a method named getIntegers that has an integer parameter named k and that asks the user for k integers, stores them in an array, and returns a reference to the array.

© 2017 – 2020, Eric. All rights reserved.