Guest User

Untitled

a guest
Feb 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4.  
  5. public class Template {
  6.  
  7. public static void main(String args[]) {
  8.  
  9. BufferedWriter writer = null;
  10. try {
  11. // create a temporary file
  12. System.out.println("dir:" + System.getProperty("user.dir"));
  13. File logFile = new File(System.getProperty("user.dir") + "\\lab3.txt");
  14.  
  15. writer = new BufferedWriter(new FileWriter(logFile));
  16. writer.write(shape3(9) + System.lineSeparator() + shape4(9) + System.lineSeparator() + shape5(9)
  17. + System.lineSeparator() + shape6(9));
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. } finally {
  21. try {
  22. // Close the writer regardless of what happens...
  23. writer.close();
  24. } catch (Exception e) {
  25. }
  26. }
  27. // append following patterns in a file e.g. "c:\\lab3.txt" one by one
  28.  
  29. // read the pattern File e.g. "c://lab3.txt" and display to the screen
  30.  
  31. }
  32.  
  33. // sample pattern printing to the screen
  34. public static void shape1(int j) {
  35. for (int i = 1; i <= j; i++) {
  36. for (int k = 1; k <= i; k++) {
  37. System.out.print(i);
  38. }
  39. System.out.println();
  40. }
  41.  
  42. }
  43.  
  44. // sample pattern printing to the screen
  45. public static void shape2(int j) {
  46. for (int i = 1; i <= j; i++) {
  47. for (int k = 1; k <= (j * 2 + 1); k++) {
  48. System.out.print(i);
  49. }
  50. System.out.println();
  51. }
  52.  
  53. }
  54.  
  55. // save the pattern in a file
  56. // need to handle the exceptions
  57. public static String shape3(int j) {
  58. int width = j * 2;
  59. int height = j;
  60. String stringToPrint = "";
  61. for (int row = 0; row < height; row++) {
  62. for (int col = 0; col < width; col++) {
  63. if (row == 0) {
  64. stringToPrint += "" + (row + 1);
  65. } else {
  66. if (row == (height - 1)) {
  67. stringToPrint += "" + (row + 1);
  68. } else {
  69. if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
  70. stringToPrint += "" + (row + 1);
  71. } else {
  72. stringToPrint += ".";
  73. }
  74. }
  75. }
  76. }
  77. stringToPrint += System.getProperty("line.separator");
  78. }
  79. return stringToPrint;
  80. }
  81.  
  82. // save the pattern in a file
  83. // need to handle the exceptions
  84. public static String shape4(int j) {
  85. int width = j * 2 + 1;
  86. int height = j;
  87. String stringToPrint = "";
  88. for (int row = 0; row < height; row++) {
  89. for (int col = 0; col < width; col++) {
  90. if (row == 0) {
  91. stringToPrint += "" + (row + 1);
  92. } else {
  93. if (row == (height - 1)) {
  94. stringToPrint += "" + (row + 1);
  95. } else {
  96. if (row == (height / 2)) {
  97. stringToPrint += "" + (row + 1);
  98. } else {
  99. if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
  100. stringToPrint += "" + (row + 1);
  101. } else {
  102. stringToPrint += ".";
  103. }
  104. }
  105. }
  106. }
  107. }
  108. stringToPrint += System.getProperty("line.separator");
  109. }
  110. return stringToPrint;
  111. }
  112.  
  113. // save the pattern in a file
  114. // need to handle the exceptions
  115. public static String shape5(int j) {
  116. int width = j * 2 + 1;
  117. int height = j;
  118. String stringToPrint = "";
  119. for (int row = 0; row < height; row++) {
  120. for (int col = 0; col < width; col++) {
  121. if (row == 0) {
  122. stringToPrint += "" + (row + 1);
  123. } else {
  124. if (row == (height - 1)) {
  125. stringToPrint += "" + (row + 1);
  126. } else {
  127. if (row == (height / 2)) {
  128. stringToPrint += "" + (row + 1);
  129. } else {
  130. if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
  131. stringToPrint += "" + (1);
  132. } else {
  133. if ((col == (width / 6)) || (col == (width / 3)) || (col == (width * 2 / 3))
  134. || (col == (width * 5 / 6))) {
  135. stringToPrint += "x";
  136. } else {
  137. stringToPrint += ".";
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. stringToPrint += System.getProperty("line.separator");
  145. }
  146. return stringToPrint;
  147. }
  148.  
  149. // save the pattern in a file
  150. // need to handle the exceptions
  151. public static String shape6(int j) {
  152. int width = j * 2 + 1;
  153. int height = j;
  154. int middle = width / 2;
  155. String stringToPrint = "";
  156. for (int row = 0; row < height; row++) {
  157. for (int col = 0; col < width; col++) {
  158. if ((col < (middle - row)) || (col > (middle + row))) {
  159. stringToPrint += ".";
  160. } else {
  161. stringToPrint += "" + (row + 1);
  162. }
  163. }
  164. stringToPrint += System.getProperty("line.separator");
  165. }
  166. return stringToPrint;
  167. }
  168.  
  169. }
Add Comment
Please, Sign In to add comment