Advertisement
Crenox

FancyWord Java Program

Apr 14th, 2015
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. 6
  2. HILLS
  3. AP
  4. COMPUTERS
  5. GOSLING
  6. INT
  7. A
  8. -------------------------------------------------------------------------------------------------------------------------------
  9. // Name: Sammy Samkough
  10. // Prog: FancyWord
  11. // Spec: Read in one word at a time from the file and output the word as an hourglass as shown – words connect along diagonal
  12. // and form complete words from one point to another – either forwards or backwards as shown.
  13.  
  14. import java.util.Arrays;
  15.  
  16. public class FancyWord
  17. {
  18. private String[][] mat;
  19.  
  20. public FancyWord()
  21. {
  22. mat = new String[0][0];
  23. }
  24.  
  25. public FancyWord(String s)
  26. {
  27. // size the matrix
  28. mat = new String[s.length()][s.length()];
  29.  
  30. // use Arrays.fill() to fill in the matrix with spaces
  31. for (int r = 0; r < mat.length; r++)
  32. {
  33. Arrays.fill(mat[r], " ");
  34. }
  35.  
  36. // use a for loop to load in the letters into the matrix
  37. for(int r = 0; r < mat.length; r++)
  38. {
  39. for(int c = 0; c < mat.length; c++)
  40. {
  41. if(r == c)
  42. {
  43. mat[r][c] = s.substring(c, c + 1);
  44. }
  45. else if(r == 0 || r == s.length() - 1)
  46. {
  47. mat[r][c] = s.substring(c, c + 1);
  48. }
  49. else if((r + c) == s.length() - 1)
  50. {
  51. mat[r][c] = s.substring(c, c + 1);
  52. }
  53. }
  54. }
  55. }
  56.  
  57. public String toString()
  58. {
  59. String output = "";
  60.  
  61. // use nested for loops to build a String from the matrix
  62. for (int r = 0; r < mat.length; r++)
  63. {
  64. output += Arrays.toString(mat[r]) + "\n";
  65. }
  66.  
  67. return output.replace(",", "").replace("[", "").replace("]", "") + "\n";
  68. }
  69. }
  70. -------------------------------------------------------------------------------------------------------------------------------
  71. // Name: Sammy Samkough
  72. // Prog: FancyWord
  73. // Spec: Read in one word at a time from the file and output the word as an hourglass as shown – words connect along diagonal
  74. // and form complete words from one point to another – either forwards or backwards as shown.
  75.  
  76. import java.io.File;
  77. import java.io.IOException;
  78. import java.util.Scanner;
  79.  
  80. public class FancyWordRunner
  81. {
  82. public static void main(String args[]) throws IOException
  83. {
  84. Scanner file = new Scanner(new File ("fancyword.dat"));
  85. String word;
  86. FancyWord fw;
  87. int size = file.nextInt();
  88. file.nextLine();
  89.  
  90. for(int i = 0; i < size; i++)
  91. {
  92. word = file.nextLine();
  93. fw = new FancyWord(word);
  94. System.out.println(fw);
  95. }
  96. }
  97. }
  98.  
  99. /*
  100. H I L L S
  101. I L
  102. L
  103. I L
  104. H I L L S
  105.  
  106.  
  107. A P
  108. A P
  109.  
  110.  
  111. C O M P U T E R S
  112. O R
  113. M E
  114. P T
  115. U
  116. P T
  117. M E
  118. O R
  119. C O M P U T E R S
  120.  
  121.  
  122. G O S L I N G
  123. O N
  124. S I
  125. L
  126. S I
  127. O N
  128. G O S L I N G
  129.  
  130.  
  131. I N T
  132. N
  133. I N T
  134.  
  135.  
  136. A
  137.  
  138.  
  139. Press any key to continue . . .
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement