CS 139 Algorithm Development - Practice coding

The following examples will give you practice in coding basic functions and methods in java. If you want to verify the accuracy of your work, please see one of the TA's during their consulting hours or see your instructor during his/her office hours.

The functions are grouped in order from easiest to hardest. Assume all functions are public and static.

Basic functions not involving loops

  1. Create a function, negative, that accepts a double number and returns a boolean value. Return true if the number is negative, false if it is positive or 0.
  2. Create a function, halve, that accepts a double number as a parameter and returns a double value that is one half of the original number.
  3. Create a function, evenlyDivides, that accepts two integer numbers and returns true if the first number is evenly divisible by the second number and false otherwise.
  4. Create a function, convertGal, that accepts a single double parameter representing the number of gallons and returns a double value representing the equivalent number of liters. 1 gallon = 3.785 liters.
  5. Create a function, isOdd, that accepts a single integer value and returns a boolean value true if the value is odd and false otherwise.
  6. Create a function, isEven, that accepts a single integer value and returns a boolean value true if the value is even and false otherwise.
  7. Write a method called floatEquals, that accepts three double numbers; two numbers to compare and one number representing a tolerance. The method should return true if the first two numbers are the same to within the tolerance of the third number and false otherwise.

Functions involving loops

  1. Write a function checkNumber that accepts an int value, a Scanner object, and a String object, and checks to see if value is greater than 0. If it is not, the function should prompt the user for a valid number using the String parameter and read the number from the keyboard until the user enters a valid (positive) number. The function should return the valid positive int.
  2. Write a function called reverse that accepts a String as a parameter and returns a new String which is the original String in reverse order.
  3. Write a function, isPrime that accepts an int parameter and returns true if the integer is a prime number and false otherwise. (A prime number is divisible only by itself and 1.)
  4. Write a function called gcd that accepts two int parameters and returns the int value of the greatest common divisor of the two.
  5. Write a function called lcm that accepts two int parameters and returns the int value of the smallest number that is a multiple of both. (Note: the lcm of 2 and 4 is 4, while the lcm of 3 and 4 is 12).
  6. Write a method that given a positive integer will print all of the powers of two from 20 to 2n, where n is the passed value. Note, 0 is a legitimate parameter value.

Functions involving arrays of primitives

  • Write a function, sumArray, which accepts an array of double values and returns a double representing the sum of the values in the array.
  • Write a function, average, which accepts an array of double values and returns a double value representing the mean (average) of the values in the array.
  • Write a function, klone that accepts an array of int values and returns a new array of int s containing the same values as the original array.
  • Write a function, makeArray, that accepts an int representing the size of the array and an int representing an initialization value. The function should return an array built to the size parameter with all of the values initialized to the initialization value.
  • Write a function, increaseSize, that accepts an array of int and an int value and returns a new array containing the contents of the array parameter, but is larger by the int value.

    Functions involving arrays of Card objects

  • Write a method makeHand that accepts a single int parameter, size, and returns an array of Card which has size random Card elements.
  • Write a method removeCard that accepts an int parameter, location, and a Card array, hand , and returns the Card found at that location. If the location does not exist in the hand, return null.
  • Write a method reverse that accepts an array of Card objects and returns an array of Card objects in the reverse order.
  • Write a method countNull that accepts an array of Card objects and returns an int representing the number of null entries in the array.
  • Write a method countCard that accepts an array of Card objects and returns an int representing the number of Card objects in the array.
  • Write a method printRank that accepts an array of Card objects and prints the value of each of the Card ranks, one per line. This function does not return any value.
  • Write a method isEqual that accepts two Card arrays and returns true if the Card s in both of the arrays are the same and false otherwise. Return false if there are a different number of Card objects in the arrays.
  • Write a method union that accepts two Card arrays and returns a new array of Card which is the two arrays combined.
    Updated 11/28/05