Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. //===============================================================================================================================================================================
  2. //Assignment 1
  3. //Nathan Leung
  4. //March 18, 2019
  5. //Java, jre1.8.0_152
  6. //===============================================================================================================================================================================
  7. //The code reads blobs
  8. //I - The x and y coordinate of the location of the asterisk
  9. //O - The original grid with the blobs and the grid after the blob is earased
  10. //===============================================================================================================================================================================
  11. /*List of variables
  12. * LENGTH - a class variable for the constant value of the number of rows in the 2D array (byte)
  13. * WIDTH - a class variable for the constant value of the the number of columns in the 2D array (byte)
  14. //===============================================================================================================================================================================
  15. */
  16. import java.io.*;
  17. public class Assignment1 {
  18. public static final byte LENGTH = 20;
  19. public static final byte WIDTH = 25;
  20. /**
  21. * main method- Procedural method that calls all the static methods and determines whether the user want to enter the program
  22. * 1. Call method commandCenter and to define anyString to see if they want to enter the program and provide a description of the program
  23. * 2. Call method blobs to read the file and store into the 2D array
  24. * 3. Call method column to read the column number of the asterisk that user chooess
  25. * 4. Call method row
  26. * @param args
  27. * @throws IOException
  28. * @throws InterruptedException
  29. */
  30.  
  31. public static void main(String args[]) throws IOException, InterruptedException{
  32. char grid[][]= new char[LENGTH][WIDTH];
  33. byte pointX, pointY;
  34. boolean flag;
  35. String anyString;
  36. anyString= commandCenter();
  37.  
  38. while(!anyString.equals("stop")) {
  39. grid = blobs();
  40. pointX=column();
  41. pointY=row();
  42. flag = eraseBlob(grid, pointY-1,pointX-1);
  43. anyString = checkFlag(flag,grid);
  44.  
  45. }
  46. System.out.println("Thanks for using Nathan's Program");
  47. }
  48.  
  49. public static String input() throws IOException{
  50. BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
  51. String input;
  52. input=br.readLine();
  53. return input;
  54. }
  55.  
  56. public static String commandCenter() throws InterruptedException, IOException {
  57. String word;
  58. System.out.println("============================================================================================");
  59. System.out.println("This program will first print out 3 blobs in a 20x25 rectangle.");
  60. System.out.println("Then the user will be able to input a coordinate on the rectangle to delete a blob");
  61. System.out.println("If you wish to proceed type [yes]");
  62. System.out.println("If you do not wish to run the program type [stop]");
  63. System.out.println("============================================================================================");
  64. while (true) {
  65. word=input();
  66. if (word.equalsIgnoreCase("stop")) {
  67. return word;
  68. }
  69. if (word.equalsIgnoreCase("yes")) {
  70. System.out.println("Lets begin:");
  71. System.out.println("Loadng your blob...");
  72. Thread.sleep(1000);
  73. return word;
  74. }
  75. else {
  76. System.out.println("Invalid, please try again");
  77. }
  78. }
  79. }
  80.  
  81. public static char[][] blobs() throws IOException {
  82. BufferedReader input=new BufferedReader (new FileReader("D:\\Java\\Blob.txt"));
  83. String line;
  84.  
  85. char grid[][]=new char[LENGTH][WIDTH];
  86. for(byte row=0;row<LENGTH;row++){
  87. line=input.readLine();
  88. for(byte col=0;col<WIDTH;col++){
  89. grid[row][col]=line.charAt(col);
  90. }
  91. }
  92. gridFinal(grid);
  93. return grid;
  94. }
  95.  
  96. public static byte column() throws IOException{
  97. byte x;
  98. System.out.println("Please input the column number of the point you wish to delete");
  99. while (true) {
  100. x = selectPoint();
  101. if (x<=WIDTH)
  102. return x;
  103. else
  104. System.out.println("The column number is invalid, please try again");
  105. }
  106. }
  107.  
  108. public static byte row() throws IOException{
  109. byte y;
  110. System.out.println("Please input the row number of the point you wish to delete");
  111. while (true) {
  112. y = selectPoint();
  113. if (y<=LENGTH)
  114. return y;
  115. else
  116. System.out.println("The row number is invalid, please try again");
  117. }
  118. }
  119.  
  120. public static String checkFlag(boolean flag, char[][]grid) throws IOException, InterruptedException{
  121. String input;
  122. if(flag==true) {
  123. System.out.println("One Second, Deleting blob...");
  124. Thread.sleep(1000);
  125. gridFinal(grid);
  126. System.out.println("Your blob has been deleted, enter [yes] to run again, [stop] to end");
  127. input= input();
  128. return input;
  129. }
  130. else {
  131. System.out.println("Your point was not an asterick, enter [yes] to continue, [stop] to end");
  132. input = input();
  133. return input;
  134. }
  135. }
  136. public static boolean eraseBlob(char grid[][], int y, int x) {
  137. boolean flag=false;
  138. if (x>=0&&x<WIDTH&&y>=0&&y<LENGTH) {
  139. if(grid[y][x]== '*'){
  140. flag=true;
  141. grid[y][x]= ' ';
  142. eraseBlob(grid,y,x+1);
  143. eraseBlob(grid,y,x-1);
  144. eraseBlob(grid,y+1,x);
  145. eraseBlob(grid,y-1,x);
  146. }
  147. }
  148. return flag;
  149. }
  150.  
  151. public static byte selectPoint() throws IOException{
  152. String temp;
  153. byte num;
  154. while (true) {
  155. try {
  156. temp = input();
  157. num = Byte.parseByte(temp);
  158. break;
  159. }
  160. catch (Exception e){
  161. System.out.println("Input not accpeted, please try again");
  162. }
  163. }
  164. return num;
  165. }
  166.  
  167. public static void gridFinal(char[][]grid){
  168. System.out.println(" 1234567891111111111222222");
  169. System.out.println(" 0123456789012345");
  170. byte rows=1;
  171.  
  172. for(byte row=0;row<LENGTH;row++){
  173. if(rows<10)
  174. System.out.print(rows+" ");
  175. else
  176. System.out.print(rows);
  177. rows++;
  178. for(byte col=0;col<WIDTH;col++){
  179. System.out.print(grid[row][col]);
  180. }
  181. System.out.println();
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement