Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Sam Conroy
- * Date: 9/20/2019
- * Course Number: CSC - 220
- * Course Name: Data Structures
- * Problem Number: Homework 3
- * Email: srconroy0001@student.stcc.edu
- * Short Description of the Problem Star Data problem.
- */
- import java.io.File;
- import java.util.Arrays;
- import java.util.Scanner;
- public class starData {
- final static String TITLE = "Star Data";
- final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
- private static int [][] arr;
- //**********************************************
- private static void readStarData(Scanner sc) {
- //This method simply reads in the data from a file specified by the user
- try {
- System.out.print("Enter Filename: ");
- String filename = sc.nextLine();
- Scanner fileInput = new Scanner(new File(filename));
- int i = fileInput.nextInt();
- int j = fileInput.nextInt();
- arr = new int[i][j];
- for (int x = 0; x < i; x++) {
- for (int y = 0; y < j; y++)
- arr[x][y] = fileInput.nextInt();
- }
- fileInput.close();
- }
- catch (Exception ex) {
- System.out.println("Something bad happened!!!!!!!!!!!!");
- }
- }
- //**********************************************
- private static int getSurroundingSum(int i, int j) {
- // this method gets the sum of the surrounding numbers
- // it then divides the sum by 5 and returns a boolean indicating whether or not it is a star
- int isStar = 0;
- for (int x = i - 1; x < i + 2; x++)
- for (int y = j - 1; y < j + 2; y++)
- isStar += arr[x][y];
- return isStar;
- }
- //**********************************************
- public static char[][] analyzeStarData(){
- //this method inevitably determines where there is a star and where there is not
- //then returns a character array contain this information and ready to be printed out
- char [][] stardata = new char [arr.length][arr[0].length];
- for(int i = 0; i < arr.length; i++ )
- for(int j = 0; j < arr[0].length; j++) {
- if(arr[i][j] > 0) {
- double star = getSurroundingSum( i, j);
- if (star / 5 > 6.0)
- stardata[i][j] = '*';
- else
- stardata[i][j] = ' ';
- }
- else
- stardata[i][j] = ' ';
- }
- return stardata;
- }
- //**********************************************
- private static void printStarData(char[][]starData) {
- //this method will print the character array that was formed in previous methods
- for(int i = 0; i < starData.length; i++) {
- System.out.println();
- for(int a = 0; a < starData[0].length; a++) {
- System.out.print("+---");
- }
- System.out.print("+");
- System.out.print("\n| ");
- for(int j = 0;j < starData[0].length; j++) {
- System.out.print(starData[i][j] + " | ");
- }
- }
- }
- //**********************************************
- private static void process(Scanner sc, String args[]) {
- readStarData(sc);
- char[][] starData = new char [arr[0].length][arr.length];
- System.out.println("Analyzing star data ...");
- starData = analyzeStarData();
- printStarData(starData);
- }
- //**********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print("\n" + prompt);
- String doOver = sc.nextLine();
- return doOver.trim().equalsIgnoreCase("Y");
- }
- //**********************************************
- public static void main(String args[]) {
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- 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