You work for NASA in their Space Center Laboratory. You are asked to write a JAV
ID: 3756226 • Letter: Y
Question
You work for NASA in their Space Center Laboratory. You are asked to write a JAVAFX program that takes an array containing the digitized picture of the night sky and locate the star(s) in it. Each element of the array represents the amount of light hitting that portion of the image when the picture was taken. Intensities range from 0 to 20.
Here is a sample text file of this data:
A star is most likely located in the area covered by the array element [i][j] if: (array[i][j] + sum of the 8 surrounding intensities)/5 > 6.0
Ignore possible stars along the edges of the array.
Output should be a character array with asterisks printed at the coordinates of where a star might possibly be. ie:
(Don’t read into the star pattern too much. I ball parked where the stars would be based on the data given above.)
Your program should have a method that reads in the star data and returns a 2D array of ints. i.e.:
You should have a method that analyzes this data to produce a 2D array of characters. This array contains a space or an asterisk depending on the calculation made for row I, column j. ie:
Output a nice character grid similar to what was shown above to display the star pattern. Please include the character borders. ie:
The JavaFx Gui should be able to import and run a scanner on text files with data sets like the above example. Required elements are an "import button", a "scan button", as well as a text field to show the character array of stars and spaces.
Explanation / Answer
/ package whatever; // don't place package name! /
import java.util.*;
import java.lang.*;
import java.io.*;
/ Name of the class has to be "Main" only if the class is public. /
public class NASA {
static int[][] readStarData(Scanner sc) {
int n,m;
n=sc.nextInt();
m=sc.nextInt();
int arr[][] = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++)
arr[i][j] = sc.nextInt();
}
}
static char[][] analyzeStarData(int stardata[][]) {
int n=stardata.length,m=stardata[0].length;
char res[][] = new char[n][m];
double sum;
for(int i=1;i<n-1;i++) {
for(int j=1;j<m-1;j++) {
sum=stardata[i][j];
sum+=stardata[i-1][j-1]+stardata[i-1][j]+stardata[i-1][j+1];
sum+=stardata[i][j-1]+stardata[i][j+1];
sum+=stardata[i+1][j-1]+stardata[i+1][j]+stardata[i+1][j+1];
if(sum/5.0 > 6.0)
res[i][j]='*';
}
}
return res;
}
static void outputStarData(char pattern[][]) {
int n=pattern.length,m=pattern[0].length;
printUpperBorder(m);
for(int i=0;i<n;i++) {
System.out.print("|");
for(int j=0;j<m;j++) {
System.out.println(" "+pattern[i][j]+" |");
}
System.out.println();
printUpperBorder(m);
}
}
static void printUpperBorder(int m) {
for(int i=0;i<m;i++) {
System.out.print("+---");
}
System.out.print("+");
System.out.println();
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int[][] stardata = readStarData(sc);
char[][] pattern = analyzeStarData(stardata);
outputStarData(pattern);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.