The following program asks you to take a single word and use it to form a simple square frame, where the word is printed four times, once each for one of the four sides of the square: The corners and the centre of the square must contain stars. On top of the square the word is printed from left to right, containing a single space between letters as shown in the sample. On the bottom of the square the word is printed from right to left, also with spaces between letters. On the right the word reads from top to bottom without blank spaces and on the left the word reads from botom to top.
DATA11.txt (DATA12.txt for the second try) contains 5 words on 5 separate lines. Each word contains less than 20 characters. Write a program to create a square as described above for each word. The program should pause between squares and proceed to the next one under input control
Sample Input
CANADA
MAPLE
TO
FIRE
SHORT
Sample Output
* C A N A D A *
A * * * * * * C
D * * * * * * A
A * * * * * * N
N * * * * * * A
A * * * * * * D
C * * * * * * A
* A D A N A C *
* M A P L E *
E * * * * * M
L * * * * * A
P * * * * * P
A * * * * * L
M * * * * * E
* E L P A M *
* T O *
O * * T
T * * O
* O T *
* F I R E *
E * * * * F
R * * * * I
I * * * * R
F * * * * E
* E R I F *
* S H O R T *
T * * * * * S
R * * * * * H
O * * * * * O
H * * * * * R
S * * * * * T
* T R O H S *
import java.util.Scanner; import java.util.ArrayList; import java.io.File; public class Problem_1_Word_Frame { public static void main(String[] args) { try { Scanner scanner = new Scanner(new File("C:\\Users\\Mike\\Desktop\\problem_1_word_frame_DATA11.txt")); ArrayList<String> list = new ArrayList<String>(); // read in the file to the ArrayList while (scanner.hasNextLine()) { list.add(scanner.nextLine()); } for (String word : list) { // print first line System.out.print("* "); for (int i = 0; i < word.length(); i++) { System.out.print(word.charAt(i) + " "); } System.out.println("*"); // print middle content for (int i = 0; i < word.length(); i++) { System.out.println(word.charAt(word.length() - 1- i) + stars(word.length()) + word.charAt(i)); } // print last line System.out.print("* "); for (int i = word.length() - 1; i > -1; i--) { System.out.print(word.charAt(i) + " "); } System.out.println("*\n"); } } catch (Exception e) { System.out.println("Exception"); } } /* returns a string with n number of stars */ public static String stars(int n) { String s = " "; for (int i = 0; i < n; i++) { s += "* "; } return s; } }
Using their sample input:
CANADA MAPLE TO FIRE SHORT
And the output to that is:
* C A N A D A * A * * * * * * C D * * * * * * A A * * * * * * N N * * * * * * A A * * * * * * D C * * * * * * A * A D A N A C * * M A P L E * E * * * * * M L * * * * * A P * * * * * P A * * * * * L M * * * * * E * E L P A M * * T O * O * * T T * * O * O T * * F I R E * E * * * * F R * * * * I I * * * * R F * * * * E * E R I F * * S H O R T * T * * * * * S R * * * * * H O * * * * * O H * * * * * R S * * * * * T * T R O H S *DOWNLOAD as .txt
Created: April 1, 2014
Completed in full by: Michael Yaworski