2D Arrays Homework
public class two_d_arrays{
}
public class two_d_arrays{
public static int[][] numbers = new int[4][4];
public static void initialize(){
for (int i=0; i<4; i++){
for (int j =0; j<4; j++){
numbers[i][j] = i+j+29;
}
}
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 static void main(String[] args){
two_d_arrays.initialize();
}
}
two_d_arrays.main(null);
public class two_d_arrays{
public static int[][] numbers = new int[4][4];
public static int[][] initialize(){
for (int i=0; i<4; i++){
for (int j =0; j<4; j++){
numbers[i][j] = i+j+29;
}
}
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();
}
return(numbers);
}
public static void reverse(){
int[][] reverse_arr = new int[4][4];
for(int i =0; i < numbers.length; i++)
{
for (int j=0; j<numbers[i].length; j++){
reverse_arr[numbers.length - 1 - i][numbers[i].length - 1 - j] = numbers[i][j];
}
}
for(int i =0; i < reverse_arr.length; i++)
{
for (int j=0; j<reverse_arr[i].length; j++){
System.out.print(reverse_arr[i][j] + " ");
// System.out.println(numbers[i][j]);
}
System.out.println();
}
}
public static void main(String[] args){
System.out.println("Original: ");
two_d_arrays.initialize();
System.out.println();
System.out.println("reversed: ");
two_d_arrays.reverse();
}
}
two_d_arrays.main(null);
import java.util.Scanner;
public class two_d_arrays{
public static int[][] numbers = new int[4][4];
public static int[][] initialize(){
for (int i=0; i<4; i++){
for (int j =0; j<4; j++){
numbers[i][j] = i+j+29;
}
}
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();
}
return(numbers);
}
public static void position(){
Scanner i_obj = new Scanner(System.in); // Create a Scanner object
String i_s = i_obj.nextLine(); // Read user input
int i = Integer.parseInt(i_s);
Scanner j_obj = new Scanner(System.in); // Create a Scanner object
String j_s = j_obj.nextLine(); // Read user input
int j = Integer.parseInt(j_s);
System.out.print(numbers[i][j]);
}
public static void main(String[] args){
System.out.println("Original: ");
two_d_arrays.initialize();
System.out.println();
System.out.println("used scanner object to input row and column: ");
System.out.println("i = 2 ");
System.out.println("j = 3 ");
two_d_arrays.position();
}
}
two_d_arrays.main(null);
public class two_d_arrays{
public static int[][] numbers = new int[4][4];
public static int[][] initialize(){
for (int i=0; i<4; i++){
for (int j =0; j<4; j++){
numbers[i][j] = i+j+29;
}
}
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();
}
return(numbers);
}
public static int product(){
int final_counter = 0;
for (int i=0; i<4; i++){
int row_product = 1;
for (int j=0; j<4; j++){
row_product = row_product * numbers[i][j];
}
final_counter = final_counter + row_product;
}
System.out.println(final_counter);
return final_counter;
}
public static void main(String[] args){
two_d_arrays.initialize();
System.out.println();
two_d_arrays.product();
}
}
two_d_arrays.main(null);
public class two_d_arrays{
public static int[][] numbers = new int[4][4];
public static int[][] initialize(){
for (int i=0; i<4; i++){
for (int j =0; j<4; j++){
numbers[i][j] = i+j+29;
}
}
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();
}
return(numbers);
}
public static void reverse(){
int[][] reverse_arr = new int[4][4];
for(int i =0; i < numbers.length; i++)
{
for (int j=0; j<numbers[i].length; j++){
reverse_arr[numbers.length - 1 - i][numbers[i].length - 1 - j] = numbers[i][j];
}
}
for(int i =0; i < reverse_arr.length; i++)
{
for (int j=0; j<reverse_arr[i].length; j++){
System.out.print(reverse_arr[i][j] + " ");
// System.out.println(numbers[i][j]);
}
System.out.println();
}
}
public static void position(){
Scanner i_obj = new Scanner(System.in); // Create a Scanner object
String i_s = i_obj.nextLine(); // Read user input
int i = Integer.parseInt(i_s);
Scanner j_obj = new Scanner(System.in); // Create a Scanner object
String j_s = j_obj.nextLine(); // Read user input
int j = Integer.parseInt(j_s);
System.out.print(numbers[i][j]);
}
public static int product(){
int final_counter = 0;
for (int i=0; i<4; i++){
int row_product = 1;
for (int j=0; j<4; j++){
row_product = row_product * numbers[i][j];
}
final_counter = final_counter + row_product;
}
System.out.println(final_counter);
return final_counter;
}
public static void main(String[] args){
System.out.println("Original: ");
two_d_arrays.initialize();
System.out.println();
System.out.println("reversed: ");
two_d_arrays.reverse();
System.out.println("Original: ");
two_d_arrays.initialize();
System.out.println();
System.out.println("used scanner object to input row and column: ");
System.out.println("i = 2 ");
System.out.println("j = 3 ");
two_d_arrays.position();
two_d_arrays.product();
}
}
two_d_arrays.main(null);