Strings

  1. Without explicitly calling any constructor in the String class, create a String that holds “Grace” and store its reference in a variable named firstName.
  2. Using the String constructor that takes a string literal as an argument, create a String that holds “Bob” and store its reference in a variable named name.
  3. Suppose an array named array has length 10 and holds 10 characters. Write a single statement that uses the first 5 characters in the array to initialize a String and store a reference to the String in a variable named firstFive.
  4. Suppose a variable named fullName holds a reference to a String. Write a single statement that creates a new String using all of the characters in fullName except the first and the last and sets a variable named subStr equal to the reference of the new String.
  5. Suppose variables str1 and str2 hold references to Strings. Write a block of code that prints “Same” if they refer to the same String on the heap.
  6. Suppose variables str3 and str4 hold references to Strings. Write a block of code that prints “Equal” if they store the same sequence of characters.
  7. Write a statement that stores in a variable named len the length of the character sequence stored in a String referenced by a variable named
  8. Suppose a variable named age is of type Integer. Write a single statement that creates a variable of type String that references a String that holds the printable representation of the Integer referenced by age.

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.

Arrays

  1. Write a fragment of code that creates an array named array of length 10 that holds 10 integers and initializes the array with random numbers between 1 and 10 (inclusively).
  2. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that sets the elements in the array with even indices to 0.
  3. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that sets the elements in the array with values greater than 5 to 0.
  4. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that creates a second array named array2 of length 10 and initializes array2 with the same values that are in array and in the same order.
  5. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that prints the values that are in array in reverse order.
  6. Write a program that creates an array named array of size 10 and initializes it with values read in from the keyboard. The program then passes the array to a function named sumArray and prints the value returned by the function. The sumArray function has one parameter and returns the sum of the values in the array that is passed into the function.
  7. Write a program that asks the user for an integer, passes the integer to a method named createArray, and prints the contents of the array that is returned by createArray. The function named createArray has one integer parameter and creates an array with a length equal to the value of the argument passed into the function.  The function should initializes the array with the values {1, 2, …, n} and return the array.
  8. Write a program that asks the user to enter an integer. The program then reads in the integer and creates an uninitialized array of size equal to the value read.  The program passes the uninitialized array to a method named initArray then prints the contents of the array after initArray returns.  The method initArray takes an array of integers as an argument and has a void return type.  The function populates the array that is passed into the function with random values between 0 and 9.
  9. Write a function named initArray that has a variable number of integers as its parameter.  The method creates an array of length equal to the number of integers passed into the function and populates the array with the values passed into the method.  The function should return a reference to the array that was created.
  10. Write a function named initArray2 that takes an array of integers as the first parameter and a variable number of integers as the second parameter. The function copies the values in the second parameter into the array, so long as there is room in the array (starting at index 0). The function returns the number of elements that were copied into the array.
  11. Write a fragment of code that creates a 3 x 3 2D array of characters named matrix with the following values:
c a t
a t e
t e a
  1. Write a fragment of code that creates a 3 x 3 2D array of integers named matrix3 and that using nested for-loops initializes the 2D array with following values:
1 0 0
0 1 0
0 0 1
  1. Assume there is a 3 x 3 2D array of characters name matrix. Write a fragment of code that creates a 3 x 3 2D array named matrix2 and populates matrix2 with the values in matrix (in the same order).
  2. Assume there is an n x m 2D array of integers name matrix3. Write a fragment of code that creates an n x m 2D array named matrix4 and populates matrix4 with the values in matrix3 (in the same order).