Advertisement
Crenox

FancyWordTwo Java Program

Apr 16th, 2015
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. 8
  2. PATRIOTS
  3. BEN
  4. FRANKLIN
  5. TOMMY
  6. J
  7. PAINE
  8. HENRY
  9. USA
  10. -----------------------------------------------------------------------------------------------------------------------------
  11. // Name: Sammy Samkough
  12. // Prog: FancyWordTwo
  13. // 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
  14. // other as shown to form a complete word on each side (forwards or backwards as shown).
  15.  
  16. import java.util.Scanner;
  17. import java.util.Arrays;
  18.  
  19. public class FancyWordTwo
  20. {
  21. private String[][] mat;
  22.  
  23. public FancyWordTwo()
  24. {
  25. mat = new String[0][0];
  26. }
  27.  
  28. public FancyWordTwo(String s)
  29. {
  30. // size the matrix
  31. mat = new String[s.length()][s.length()];
  32.  
  33. // use Arrays.fill() to fill in the matrix with spaces
  34. for (int r = 0; r < mat.length; r++)
  35. {
  36. Arrays.fill(mat[r], " ");
  37. }
  38.  
  39. // use a for loop to load in the letters into the matrix
  40. for(int r = 0; r < mat.length; r++)
  41. {
  42. for(int c = 0; c < mat.length; c++)
  43. {
  44. if(r == 0)
  45. {
  46. mat[r][c] = s.substring(c, c + 1);
  47. }
  48. else if(c == 0)
  49. {
  50. mat[r][c] = s.substring(r, r + 1);
  51. }
  52. else if(r == 0 || r == s.length() - 1)
  53. {
  54. mat[r][c] = s.substring(mat.length - c - 1, mat.length - c);
  55. }
  56. else if(c == 0 || c == s.length() - 1)
  57. {
  58. mat[r][c] = s.substring(mat.length - r - 1, mat.length - r);
  59. }
  60. }
  61. }
  62. }
  63.  
  64. /*
  65. public String[] reverseArray(String[] arr)
  66. {
  67. String temp = "";
  68.  
  69. for(int i = 0; i < arr.length / 2; i++)
  70. {
  71. temp = arr[i];
  72. arr[i] = arr[arr.length - i - 1];
  73. arr[arr.length - i - 1] = temp;
  74. }
  75.  
  76. return arr;
  77. }
  78. */
  79.  
  80. public String toString()
  81. {
  82. String output = "";
  83.  
  84. // use nested for loops to build a String from the matrix
  85. for (int r = 0; r < mat.length; r++)
  86. {
  87. output += Arrays.toString(mat[r]) + "\n";
  88. }
  89.  
  90. return output.replace(",", "").replace("[", "").replace("]", "") + "\n";
  91. }
  92. }
  93. -----------------------------------------------------------------------------------------------------------------------------
  94. // Name: Sammy Samkough
  95. // Prog: FancyWordTwo
  96. // 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
  97. // other as shown to form a complete word on each side (forwards or backwards as shown).
  98.  
  99. import java.io.File;
  100. import java.io.IOException;
  101. import java.util.Scanner;
  102.  
  103.  
  104. public class FancyWordTwoRunner
  105. {
  106. public static void main(String args[]) throws IOException
  107. {
  108. Scanner file = new Scanner(new File("fancyword2.dat"));
  109. int numValues = file.nextInt();
  110. file.nextLine(); // clear /n from the buffer
  111.  
  112. for(int i = 0; i < numValues; i++)
  113. {
  114. String line = file.nextLine();
  115. FancyWordTwo one = new FancyWordTwo(line);
  116. System.out.println(one);
  117. }
  118. }
  119. }
  120. /*
  121. P A T R I O T S
  122. A T
  123. T O
  124. R I
  125. I R
  126. O T
  127. T A
  128. S T O I R T A P
  129.  
  130.  
  131. B E N
  132. E E
  133. N E B
  134.  
  135.  
  136. F R A N K L I N
  137. R I
  138. A L
  139. N K
  140. K N
  141. L A
  142. I R
  143. N I L K N A R F
  144.  
  145.  
  146. T O M M Y
  147. O M
  148. M M
  149. M O
  150. Y M M O T
  151.  
  152.  
  153. J
  154.  
  155.  
  156. P A I N E
  157. A N
  158. I I
  159. N A
  160. E N I A P
  161.  
  162.  
  163. H E N R Y
  164. E R
  165. N N
  166. R E
  167. Y R N E H
  168.  
  169.  
  170. U S A
  171. S S
  172. A S U
  173.  
  174.  
  175. Press any key to continue . . .
  176. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement