Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 8
- PATRIOTS
- BEN
- FRANKLIN
- TOMMY
- J
- PAINE
- HENRY
- USA
- -----------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: FancyWordTwo
- // Spec: Read in one word at a time from the file and output the word as a square where the sides and bottom connect to each
- // other as shown to form a complete word on each side (forwards or backwards as shown).
- import java.util.Scanner;
- import java.util.Arrays;
- public class FancyWordTwo
- {
- private String[][] mat;
- public FancyWordTwo()
- {
- mat = new String[0][0];
- }
- public FancyWordTwo(String s)
- {
- // size the matrix
- mat = new String[s.length()][s.length()];
- // use Arrays.fill() to fill in the matrix with spaces
- for (int r = 0; r < mat.length; r++)
- {
- Arrays.fill(mat[r], " ");
- }
- // use a for loop to load in the letters into the matrix
- for(int r = 0; r < mat.length; r++)
- {
- for(int c = 0; c < mat.length; c++)
- {
- if(r == 0)
- {
- mat[r][c] = s.substring(c, c + 1);
- }
- else if(c == 0)
- {
- mat[r][c] = s.substring(r, r + 1);
- }
- else if(r == 0 || r == s.length() - 1)
- {
- mat[r][c] = s.substring(mat.length - c - 1, mat.length - c);
- }
- else if(c == 0 || c == s.length() - 1)
- {
- mat[r][c] = s.substring(mat.length - r - 1, mat.length - r);
- }
- }
- }
- }
- /*
- public String[] reverseArray(String[] arr)
- {
- String temp = "";
- for(int i = 0; i < arr.length / 2; i++)
- {
- temp = arr[i];
- arr[i] = arr[arr.length - i - 1];
- arr[arr.length - i - 1] = temp;
- }
- return arr;
- }
- */
- public String toString()
- {
- String output = "";
- // use nested for loops to build a String from the matrix
- for (int r = 0; r < mat.length; r++)
- {
- output += Arrays.toString(mat[r]) + "\n";
- }
- return output.replace(",", "").replace("[", "").replace("]", "") + "\n";
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: FancyWordTwo
- // Spec: Read in one word at a time from the file and output the word as a square where the sides and bottom connect to each
- // other as shown to form a complete word on each side (forwards or backwards as shown).
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- public class FancyWordTwoRunner
- {
- public static void main(String args[]) throws IOException
- {
- Scanner file = new Scanner(new File("fancyword2.dat"));
- int numValues = file.nextInt();
- file.nextLine(); // clear /n from the buffer
- for(int i = 0; i < numValues; i++)
- {
- String line = file.nextLine();
- FancyWordTwo one = new FancyWordTwo(line);
- System.out.println(one);
- }
- }
- }
- /*
- P A T R I O T S
- A T
- T O
- R I
- I R
- O T
- T A
- S T O I R T A P
- B E N
- E E
- N E B
- F R A N K L I N
- R I
- A L
- N K
- K N
- L A
- I R
- N I L K N A R F
- T O M M Y
- O M
- M M
- M O
- Y M M O T
- J
- P A I N E
- A N
- I I
- N A
- E N I A P
- H E N R Y
- E R
- N N
- R E
- Y R N E H
- U S A
- S S
- A S U
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement