Advertisement
nate23nate23

hw3 stillwrong

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