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; } /** * Tests printArray(...) and power(...). * @param args */ public static void main(String[] args) { int[] A0 = {}; printArray (A0, 0, A0.length - 1); int[] A1 = {-3}; printArray (A1, 0, A1.length - 1); int[] A = {99, 0 , -8, 55, 0, 23}; printArray (A, 1, A.length - 2); System.out.println ("power (-2, 0) = " + power (-2, 0)); System.out.println ("power (-2, 1) = " + power (-2, 1)); System.out.println ("power (-2, 2) = " + power (-2, 2)); } }