Solutions to the Practice Questions#

Arrays Revisited#

  1. a. y[1] b. y[y.length 1] c. y[y.length / 2] or y[6] or y[y.length 7]

  2. char[] myName = {'R', 'o', 's', 'a', 'n', 'n', 'a'};
    for (char letter : myName){
       System.out.print(letter);
    }
    

Back to Questions

2D and Multi-D Arrays#

  1. char[][] anArray = new char[9][9];
    int row = 0;
    int col = 0;
    
    while (row < 9){
       while (col < 9){
          anArray[row][col] = '#';
          col = col + 1;
       }//inner while
       row = row + 1;
       col = 0;
    }//outer while
    
    anArray[4][4] = 'R';
    
  2. int[][][] box = new int[3][4][5]; //initializes to all 0
    box[0][0][0] = 100;
    box[0][3][0] = 100;
    box[2][0][0] = 100;
    box[2][3][0] = 100;
    box[0][0][4] = 100;
    box[0][3][4] = 100;
    box[2][0][4] = 100;
    box[2][3][4] = 100;
    

Back to Questions

for Loop#

  1. char[][][] someArray = new char[3][4][5];
    for (int i = 0; i < someArray.length; i++){
       for (int j = 0; j < someArray[i].length; j++){
          for (int k = 0; k < someArray[i][j].length; k++){
             someArray[i][j][k] = '*';
          }
       }
    }
    
  2. int oddSum = 0;
    for (int num = 1; num <= 29; num = num + 2){
       oddSum = oddSum + num;
    }
    System.out.println("The sum of odd numbers from 1 to 29 is " + 
                           oddSum);
    
  3. /**
     * Takes an integer array and doubles each value.
     *
     * @param intArray which will be changed by each value being
     *      twice the original.
     */
    public static void doubleArrayValues(int[][] intArray){
       for (int row = 0; row < intArray.length; row++){
          for (int col = 0; col < intArray[row].length; col++){
             intArray[row][col] = intArray[row][col] * 2;
          }
       }
    }//doubleArrayValues
    
  4. a. ruby-- and gold = gold + 2
    b. ruby > 0 and gold < kiwi[ruby].length
    c. ruby = kiwi.length - 1 and gold = 0

  5. .

    1. make array in memory
    2. initialize ruby to 3 
       check condition 3 > 0 true
    3. initialize gold to 0
       check condition 0 < 3 true
    4. print kiwi[3][0] * 3 which is 10 * 3 = 30
    3. change gold to 0 + 2 = 2
       check condition 2 < 3 true
    4. print kiwi[3][2] * 3 which is 12 * 3 = 36
    3. change gold to 2 + 2 = 4
       check condition 4 < 3 false
    6. print newline
    2. change ruby to 3 - 1 = 2
       check condition 2 > 0 true
    3. initialize gold to 0
       check condition 0 < 3 true
    4. print kiwi[2][0] * 3 which is 7 * 2 = 14
    3. change gold to 0 + 2 = 2
       check condition 2 < 3 true
    4. print kiwi[2][2] * 3 which is 9 * 2 = 18
    3. change gold to 2 + 2 = 4
       check condition 4 < 3 false
    6. print newline
    2. change ruby to 2 - 1 = 1
       check condition 1 > 0 true
    3. initialize gold to 0
       check condition 0 < 3 true
    4. print kiwi[1][0] * 3 which is 4 * 1 = 4
    3. change gold to 0 + 2 = 2
       check condition 2 < 3 true
    4. print kiwi[1][2] * 3 which is 6 * 1 = 6
    3. change gold to 2 + 2 = 4
       check condition 4 < 3 false
    6. print newline
    2. change ruby to 1 - 1 = 0
       check condition 0 > 0 false
    7.
    
    diagram of the execution showing memory and output
  6. Starting from the bottom row of the array, print the first and third elements multiplied by the row number. Stop just before getting to the top row.

  7. Missing initialization condition i = 10 and incorrect Boolean expression should be i <= 100.

  8. The inner for loop condition should be col < intsGrid[row].length because other wise it is assuming a square array but there may be a different number of rows and columns. Also, in the if statement, the indexing is in the wrong order and should be intsGrid[row][col] in three places, so the row index is first and column index is second.

Back to Questions

Multiple Files#

  1. Square

    public double calcPerimeter(){
       return length * 4;
    }
    

    RightTriangle

    public double calcPerimeter(){
       double hypotenuse = Math.sqrt(base * base + height * height);
       return base + height + hypotenuse;
    }
    

    Circle

    public double calcPerimeter(){
       return 2 * Math.PI * radius;
    }
    
  2. System.out.println("mirror perimeter: " + mirror.calcPerimeter());
    System.out.println("cornerMold perimeter: " + cornerMold.calcPerimeter());
    System.out.println("plate perimeter: " + plate.calcPerimeter());
    
  3. Instance: length Methods: calcArea, calcPerimeter

  4. Instance: base, height Methods: calcArea, calcPerimeter

  5. Instance: radius Methods: calcArea, calcPerimeter

Back to Questions