Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. /*
  2. * Name: Sam Conroy
  3. * Date: 9/20/2019
  4. * Course Number: CSC - 220
  5. * Course Name: Data Structures
  6. * Problem Number: Homework 3
  7. * Email: srconroy0001@student.stcc.edu
  8. * Short Description of the Problem Star Data problem.
  9. */
  10.  
  11. import java.io.File;
  12. import java.util.Arrays;
  13. import java.util.Scanner;
  14.  
  15. public class starData {
  16. final static String TITLE = "Star Data";
  17. final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
  18. private static int [][] arr;
  19. //**********************************************
  20. private static void readStarData(Scanner sc) {
  21. //This method simply reads in the data from a file specified by the user
  22. try {
  23. System.out.print("Enter Filename: ");
  24. String filename = sc.nextLine();
  25. Scanner fileInput = new Scanner(new File(filename));
  26.  
  27. int i = fileInput.nextInt();
  28. int j = fileInput.nextInt();
  29.  
  30. arr = new int[i][j];
  31.  
  32. for (int x = 0; x < i; x++) {
  33. for (int y = 0; y < j; y++)
  34. arr[x][y] = fileInput.nextInt();
  35. }
  36.  
  37. fileInput.close();
  38. }
  39.  
  40. catch (Exception ex) {
  41. System.out.println("Something bad happened!!!!!!!!!!!!");
  42. }
  43.  
  44. }
  45.  
  46. //**********************************************
  47. private static int getSurroundingSum(int i, int j) {
  48. // this method gets the sum of the surrounding numbers
  49. // it then divides the sum by 5 and returns a boolean indicating whether or not it is a star
  50. int isStar = 0;
  51.  
  52. for (int x = i - 1; x < i + 2; x++)
  53. for (int y = j - 1; y < j + 2; y++)
  54. isStar += arr[x][y];
  55.  
  56. return isStar;
  57. }
  58.  
  59. //**********************************************
  60. public static char[][] analyzeStarData(){
  61. //this method inevitably determines where there is a star and where there is not
  62. //then returns a character array contain this information and ready to be printed out
  63. char [][] stardata = new char [arr.length][arr[0].length];
  64.  
  65. for(int i = 0; i < arr.length; i++ )
  66. for(int j = 0; j < arr[0].length; j++) {
  67. if(arr[i][j] > 0) {
  68. double star = getSurroundingSum( i, j);
  69. if (star / 5 > 6.0)
  70. stardata[i][j] = '*';
  71.  
  72. else
  73. stardata[i][j] = ' ';
  74. }
  75. else
  76. stardata[i][j] = ' ';
  77. }
  78. return stardata;
  79.  
  80. }
  81.  
  82. //**********************************************
  83. private static void printStarData(char[][]starData) {
  84. //this method will print the character array that was formed in previous methods
  85.  
  86. for(int i = 0; i < starData.length; i++) {
  87. System.out.println();
  88. for(int a = 0; a < starData[0].length; a++) {
  89. System.out.print("+---");
  90. }
  91.  
  92. System.out.print("+");
  93. System.out.print("\n| ");
  94. for(int j = 0;j < starData[0].length; j++) {
  95. System.out.print(starData[i][j] + " | ");
  96.  
  97. }
  98. }
  99. }
  100. //**********************************************
  101.  
  102. private static void process(Scanner sc, String args[]) {
  103.  
  104. readStarData(sc);
  105. char[][] starData = new char [arr[0].length][arr.length];
  106. System.out.println("Analyzing star data ...");
  107. starData = analyzeStarData();
  108. printStarData(starData);
  109.  
  110. }
  111.  
  112. //**********************************************
  113.  
  114. private static boolean doThisAgain(Scanner sc, String prompt) {
  115. System.out.print("\n" + prompt);
  116. String doOver = sc.nextLine();
  117. return doOver.trim().equalsIgnoreCase("Y");
  118. }
  119.  
  120. //**********************************************
  121.  
  122. public static void main(String args[]) {
  123. System.out.println("Welcome to " + TITLE);
  124. Scanner sc = new Scanner(System.in);
  125. do {
  126. process(sc, args);
  127. } while (doThisAgain(sc, CONTINUE_PROMPT));
  128. sc.close();
  129. System.out.println("Thank you for using " + TITLE);
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement