Question

In: Computer Science

Using JAVA The following code is able to read integers from a file that is called...

Using JAVA

The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage".

Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage".

The purpose of this will be to add both arrays and then get the average

Save the average onto a separte 3darray,lets say you call it "middlearray"

Then add startImage and middlearray and get the average then save it to another array called "fourtharray"

Then add finalImage and middle array and get the average then save it to another array called "fiftharray".

Print all 5 arrays

I can not submit data file because there are about 667,000 integers on the files. The file is from an image that got converted onto a ppm file. I don't really see the purpose in adding the file to this question either way. The code below is already capalbe of reading from one file i just need to code that will allow me to read from a different file (lets say we call that file "final.ppm". and then ansew the questions above.

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

public class P1
{

public static void main(String[] args) throws IOException
{
final int ROW=1000;
final int COL=667;
File file= new File("start.ppm");
Scanner inputF= new Scanner(file);

int[][][] startImage= new int[ROW][COL][3];
  
int row=0, col=0;
int line=1;
while (inputF.hasNext())
{
if(line<=4)
{
inputF.nextLine();
line++;
}
else
{
line+=3;
if (col< COL)
{
startImage[row][col][0]=inputF.nextInt();
startImage[row][col][1]=inputF.nextInt();
startImage[row][col][2]=inputF.nextInt();
col++;
}
else
{
row++;
col=0;
startImage[row][col][0]=inputF.nextInt();
startImage[row][col][1]=inputF.nextInt();
startImage[row][col][2]=inputF.nextInt();
col++;

}
}
  
}
inputF.close();


for(row=0;row {
for(col=0;col {
for(int color=0;color<3;color++)
{
System.out.println(startImage[row][col][color]);

}
}
}

}

}

Solutions

Expert Solution

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

public class P1 {
        static final int ROW = 1000;
        static final int COL = 667;

        public static void readImage(int[][][] startImage, String fileName) {
                Scanner inputF = new Scanner(fileName);

                int row = 0, col = 0;
                int line = 1;
                while (inputF.hasNext()) {
                        if (line <= 4) {
                                inputF.nextLine();
                                line++;
                        } else {
                                line += 3;
                                if (col < COL) {
                                        startImage[row][col][0] = inputF.nextInt();
                                        startImage[row][col][1] = inputF.nextInt();
                                        startImage[row][col][2] = inputF.nextInt();
                                        col++;
                                } else {
                                        row++;
                                        col = 0;
                                        startImage[row][col][0] = inputF.nextInt();
                                        startImage[row][col][1] = inputF.nextInt();
                                        startImage[row][col][2] = inputF.nextInt();
                                        col++;
                                }
                        }

                }
                inputF.close();
        }

        public static void printImage(int[][][] startImage) {
                for (int row = 0; row < ROW; row++) {
                        for (int col = 0; col < COL; col++) {
                                for (int color = 0; color < 3; color++) {
                                        System.out.println(startImage[row][col][color]);
                                }
                        }
                }
        }
        
        public static int[][][] getAverage(int[][][] p1, int[][][] p2) {
                int[][][] avgImage = new int[ROW][COL][3];
                
                for(int i=0; i<ROW; i++) {
                        for(int j=0; j<COL; j++) {
                                for(int k=0; k<3; k++) {
                                        avgImage[i][j][k] = p1[i][j][k] + p2[i][j][k];
                                }
                        }
                }
                
                return avgImage;
        }
        
        public static void main(String[] args) throws IOException {
                int[][][] startImage = new int[ROW][COL][3];
                readImage(startImage, "start.ppm");
                printImage(startImage);

                int[][][] finalImage = new int[ROW][COL][3];
                readImage(startImage, "final.ppm");
                printImage(startImage);

                int[][][] middlearray = getAverage(startImage, finalImage);
                printImage(middlearray);

                int[][][] fourtharray = getAverage(startImage, middlearray);
                printImage(fourtharray);

                int[][][] fiftharray = getAverage(finalImage, middlearray);
                printImage(fiftharray);
                
        }

}
**************************************************
I have converted your code to modules and hence, The main method is very clean now.. And you can add more functionality if needed.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt"...
Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt" (you can find this in the current directory/data) Add each word to a hash map after splitting words and throwing away punctuation (see the demo). open a file called "spell.txt" to check in the current directory. Print out every word in spell.txt that is NOT in the dictionary. Example: if spell.txt contains: hello, this is a test. 2152189u5 Misspelled! cApitalized The output should be:...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
using java, parse a text file to be able to list the word(s) with the highest...
using java, parse a text file to be able to list the word(s) with the highest frequency in a sentence across all sentences in the whole file, also print its frequency and the corresponding sentence. cannot use hash maps. assume text file will be multiple paragraphs long.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT