Solutions to the Practice Questions#
Arrays Revisited#
a.
y[1]
b.y[y.length – 1]
c.y[y.length / 2]
ory[6]
ory[y.length – 7]
char[] myName = {'R', 'o', 's', 'a', 'n', 'n', 'a'}; for (char letter : myName){ System.out.print(letter); }
2D and Multi-D Arrays#
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';
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;
for Loop#
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] = '*'; } } }
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);
/** * 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
a.
ruby--
andgold = gold + 2
b.ruby > 0
andgold < kiwi[ruby].length
c.ruby = kiwi.length - 1
andgold = 0
.
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.
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.
Missing initialization condition
i = 10
and incorrect Boolean expression should bei <= 100
.The inner
for
loop condition should becol < 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 thei
f statement, the indexing is in the wrong order and should beintsGrid[row][col]
in three places, so the row index is first and column index is second.
Multiple Files#
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; }
System.out.println("mirror perimeter: " + mirror.calcPerimeter()); System.out.println("cornerMold perimeter: " + cornerMold.calcPerimeter()); System.out.println("plate perimeter: " + plate.calcPerimeter());
Instance:
length
Methods:calcArea
,calcPerimeter
Instance:
base
,height
Methods:calcArea
,calcPerimeter
Instance:
radius
Methods:calcArea
,calcPerimeter