Unit 8 Homework
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
// Print the last element in the array!
System.out.println(arr[arr.length-1][arr[0].length-1]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Change Austin to Athens and print!
System.out.println("Change Austin to Athens and print!");
arr[2][0] = "Athens";
System.out.println(arr[2][0]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Print out the array without using numerical values!
int row = arr.length;
int col = arr[0].length;
for(int x = 0; x < row; x++) {
for(int y = 0;y < col; y++) {
System.out.println(arr[x][y]);
}
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0];
// Use nested for loops to find the longest or shortest string!
int row = arr.length;
int col = arr[0].length;
for(int x = 0; x < row; x++) {
for(int y = 0;y < col; y++) {
if(arr[x][y].length() > longest.length()) {
longest = arr[x][y];
}
}
}
System.out.println(longest);
}
}
Test.main(null);
public static Position findPosition(int num, int[][] intArr) {
for (int row=0; row < intArr.length; row++) {
for (int col=0; col < intArr[0].length; col++) {
if (intArr[row][col] == num) {
return new Position(row, col);
}
}
}
return null;
}
public static Position[][] getSuccessorArray(int[][] intArr) {
Position[][] newArr = new Position[intArr.length][intArr[0].length];
for (int row=0; row < intArr.length; row++) {
for (int col=0; col < intArr[0].length; col++) {
newArr[row][col] = findPosition(intArr[row][col]+1, intArr);
}
}
return newArr;
}
public class xmasTree {
public static void main(String args[]) {
String[][] tree = {
{" "," "," ","*"," "," "," "},
{" "," ","*","*","*"," "," "},
{" ","*","*","*","*","*"," "},
{"*","*","*","*","*","*","*"},
{" "," "," ","M"," "," "," "}
};
int row = tree.length;
int col = tree[0].length;
for(int r = 0; r < row; r++) {
for(int c = 0; c < col; c++) {
System.out.print(tree[r][c]);
}
System.out.println(" ");
}
}
}
xmasTree.main(null);