nate23nate23

hw3 stillwrong

Oct 3rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1.  
  2. /*
  3.  * Name: Nate Wheeler
  4.  * Date: sept 30,2016
  5.  * Course Number: csc220
  6.  * Course Name: data structures
  7.  * Problem Number: hw 3
  8.  * Short Description of the Problem:
  9.  * You are asked to write a JAVA program that
  10.  * takes an array containing the digitized
  11.  * picture of the night sky and locate the star(s) in it.
  12.  */
  13.  
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.util.Scanner;
  17.  
  18. public class StarGazer {
  19.  
  20.     private static void process(Scanner sc, String args[]) {
  21.         int starData[][] = getStarData(sc);
  22.         print(starData);
  23.         System.out.println();
  24.         //print(analyzeStarData2(starData));
  25.         outputStarData(analyzeStarData(starData));
  26.         System.out.println();
  27.         sc.nextLine(); // IMPORTANT!! Reset Scanner
  28.         // System.out.println("Processing " + + " ...");
  29.     }
  30.  
  31.     // **********************************************
  32.  
  33.     private static void print(int[][] starData) {
  34.         // TODO Auto-generated method stub
  35.         // String border ="--+";
  36.         // for(int b=0;b<starData.length;b++)
  37.         // border=border+border;
  38.         for (int i = 0; i < starData.length; i++) {
  39.             // System.out.printf("+%-5s\n",border);
  40.             for (int j = 0; j < starData[0].length; j++) {
  41.                 System.out.printf("%5d", starData[i][j]);
  42.             }
  43.             System.out.println();
  44.         }
  45.  
  46.     }
  47.  
  48.     private static void outputStarData(char starMap[][]) {
  49.         String border = "+-----";
  50.         String add = "+-----";
  51.         int count = 0;
  52.         while (count < starMap[0].length - 1) {
  53.             border += add;
  54.             count++;
  55.         }
  56.         for (int i = 0; i < starMap.length; i++) {
  57.             // System.out.printf("%6s+%6s\n", Library.repeatString("-", 6),
  58.             // Library.repeatString("-", 6));
  59.             System.out.println(border + "+");
  60.             // if(i == (starMap[0].length - 1))
  61.             for (int j = 0; j < starMap[0].length; j++) {
  62.                 System.out.printf("|%5c", starMap[i][j]);
  63.                 if (j == (starMap[i].length - 1))
  64.                     System.out.println("|");
  65.             }
  66.         }
  67.         System.out.println(border + "+");
  68.  
  69.     }
  70.     public static int[][] falseArray(int[][] src) {
  71.         //this method is to help identify array parameters without affecting the calculations
  72.         int[][] fake = new int[src.length][src[0].length];
  73.         for (int i = 0; i < src.length; i++) {
  74.             System.arraycopy(src[i], 0, fake[i], 0, src[i].length);
  75.         }
  76.         return fake;
  77.     }
  78. /*
  79.     private static int[][] analyzeStarData2(int starData[][]) {
  80.         // create char array
  81.         int[][] starData2=falseArray(starData);
  82.         char[][] starMap = new char[starData.length][starData[0].length];
  83.         double count = 0;
  84.         // calculate the intensities
  85.  
  86.         for (int i = 0; i < starMap.length; i++) {
  87.             for (int j = 0; j < starMap[0].length; j++) {
  88.                 // make borders 0
  89.                 starData2[0][j] =-1;
  90.                 starData2[starData2.length - 1][j] = -1;
  91.                 starData2[i][0] = -1;
  92.                 starData2[i][starData2[0].length - 1] = -1;
  93.                 // identify the adjacent numbers
  94.                 if (starData2[i][j]!=-1) {
  95.                     // calculating
  96.                     // Formatter:on
  97.                     count = starData[i - 1][j - 1] + starData[i - 1][j] + starData[i - 1][j + 1]
  98.                             + starData[i][j - 1]+ starData[i][j] + starData[i][j + 1]
  99.                             + starData[i + 1][j - 1] + starData[i + 1][j]+ starData[i + 1][j + 1];
  100.                     // Formatter:off
  101.                     count = count / 15;
  102.                     // mark when intensity passes base
  103.                     if (count > 6.0) {
  104.                         starMap[i][j] = '*';
  105.                         starData[i][j] = 23;
  106.                     }
  107.                     count = 0;
  108.                 }
  109.                 if (starMap[i][j] != '*')
  110.                     starMap[i][j] = ' ';
  111.             }
  112.         }
  113.  
  114.         return starData;
  115.     }
  116.     */
  117.     private static char[][] analyzeStarData(int starData[][]) {
  118.         // create char array
  119.         int[][] starData2=falseArray(starData);
  120.         char[][] starMap = new char[starData.length][starData[0].length];
  121.         double count = 0;
  122.         // calculate the intensities
  123.  
  124.         for (int i = 0; i < starMap.length; i++) {
  125.             for (int j = 0; j < starMap[0].length; j++) {
  126.                 // make borders 0
  127.                 starData2[0][j] =-1;
  128.                 starData2[starData2.length - 1][j] = -1;
  129.                 starData2[i][0] = -1;
  130.                 starData2[i][starData2[0].length - 1] = -1;
  131.                 // identify the adjacent numbers
  132.                 if (starData2[i][j]!=-1) {
  133.                     // calculating
  134.                     // Formatter:on
  135.                     count = starData[i - 1][j - 1] + starData[i - 1][j] + starData[i - 1][j + 1]
  136.                             + starData[i][j - 1]+ starData[i][j] + starData[i][j + 1]
  137.                             + starData[i + 1][j - 1] + starData[i + 1][j]+ starData[i + 1][j + 1];
  138.                     // Formatter:off
  139.                     count = count / 5;
  140.                     // mark when intensity passes base
  141.                     if (count > 6.0) {
  142.                         starMap[i][j] = '*';
  143.                         //starData[i][j] = 23;
  144.                     }
  145.                     count = 0;
  146.                 }
  147.                 if (starMap[i][j] != '*')
  148.                     starMap[i][j] = ' ';
  149.             }
  150.         }
  151.         return starMap;
  152.     }
  153.     private static int[][] getStarData(Scanner sc) {
  154.         // TODO Auto-generated method stub
  155.         int rows = sc.nextInt();
  156.         int cols = sc.nextInt();
  157.         int data[][] = new int[rows][cols];
  158.         for (int i = 0; i < rows; i++) {
  159.             for (int j = 0; j < cols; j++) {
  160.                 data[i][j] = sc.nextInt();
  161.             }
  162.         }
  163.         return data;
  164.     }
  165.  
  166.     private static boolean doThisAgain(Scanner sc, String prompt) {
  167.         System.out.print(prompt);
  168.         System.out.println();
  169.         String doOver = sc.nextLine();
  170.         return doOver.equalsIgnoreCase("Y");
  171.     }
  172.  
  173.     // **********************************************
  174.  
  175.     public static void main(String args[]) throws FileNotFoundException {
  176.         final String TITLE = "CSC220 Project Template";
  177.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  178.  
  179.         Scanner sc = new Scanner(new File("StarData.txt"));
  180.  
  181.         // System.out.println("Welcome to " + TITLE);
  182.         do {
  183.             process(sc, args);
  184.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  185.  
  186.         sc.close();
  187.         System.out.println("Thank you for using " + TITLE);
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment