CSCI 161 - Lab 8: Flip Objectives The objectives of this lab are to reinforce an
ID: 3708557 • Letter: C
Question
CSCI 161 - Lab 8: Flip
Objectives
The objectives of this lab are to reinforce and help teach:
file I/O, in particular reading a text file of know line length and line number into a two-dimensional array
use of a two-dimensional array to hold char values
writing nested for loops
Overview
In this lab you will write a program that reads a text file into a two-dimensional array. The text file contains characters that represent an "ascii-art" letter. As such, the text file has exactly 7 lines, with each line having 15 characters. Your program will print out the "letter", then it will vertically flip the letter, and lastly print the flipped letter.
Instructions
Following are the instructions and you should follow them carefully:
Using Eclipse create a new Java Project and name it: Lab8
As with all your labs and assignments, add a class of the same name, Lab8, and include in that class a main method.
Think about how to deconstruct the problem. Your program must literally do the following tasks, in the following order:
read the letter from the file
print the letter
flip the letter
print the flipped letter (hey, we already know how to print the letter)
Given the above steps you should already be thinking that you need three methods:
One that takes as input a scanner class and reads the file and returns the multi-dimensional array of characters
One that prints the letter out when passed to it the two-dimensional array.
Another that flips the contents of the two-dimensional array that it is passed.
If done properly, your main method has 5 statements: one that declares and initializes your file scanner class, another to read the file into and return the multi-dimensional array, a third that prints the letter given the array, a fourth that flips the content of the array passed to it, and lastly, a repeated call to the method that prints the array.
Your program must read a file according to the "Input Specification" below.
Your program must output according to the "Output Specification" below.
Iteratively develop the assignment, get the file reading working, get the letter printing working, etc.
When the assignment is complete and working submit it as Lab8
Input Specification
Your program shall read the letter.txt file from the current directory. The file has exactly 7 lines and each line has exactly 15 characters. Each character of each line could be anything, but expect that the file will represent an ASCII-art letter composed of space characters and other characters that represent the letter.
For example, the capital letter "T" would be represented by a file containing the following characters:
When creating your own sample letter.txt file make sure that it has exactly 7 lines and each line has exactly 15 characters.
Output Specification
Your program should read the text file into a two-dimensional array, print out the file contents from the two dimensional array, print a blank line, flip the contents of the two-dimensional array vertically, and then print the resulting letter.
Example output, given the letter T as described above is as follows:
Explanation / Answer
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner("letter.txt");
char[][] arr=new char[7][15];
int i,j;
for(i=0;i<7;i++){
String temp = sc.next();
for(j=0;j<15;j++){
arr[i][j] = temp.charAt(j);
}
}
display(arr);
displayFlip(arr);
}
public static void display(char[][] arr){
for(int i=0;i<7;i++){
for(int j=0;j<15;j++){
System.out.print(arr[i][j]);
}
System.out.println("");
}
}
public static void displayFlip(char[][] arr){
// Print from last row to first row
for(int i=6;i>=0;i--){
// Column printing order remains same
for(int j=0;j<15;j++){
System.out.print(arr[i][j]);
}
System.out.println("");
}
}
}