-
-
Save bytecodeman/3f04451ed9b66cf3ffd86bb5d7d3de30 to your computer and use it in GitHub Desktop.
CSC-220 HW3 Two Dimensional Array Problem Get Data From Internet
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/ | |
* V2.0 Gets data fron internet | |
*/ | |
import java.net.URL; | |
import java.util.Scanner; | |
public class StarDataProcessor2 { | |
@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) { | |
Scanner sc = null; | |
try { | |
URL file = new URL(filename); | |
sc = new Scanner(file.openStream()); | |
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; | |
} finally { | |
try { | |
sc.close(); | |
} catch (Exception ex) { | |
} | |
} | |
} | |
// ********************************************** | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter URL of StarData: "); | |
String filename = sc.nextLine(); | |
int starData[][] = readStarData(filename); | |
if (starData != null) { | |
char stars[][] = analyzeStarData(starData); | |
print(stars); | |
System.out.println(); | |
} | |
else { | |
System.out.println("Error getting Start Data from Internet."); | |
} | |
} | |
// ********************************************** | |
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 V2.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