package utils; /** * * @author Dung X. Nguyen */ public class Utility { /** * Prints an array of int for indices ranging from lo to hi. The printed array * should be bracketted by by matching curly braces. The array elements should * be separated by commas. * Examples: * The empty array should print as {}. * The array containing the single int -3 should print as {-3}. * The array containing -3 and 5 in this order should print as {-3, 5}. * * @param A an array of int. * @param lo an index in the range [0:A.length-1]. * @param hi an index in the range [0:A.length-1]. */ public static void printArray(int[] A, int lo, int hi) { if (hi < lo) { System.out.println ("{}"); return; } System.out.print ("{"); for (int i = lo; i < hi; i++) { System.out.print(A[i] + ", "); } System.out.println (A[hi] + "}"); } /** * Computes and returns x to the n-th power. * @param x * @param n >= 0. * @return x to the n-th power. */ public static int power(int x, int n) { int iP = 1; for (int i = 1; i <= n; i++) { iP *= x; } return iP; } }