Write a Java program that reads in lines of text from a file then prints out the
ID: 3545063 • Letter: W
Question
Write a Java program that reads in lines of text from a file then prints out the text with different transformations around the vertical, horizontal, and diagonal axis. The program will accomplish this by reading in the text as a rectangular block of data into a 2D array of characters (char[][]). The program will create only one 2D array and will not copy from one array to another. The dimensions of the 2D array will be contained in the first line of the input file, row first and then columns.
This is whats contained in the file:
4 4
JAVA
PROG
NUMB
FOUR
Apparently, the first 2 integer are there to tell us the row and columns according to our professor. so all we need to transform is the four words below that. And i have trouble reading Characters into the 2D array and tranform them around the vertical horizontal and diagonal axis.
The output supposed to be like this:
AVAJ
GORP
BMUN
RUOF
FOUR
NUMB
PROG
JAVA
RBGA
UMOV
OURA
FNPJ
Explanation / Answer
// Transform.javaimport java.io.*; class Transform { public static void main(String[] args) throws IOException { FileInputStream input = new FileInputStream("input.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(input)); PrintWriter pw = new PrintWriter("output.txt"); String line; String[] split; char[] chars; int x, y;
try { line = br.readLine(); split = line.split(" "); x = Integer.parseInt(split[0]); y = Integer.parseInt(split[1]); char[][] data = new char[x][y];
for(int i = 0; i < x; i++) { line = br.readLine(); chars = line.toCharArray(); for(int j = 0; j < y; j++) data[i][j] = chars[j]; }
char[][] data2 = copyArray(data, x, y);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[i][y - 1 - j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[x - 1 - i][j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < y; i++) { for(int j = y - 1; j >= 0; j--) { data2[x - 1 - i][j] = data[y - 1 - j][i]; } } printArray(data2, x, y, pw); } catch(IOException e) { System.out.println(e); return; } finally { br.close(); input.close(); pw.close(); } }
static char[][] copyArray(char[][] orig, int x, int y) { char[][] copy = new char[x][y];
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { copy[i][j] = orig[i][j]; } }
return copy; }
static void printArray(char[][] data, int x, int y, PrintWriter pw) { for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { pw.print(data[i][j]); } pw.println(); }
pw.println(); } }
// Transform.java
import java.io.*; class Transform { public static void main(String[] args) throws IOException { FileInputStream input = new FileInputStream("input.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(input)); PrintWriter pw = new PrintWriter("output.txt"); String line; String[] split; char[] chars; int x, y;
try { line = br.readLine(); split = line.split(" "); x = Integer.parseInt(split[0]); y = Integer.parseInt(split[1]); char[][] data = new char[x][y];
for(int i = 0; i < x; i++) { line = br.readLine(); chars = line.toCharArray(); for(int j = 0; j < y; j++) data[i][j] = chars[j]; }
char[][] data2 = copyArray(data, x, y);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[i][y - 1 - j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[x - 1 - i][j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < y; i++) { for(int j = y - 1; j >= 0; j--) { data2[x - 1 - i][j] = data[y - 1 - j][i]; } } printArray(data2, x, y, pw); } catch(IOException e) { System.out.println(e); return; } finally { br.close(); input.close(); pw.close(); } }
static char[][] copyArray(char[][] orig, int x, int y) { char[][] copy = new char[x][y];
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { copy[i][j] = orig[i][j]; } }
return copy; }
static void printArray(char[][] data, int x, int y, PrintWriter pw) { for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { pw.print(data[i][j]); } pw.println(); }
pw.println(); } }
// Transform.java
import java.io.*; class Transform { public static void main(String[] args) throws IOException { FileInputStream input = new FileInputStream("input.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(input)); PrintWriter pw = new PrintWriter("output.txt"); String line; String[] split; char[] chars; int x, y;
try { line = br.readLine(); split = line.split(" "); x = Integer.parseInt(split[0]); y = Integer.parseInt(split[1]); char[][] data = new char[x][y];
for(int i = 0; i < x; i++) { line = br.readLine(); chars = line.toCharArray(); for(int j = 0; j < y; j++) data[i][j] = chars[j]; }
char[][] data2 = copyArray(data, x, y);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[i][y - 1 - j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { data2[i][j] = data[x - 1 - i][j]; } }
printArray(data2, x, y, pw);
for(int i = 0; i < y; i++) { for(int j = y - 1; j >= 0; j--) { data2[x - 1 - i][j] = data[y - 1 - j][i]; } } printArray(data2, x, y, pw); } catch(IOException e) { System.out.println(e); return; } finally { br.close(); input.close(); pw.close(); } }
static char[][] copyArray(char[][] orig, int x, int y) { char[][] copy = new char[x][y];
for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { copy[i][j] = orig[i][j]; } }
return copy; }
static void printArray(char[][] data, int x, int y, PrintWriter pw) { for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { pw.print(data[i][j]); } pw.println(); }
pw.println(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.