-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava2DArrays Assignment
More file actions
88 lines (71 loc) · 2.21 KB
/
Java2DArrays Assignment
File metadata and controls
88 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.java2darrays;
// Import important modules
import java.util.Arrays;
import java.util.Scanner;
/**
*
* Java Array Flipping Assignments
*
* This program modifies an existing grid based on user-inputted instructions.
* The possible modifications are a vertical and a horizontal swap
* @author Chibueze Benneth
* @version Java25
* @since 2025-11-01
*/
public class Java2DArrays {
public static void main(String[] args) {
// This is the default array
int [][] grid = {{1,2},
{3,4}
};
// Take user input
Scanner input = new Scanner(System.in);
String word = input.nextLine();
String[] words = word.split("");
// Check what operation is being called
for (String letter:words) {
if (letter.equals("V")) {
vFlip(grid);
}
if (letter.equals("H")) {
hFlip(grid);
}
}
input.close();
// Print results
printGrid(grid);
}
// Vertical Flip method
public static void vFlip(int[][] arr2D) {
// swap along the vertical axis
int temp = arr2D[0][0];
int temp2 = arr2D[1][0];
arr2D[0][0] = arr2D[0][1];
arr2D[0][1] = temp;
arr2D[1][0] = arr2D[1][1];
arr2D[1][1] = temp2;
}
// Horizontal Flip method
public static void hFlip(int[][] arr2D) {
// swap along the horizontal axis
int temp = arr2D[0][0];
int temp2 = arr2D[0][1];
arr2D[0][0] = arr2D[1][0];
arr2D[1][0] = temp;
arr2D[0][1] = arr2D[1][1];
arr2D[1][1] = temp2;
}
// method that prints out the numbers
public static void printGrid(int[][] arr2D) {
for (int [] row:arr2D) {
// Nested for loop
for (int num:row) {
System.out.print((num + " "));
}
System.out.println(); // print new line after each array is printed
}
}
}