Unit 8 2D Arrays Homework
import java.util.Scanner;
public class Arrays{
int[][] numbers = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
public void printArray(){
System.out.println("\nForwards");
for(int i = 0; i < numbers.length; i++){
for(int j = 0; j < numbers[i].length; j++){
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}
public void reverseArray() {
System.out.println("\nReverse");
for (int i = numbers.length - 1; i >= 0; i--){
for(int j = numbers[i].length - 1; j >= 0; j--) {
System.out.print(numbers[i][j]+ " ");
}
System.out.println();
}
}
public void askForIndex(){
System.out.println("\nAsk for Input");
Scanner scanner = new Scanner(System.in);
System.out.print("Row: ");
int rowIndex = scanner.nextInt();
System.out.println(rowIndex);
System.out.print("Column: ");
int columnIndex = scanner.nextInt();
System.out.println(columnIndex);
System.out.println("Result = " + numbers[rowIndex][columnIndex]);
}
public void multiplyThenSum(){
System.out.println("\nSum of Row Products");
int a = 1;
int b = 0;
for (int j = 0; j < numbers.length; j++) {
for(int i = 0; i < numbers.length; i++){
a = a*numbers[j][i];
}
b = b + a;
System.out.println("product of row " + j + " = " + a);
a = 1;
}
System.out.print("sum of rows = " + b);
}
public static void main(String[] args){
Arrays array = new Arrays();
array.printArray();
array.reverseArray();
array.askForIndex();
array.multiplyThenSum();
}
}
Arrays.main(null);
class CarsLoop {
String [][] cars;
String [][] cthulhus;
//hint: missing code
public CarsLoop() {
cars = new String[][]{
{
" ______ ", //[0][0] eyes
},
{
" /|_||_\\`.__ ", //[1][0] chin
},
{
"( _ _ _\\ ", //[2][0] body
},
{
"=`-(_)--(_)-' " //[3][0] legs
},
};
cthulhus = new String[][]{
{
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⡀⠀⠀⠀⠀⠀⠀⢎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ _________________________",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⢱⠀⠀⢀⣤⡀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀| | | | | | \\",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠻⠉⣧⣿⣿⣿⠀⠀⢸⠇⠀⠐⠉⡆⠀⠀⠀⠀⠀|___|_____|_____|____|_|___\\",
"⠀⠀⠀⠀⢀⠔⠒⢦⠀⢻⡄⠀⢿⣻⣿⡿⢀⣴⣋⣄⣄⣌⣠⠃⠀⠀⠀⠀| | | \\",
"⠀⠀⠀⠀⠈⠀⢀⡞⠀⠈⠛⣷⣾⣿⣿⣿⣿⣿⣯⣥⣀⠀⠀⠀⠀⠀⠀⠀⠀ `--(o)(o)--------------(o)--'",
"⠀⠀⠀⠀⠀⠀⠈⠷⣦⣴⡾⢿⣿⡿⢿⣿⣋⣽⠶⢦⠙⢷⡀⠀⠀⠀⠀⠀⠀⠀ ",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⢀⡆⠈⠉⠁⡄⠈⡇⠘⢇⠀⢈⡆⠀⠀⠀⠀ ",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡯⠀⠸⠁⠀⠀⠸⣧⡀⡇⠀⠈⠉⠉⠀⠀⠀⠀⠀ ",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⡴⠁⠀⠀⠀⠀⠙⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀ "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("A story of a Car and Cthulhus");
int carsCount = cars.length;
int cthulhusCount = cthulhus[0].length;
for (int i = 0; i < carsCount; i++)
{
System.out.println(cars[i][0]);
}
for (int j = 0; j < cthulhusCount; j++)
{
System.out.println(cthulhus[0][j]);
}
System.out.println("0000000000000000000000000000000000");
System.out.println(" THE END ");
}
public static void main(String[] args) {
new CarsLoop().printPoem();
}
}
CarsLoop.main(null);