Omar_Natour

Natour, O. 10/3/16 Csc-220 starData

Oct 4th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 10/3/2016
  4.  * Csc-220 Data Structures
  5.  * Hw3 Two Dimensional Arrays
  6.  * Check values of surround numbers of a particular number within a 2d array.
  7.  */
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.util.Scanner;
  12.  
  13. public class StarAnalysis {
  14.  
  15.     static int rows = 0;
  16.     static int columns = 0;
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         Scanner sc = null;
  21.         try {
  22.             sc = new Scanner(new File("starData.txt"));
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         }
  26.  
  27.         do {
  28.             outputStarData(analyzeStarData(readStarsData(sc)));
  29.         } while (doThisAgain(sc));
  30.  
  31.         sc.close();
  32.     }
  33.  
  34.     private static boolean doThisAgain(Scanner sc) {
  35.         String doOver = sc.next();
  36.         if (doOver.equalsIgnoreCase("Y")) {
  37.             return true;
  38.         } else {
  39.             return false;
  40.         }
  41.     }
  42.  
  43.     public static int[][] readStarsData(Scanner s) {
  44.  
  45.         if (s.hasNextInt())
  46.             rows = s.nextInt();
  47.         else
  48.             System.err.print("Bad Row length input");
  49.         if (s.hasNextInt())
  50.             columns = s.nextInt();
  51.         else
  52.             System.err.print("Bad Column Length input.");
  53.  
  54.         int[][] starData = new int[rows][columns];
  55.  
  56.         for (int i = 0; i < rows; i++) {
  57.             for (int j = 0; j < columns; j++) {
  58.                 starData[i][j] = s.nextInt();
  59.             }
  60.         }
  61.         return starData;
  62.     }
  63.  
  64.     public static char[][] analyzeStarData(int starData[][]) {
  65.  
  66.         int current;
  67.         char[][] stars = new char[rows][columns];
  68.  
  69.         for (int i = 1; i < rows - 1; i++) {
  70.             for (int j = 1; j < columns - 1; j++) {
  71.                 current = 0;
  72.                 for (int k = i - 1; k <= i + 1; k++) {
  73.                     for (int m = j - 1; m <= j + 1; m++) {
  74.                         current += starData[k][m];
  75.  
  76.                         if (current / 5.0 > 6.0) {
  77.                             stars[i][j] = '*';
  78.                         } else
  79.                             stars[i][j] = ' ';
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         return stars;
  85.     }
  86.  
  87.     public static void outputStarData(char pattern[][]) {
  88.         String output = "";
  89.  
  90.         for (int i = 1; i < columns - 1; i++) {
  91.             output += ("+---");
  92.         }
  93.         output += "+";
  94.         output += ('\n');
  95.  
  96.         for (int i = 1; i < rows - 1; i++) {
  97.             for (int j = 1; j < columns - 1; j++) {
  98.                 output += ("| " + pattern[i][j] + " ");
  99.             }
  100.  
  101.             output += "|";
  102.             output += ("\n");
  103.  
  104.             for (int k = 1; k < columns - 1; k++) {
  105.                 output += ("+---");
  106.             }
  107.  
  108.             output += "+";
  109.             output += ("\n");
  110.         }
  111.         System.out.println(output);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment