Advertisement
Omar_Natour

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

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