Last active
October 6, 2021 01:14
-
-
Save jananpatel2002/8ae5eb7fc8604f4d09e47f2b3c0afe67 to your computer and use it in GitHub Desktop.
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: Janan Patel | |
* Date: 10/5/2021 | |
* Course Number: CSC-220 | |
* Course Name: Data Structures and Algorithms | |
* Problem Number: HW5 | |
* Email: [email protected] | |
* Algorithm Description: | |
* Using information from a website to draw stars on the output | |
* | |
*/ | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Scanner; | |
import java.util.Scanner; | |
public class NasaStarsProject { | |
private final static String TITLE = "The NASA Star Analyzing program"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
static String urlStr; | |
static String symbols = "---"; | |
// ********************************************** | |
// Put as many methods you need here | |
private static int getRow() throws IOException { | |
URL url = new URL(urlStr); | |
Scanner sc = new Scanner(url.openStream()); | |
int row = sc.nextInt(); | |
sc.close(); | |
return row; | |
} | |
private static int getColumn() throws IOException { | |
URL url = new URL(urlStr); | |
Scanner sc = new Scanner(url.openStream()); | |
int temp = sc.nextInt(); | |
int column = sc.nextInt(); | |
sc.close(); | |
return column; | |
} | |
private static int[][] readData(Scanner sc) throws MalformedURLException, IOException { | |
int rows = sc.nextInt(); | |
int columns = sc.nextInt(); | |
int[][] array = new int[rows][columns]; | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < columns; j++) { | |
array[i][j] = sc.nextInt(); | |
} | |
} | |
sc.close(); | |
return array; | |
} | |
static char[][] analyzeStarData(int stardata[][]) throws IOException { | |
// Method is going to be made to ignore the outside values (0's on the outside) | |
// hence why I do (row-2) and (column-2) for the array | |
int row = getRow(); | |
int column = getColumn(); | |
char[][] pattern = new char[row - 2][column - 2]; | |
for (int i = 1; i < row - 1; i++) { | |
for (int j = 1; j < column - 1; j++) { | |
if ((Math.floor(stardata[i][j] + (stardata[i - 1][j] + stardata[i - 1][j + 1] + stardata[i][j + 1] | |
+ stardata[i + 1][j + 1] + stardata[i + 1][j] + stardata[i + 1][j - 1] + stardata[i][j - 1] | |
+ stardata[i - 1][j - 1])) / 5) > 6) { | |
pattern[i - 1][j - 1] = '*'; | |
} else | |
pattern[i - 1][j - 1] = ' '; | |
} | |
} | |
return pattern; | |
} | |
static void outputStarData(char pattern[][]) { | |
// was made on purpose to not read any of the exterior 0's. | |
// therefore the stars might look like they are closer to the edge than they | |
// would be in the data set | |
// the output only outputs the inside square since we were suppose to ignore the | |
// outside 0's. | |
System.out.println("\nHERE IS THE PATTERN\n" | |
+ "-------------------"); | |
int row = pattern.length; | |
int column = pattern[0].length; | |
int i1 = 0; | |
for (int i = 0; i < row; i++) { | |
System.out.println(); | |
for (int j = 0; j < column; j++) { | |
System.out.print("+"); | |
System.out.print(symbols); | |
if (j == column - 1) { | |
System.out.print("+"); | |
} | |
} | |
System.out.println(); | |
for (int j = 0; j < column; j++) { | |
System.out.print("| " + pattern[i1][j] + " "); | |
if (j == column - 1) { | |
System.out.print("|"); | |
} | |
} | |
i1++; | |
} | |
System.out.println(); | |
for (int j = 0; j < column; j++) { | |
System.out.print("+"); | |
System.out.print(symbols); | |
if (j == column - 1) { | |
System.out.print("+"); | |
} | |
} | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
System.out.println("\nHere are a couple links that you can copy and paste:\n" | |
+ "https://cs.stcc.edu/wp-content/uploads/2018/10/StarData1.txt\n" | |
+ "https://cs.stcc.edu/wp-content/uploads/2018/10/StarData2.txt\n" | |
+ "https://cs.stcc.edu/wp-content/uploads/2018/10/StarData3.txt\n"); | |
System.out.println("Please enter your link to the dataset: "); | |
System.out.println("-----------------------------------------"); | |
@SuppressWarnings("resource") | |
Scanner sc1 = new Scanner(System.in); | |
urlStr = sc1.next(); | |
URL url = new URL(urlStr); | |
Scanner sc = new Scanner(url.openStream()); | |
outputStarData(analyzeStarData(readData(sc))); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.println(); | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println(); | |
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