Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You work for the Jet Propulsion Laboratory. They want you to write a program tha

ID: 668733 • Letter: Y

Question

You work for the Jet Propulsion Laboratory. They want you to write a program that will take a two-dimensional array containing the digitized representation of a picture of the night sky and locate the stars on it. The array elements are integers and will be read in from a text file. Each element of the array represents the amount of light hitting that portion of the image when the picture was taken. Intensities can range from 0 (no light) to 20 (maximum light).


A star is probably located in the area covered by the array element i,j if the following is the case:


(Array(i,j) + sum of the 4 surrounding intensities) / 5.0 > 6.0


Ignore possible stars along the edges of the matrix.


The desired output is a star map containing asterisks where you have found a star and blanks elsewhere.   


Note: Output should be sent only to the file “picture.txt”. No output should appear on the screen.

INPUT FILE: (project1.txt): An external text file containing 20 lines of data. Each line contains 20 integers. Therefore, your array(s) should be 20 x 20 in size.


OUTPUT FILE: (picture.txt): A star map (with a border). Print two blanks to indicate "no star" and one blank and an asterisk to indicate the presence of a star.

Explanation / Answer

import java.io.*;
import java.util.*;

class main{
   public static void main(String[] args){
       BufferedReader br = null;
       int[][] intensity = new int[20][20];
       try {
           String line;
           br = new BufferedReader(new FileReader("project1.txt");
           int i = 0;
           // Reading to a file
           while ((line = br.readLine()) != null) {
               String[] st = line.split(" ");
               for (int j = 0; j < st.length; j++)
                   intensity[i][j] = Integer.parseInt(st[j]);
           }
           boolean[][] res = new boolean[20][20];
           // Ignore possible stars along the edges of the matrix
           for (int i = 0; i < 20; i++){
               res[0][i] = false;   res[i][0] = false;
               res[19][i] = false;   res[i][19] = false;
           }
           for (int i = 1; i < 19; i++){
               for (int j = 1; j < 19; j++){
                   if ((intensity[i][j] + intensity[i-1][j] + intensity[i][j-1] + intensity[i+1][j] + intensity[i][j+1] + 0.0)/5.0 > 6.0)
                       res[i][j] = true;
                   else
                       res[i][j] = false;
               }
           }
           File file = new File("Hello1.txt");
       // creates the file
       file.createNewFile();
       // creates a FileWriter Object
       FileWriter writer = new FileWriter(file);
          
           for (int i = 0; i < 20; i++){
               for (int j = 0; j < 20; j++){
                   // if no star
                   if (res[i][j] == false)
                       writer.write("__ ");
                   // if star is there
                   else
                       writer.write("_' ");
               }
               writer.write(" ");
           }
       }
       catch (IOException e) {
           e.printStackTrace();
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote