Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 10/3/2016
- * Csc-220 Data Structures
- * Hw3 Two Dimensional Arrays
- * Check values of surround numbers of a particular number within a 2d array.
- * Ojnatour0001@student.stcc.edu
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class StarAnalysis {
- static int rows = 0;
- static int columns = 0;
- public static void main(String[] args) {
- Scanner sc = null;
- try {
- sc = new Scanner(new File("starData.txt"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- do {
- outputStarData(analyzeStarData(readStarsData(sc)));
- } while (doThisAgain(sc));
- sc.close();
- }
- private static boolean doThisAgain(Scanner sc) {
- String doOver = sc.next();
- if (doOver.equalsIgnoreCase("Y")) {
- return true;
- } else {
- return false;
- }
- }
- public static int[][] readStarsData(Scanner s) {
- if (s.hasNextInt())
- rows = s.nextInt();
- else
- System.err.print("Bad Row length input");
- if (s.hasNextInt())
- columns = s.nextInt();
- else
- System.err.print("Bad Column Length input.");
- int[][] starData = new int[rows][columns];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- starData[i][j] = s.nextInt();
- }
- }
- return starData;
- }
- public static char[][] analyzeStarData(int starData[][]) {
- int current;
- char[][] stars = new char[rows][columns];
- for (int i = 1; i < rows - 1; i++) {
- for (int j = 1; j < columns - 1; j++) {
- current = 0;
- for (int k = i - 1; k <= i + 1; k++) {
- for (int m = j - 1; m <= j + 1; m++) {
- current += starData[k][m];
- if (current / 5.0 > 6.0) {
- stars[i][j] = '*';
- } else
- stars[i][j] = ' ';
- }
- }
- }
- }
- return stars;
- }
- public static void outputStarData(char pattern[][]) {
- String output = "";
- for (int i = 1; i < columns - 1; i++) {
- output += ("+---");
- }
- output += "+";
- output += ('\n');
- for (int i = 1; i < rows - 1; i++) {
- for (int j = 1; j < columns - 1; j++) {
- output += ("| " + pattern[i][j] + " ");
- }
- output += "|";
- output += ("\n");
- for (int k = 1; k < columns - 1; k++) {
- output += ("+---");
- }
- output += "+";
- output += ("\n");
- }
- System.out.println(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement