-
-
Save bytecodeman/c6607b7f553f32a2913b164564876fda to your computer and use it in GitHub Desktop.
CSC-220 HW3 Two Dimensional Array Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Prof. A.C. Silvestri | |
* Date: 9/29/18 | |
* Course Number: CSC-220 | |
* Course Name: Data Structures and Algorithms | |
* Problem Number: HW3 | |
* Email: silvestri@stcc.edu | |
* Description: Two Dimensional Array Processing specified in this assignment: | |
* http://cs.stcc.edu/csc-220-hw3-two-dimensional-array-problem-stcccsc220/ | |
*/ | |
import java.io.File; | |
import java.util.Scanner; | |
public class StarDataProcessor { | |
@SuppressWarnings("unused") | |
private static void print(int x[][], int columnsize, int numPerLine) { | |
System.out.printf("%" + columnsize + "d", x.length); | |
System.out.printf("%" + columnsize + "d\n", x[0].length); | |
for (int i = 0; i < x.length; i++) { | |
for (int j = 0; j < x[i].length; j++) { | |
System.out.printf("%" + columnsize + "d", x[i][j]); | |
} | |
System.out.println(); | |
} | |
} | |
private static void print(char[][] stars) { | |
for (int i = 0; i < stars.length; i++) { | |
for (int j = 0; j < stars[0].length; j++) | |
System.out.print("+---"); | |
System.out.println("+"); | |
for (int j = 0; j < stars[0].length; j++) | |
System.out.print("| " + stars[i][j] + " "); | |
System.out.println("|"); | |
} | |
for (int j = 0; j < stars[0].length; j++) | |
System.out.print("+---"); | |
System.out.println("+"); | |
} | |
private static int sumOfNeighbors(int[][] starData, int row, int col) { | |
int sum = 0; | |
for (int i = -1; i <= 1; i++) | |
for (int j = -1; j <= 1; j++) | |
sum += starData[row + i][col + j]; | |
return sum; | |
} | |
private static char[][] analyzeStarData(int[][] starData) { | |
char stars[][] = new char[starData.length][starData[0].length]; | |
for (int i = 0; i < stars.length; i++) | |
for (int j = 0; j < stars[0].length; j++) | |
stars[i][j] = ' '; | |
for (int i = 1; i < stars.length - 1; i++) | |
for (int j = 1; j < stars[0].length - 1; j++) | |
if (sumOfNeighbors(starData, i, j) / 5 > 6.0) | |
stars[i][j] = '*'; | |
return stars; | |
} | |
private static int[][] readStarData(String filename) { | |
try { | |
Scanner sc = new Scanner(new File(filename)); | |
int row = sc.nextInt(); | |
int col = sc.nextInt(); | |
int data[][] = new int[row][col]; | |
for (int i = 0; i < data.length; i++) | |
for (int j = 0; j < data[0].length; j++) | |
data[i][j] = sc.nextInt(); | |
return data; | |
} | |
catch (Exception ex) { | |
return null; | |
} | |
} | |
// ********************************************** | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter StarData filename: "); | |
String filename = sc.nextLine(); | |
int starData[][] = readStarData(filename); | |
char stars[][] = analyzeStarData(starData); | |
print(stars); | |
System.out.println(); | |
} | |
// ********************************************** | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
public static void main(String args[]) { | |
final String TITLE = "Star Data Processor V1.0"; | |
final String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment