Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Nate Wheeler
- * Date: sept 30,2016
- * Course Number: csc220
- * Course Name: data structures
- * Problem Number: hw 3
- * Email: nate23nate23@gmail.com
- * Short Description of the Problem:
- * You are asked to write a JAVA program that
- * takes an array containing the digitized
- * picture of the night sky and locate the star(s) in it.
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class StarGazer {
- private static void process(Scanner sc, String args[]) {
- int starData[][] = getStarData(sc);
- print(starData);
- System.out.println();
- //print(analyzeStarData2(starData));
- outputStarData(analyzeStarData(starData));
- System.out.println();
- sc.nextLine(); // IMPORTANT!! Reset Scanner
- // System.out.println("Processing " + + " ...");
- }
- // **********************************************
- private static void print(int[][] starData) {
- // TODO Auto-generated method stub
- // String border ="--+";
- // for(int b=0;b<starData.length;b++)
- // border=border+border;
- for (int i = 0; i < starData.length; i++) {
- // System.out.printf("+%-5s\n",border);
- for (int j = 0; j < starData[0].length; j++) {
- System.out.printf("%5d", starData[i][j]);
- }
- System.out.println();
- }
- }
- private static void outputStarData(char starMap[][]) {
- String border = "+-----";
- String add = "+-----";
- int count = 0;
- while (count < starMap[0].length - 1) {
- border += add;
- count++;
- }
- for (int i = 0; i < starMap.length; i++) {
- // System.out.printf("%6s+%6s\n", Library.repeatString("-", 6),
- // Library.repeatString("-", 6));
- System.out.println(border + "+");
- // if(i == (starMap[0].length - 1))
- for (int j = 0; j < starMap[0].length; j++) {
- System.out.printf("|%5c", starMap[i][j]);
- if (j == (starMap[i].length - 1))
- System.out.println("|");
- }
- }
- System.out.println(border + "+");
- }
- public static int[][] falseArray(int[][] src) {
- //this method is to help identify array parameters without affecting the calculations
- int[][] fake = new int[src.length][src[0].length];
- for (int i = 0; i < src.length; i++) {
- System.arraycopy(src[i], 0, fake[i], 0, src[i].length);
- }
- return fake;
- }
- /*
- private static int[][] analyzeStarData2(int starData[][]) {
- // create char array
- int[][] starData2=falseArray(starData);
- char[][] starMap = new char[starData.length][starData[0].length];
- double count = 0;
- // calculate the intensities
- for (int i = 0; i < starMap.length; i++) {
- for (int j = 0; j < starMap[0].length; j++) {
- // make borders 0
- starData2[0][j] =-1;
- starData2[starData2.length - 1][j] = -1;
- starData2[i][0] = -1;
- starData2[i][starData2[0].length - 1] = -1;
- // identify the adjacent numbers
- if (starData2[i][j]!=-1) {
- // calculating
- // Formatter:on
- count = starData[i - 1][j - 1] + starData[i - 1][j] + starData[i - 1][j + 1]
- + starData[i][j - 1]+ starData[i][j] + starData[i][j + 1]
- + starData[i + 1][j - 1] + starData[i + 1][j]+ starData[i + 1][j + 1];
- // Formatter:off
- count = count / 15;
- // mark when intensity passes base
- if (count > 6.0) {
- starMap[i][j] = '*';
- starData[i][j] = 23;
- }
- count = 0;
- }
- if (starMap[i][j] != '*')
- starMap[i][j] = ' ';
- }
- }
- return starData;
- }
- */
- private static char[][] analyzeStarData(int starData[][]) {
- // create char array
- int[][] starData2=falseArray(starData);
- char[][] starMap = new char[starData.length][starData[0].length];
- double count = 0;
- // calculate the intensities
- for (int i = 0; i < starMap.length; i++) {
- for (int j = 0; j < starMap[0].length; j++) {
- // make borders 0
- starData2[0][j] =-1;
- starData2[starData2.length - 1][j] = -1;
- starData2[i][0] = -1;
- starData2[i][starData2[0].length - 1] = -1;
- // identify the adjacent numbers
- if (starData2[i][j]!=-1) {
- // calculating
- // Formatter:on
- count = starData[i - 1][j - 1] + starData[i - 1][j] + starData[i - 1][j + 1]
- + starData[i][j - 1]+ starData[i][j] + starData[i][j + 1]
- + starData[i + 1][j - 1] + starData[i + 1][j]+ starData[i + 1][j + 1];
- // Formatter:off
- count = count / 5;
- // mark when intensity passes base
- if (count > 6.0) {
- starMap[i][j] = '*';
- //starData[i][j] = 23;
- }
- count = 0;
- }
- if (starMap[i][j] != '*')
- starMap[i][j] = ' ';
- }
- }
- return starMap;
- }
- private static int[][] getStarData(Scanner sc) {
- // TODO Auto-generated method stub
- int rows = sc.nextInt();
- int cols = sc.nextInt();
- int data[][] = new int[rows][cols];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- data[i][j] = sc.nextInt();
- }
- }
- return data;
- }
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- System.out.println();
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // **********************************************
- public static void main(String args[]) throws FileNotFoundException {
- final String TITLE = "CSC220 Project Template";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- Scanner sc = new Scanner(new File("StarData.txt"));
- // System.out.println("Welcome to " + TITLE);
- do {
- process(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement