Question

In: Computer Science

home / study / engineering / computer science / computer science questions and answers / Using...

home / study / engineering / computer science / computer science questions and answers / Using JAVA The Following Code Is Able To Read Integers From A File That Is Called "start.ppm" ...

Your question has been answered

Let us know if you got a helpful answer. Rate this answer

Question: Using JAVA The following code is able to read integers from a file that is called "start.ppm" ont...

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 each file and both files are the same size. 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 capable 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". Please write an individual method for each file that is being read. Then add answer the questions from above.

I have already posted this question 2 times and its been answered by the same person and everytime i run the program it just prints only 0's. If this questinon gets answered by the same person again its OK.

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

/***********************************P1.java***********************************/

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

public class P1 {

   public static void main(String[] args) throws IOException

   {
       final int ROW = 667;
       final int COL = 1000;
       File file = new File("start.ppm");
       File file2 = new File("end.ppm");
       Scanner inputF = new Scanner(file);
       Scanner inputF2 = new Scanner(file2);

       int[][][] startImage = new int[ROW][COL][3];
       int[][][] endImage = new int[ROW][COL][3];
       int[][][] middleArray = new int[ROW][COL][3];
       int[][][] fourthArray = new int[ROW][COL][3];
       int[][][] fifthArray = new int[ROW][COL][3];

       int row = 0, col = 0;
       int line = 1;

       while (inputF.hasNext()) {
           if (line <= 4)

           {
               inputF.nextLine();
               line++;
           } else {
               line++;

               if (col < COL) {
                   startImage[row][col][0] = Integer.parseInt(inputF.nextLine());
                   startImage[row][col][1] = Integer.parseInt(inputF.nextLine());
                   startImage[row][col][2] = Integer.parseInt(inputF.nextLine());
                   col++;
               } else {
                   row++;
                   col = 0;
                   startImage[row][col][0] = Integer.parseInt(inputF.nextLine());
                   startImage[row][col][1] = Integer.parseInt(inputF.nextLine());
                   startImage[row][col][2] = Integer.parseInt(inputF.nextLine());
                   col++;

               }

           }
       }
       inputF.close();

       row = 0;
       col = 0;
       line = 1;
       while (inputF2.hasNext()) {
           if (line <= 4)

           {
               inputF2.nextLine();
               line++;
           } else {
               line++;

               if (col < COL) {
                   row = 0;
                   endImage[row][col][0] = Integer.parseInt(inputF2.nextLine());
                   endImage[row][col][1] = Integer.parseInt(inputF2.nextLine());
                   endImage[row][col][2] = Integer.parseInt(inputF2.nextLine());
                   col++;
               } else {
                   row++;
                   col = 0;
                   endImage[row][col][0] = Integer.parseInt(inputF2.nextLine());
                   endImage[row][col][1] = Integer.parseInt(inputF2.nextLine());
                   endImage[row][col][2] = Integer.parseInt(inputF2.nextLine());
                   col++;

               }

           }
       }
       inputF2.close();

       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {
                   middleArray[row][col][color] = (startImage[row][col][color] + endImage[row][col][color]) / 2;

               }
           }
       }
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {
                   fourthArray[row][col][color] = (startImage[row][col][color] + middleArray[row][col][color]) / 2;

               }
           }
       }
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {
                   fifthArray[row][col][color] = (endImage[row][col][color] + middleArray[row][col][color]) / 2;

               }
           }
       }
       System.out.println("Start Image array: ");
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {

                   System.out.println(startImage[row][col][color]);

               }
           }
       }

       System.out.println("End Image array: ");
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {

                   System.out.println(endImage[row][col][color]);

               }
           }
       }
       System.out.println("Middle array: ");
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {

                   System.out.println(middleArray[row][col][color]);

               }
           }
       }
       System.out.println("Fourth Array: ");
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {

                   System.out.println(fourthArray[row][col][color]);

               }
           }
       }
       System.out.println("Fifth array: ");
       for (row = 0; row < ROW; row++) {
           for (col = 0; col < COL; col++) {
               for (int color = 0; color < 3; color++)

               {

                   System.out.println(fifthArray[row][col][color]);
               }
           }
       }
   }

}

I have your ppm files but
I'm not able to upload the output

Please let me know if you have any problem or modify the answer, Thanks:)


Related Solutions

home / study / engineering / computer science / computer science questions and answers / create...
home / study / engineering / computer science / computer science questions and answers / create a new java file, containing this code public class datastatsuser { public static void ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Create a new Java file, containing this code public class DataStatsUser { public static void... Create a new Java file, containing this code public class DataStatsUser { public static void main...
home / study / engineering / computer science / computer science questions and answers / Modify...
home / study / engineering / computer science / computer science questions and answers / Modify StudentLinkedList Class By Adding The Following Methods:  PrintStudentList: Print ... Your question has expired and been refunded. We were unable to find a Chegg Expert to answer your question. Question: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by call... Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in...
home / study / engineering / computer science / computer science questions and answers / 2....
home / study / engineering / computer science / computer science questions and answers / 2. design an er-diagram for a bank that implements the following requirements. the database ... Question: 2. Design an ER-diagram for a bank that implements the following requirements. The database you d... 2. Design an ER-diagram for a bank that implements the following requirements. The database you design should store information about customers, accounts, branches and employees • Customer: Customers are identified by their SSN....
home / study / engineering / computer science / computer science questions and answers / write...
home / study / engineering / computer science / computer science questions and answers / write a program that in c++: 1.prompts the user to enter a positive integer, think of this ... Question: Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this intege... Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this integer as representing a specific number of pennies. 2. The program...
home / study / engineering / computer science / questions and answers / this is c....
home / study / engineering / computer science / questions and answers / this is c. create three files to submit. contacts.h ... Question: This is C. Create three files to submit. Contacts.... Bookmark This is C. Create three files to submit. Contacts.h - Struct definition, including the data members and related function declarations Contacts.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode*...
home / study / engineering / computer science / questions and answers / working with layout...
home / study / engineering / computer science / questions and answers / working with layout managers. notes: 1. in part ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Working with Layout Managers. Notes: 1. In part 2,... Bookmark Working with Layout Managers. Notes: 1. In part 2, note that the Game class inherits from JPanel. Therefore, the panel you are asked to add to the center of the...
The questions read as follows: home / study / engineering / computer science / computer science...
The questions read as follows: home / study / engineering / computer science / computer science questions and answers / Course Grades Java Class In A Course, A Teacher Gives The Following Tests And Assignments: ... Question: Course grades java class In a course, a teacher gives the following tests and assignments: A lab ... course grades java class In a course, a teacher gives the following tests and assignments: A lab activity that is observed by the teacher and...
home / study / science / nursing / nursing questions and answers / This Is A...
home / study / science / nursing / nursing questions and answers / This Is A Theoretical Case Taken From VHA Intensive Ethics Advisory Committee Training, 1998, ... Your question has been posted. We'll notify you when a Chegg Expert has answered. Post another question. Next time just snap a photo of your problem. No typing, no scanning, no explanation required. Get Chegg Study App Question: This is a theoretical case taken from VHA Intensive Ethics Advisory Committee Training, 1998,...
home / study / science / nursing / nursing questions and answers / you are a...
home / study / science / nursing / nursing questions and answers / you are a public health researcher. you have been asked to identify a vaccine-preventable disease. ... Question: You are a public health researcher. You have been asked to identify a vaccine-preventable disease... You are a public health researcher. You have been asked to identify a vaccine-preventable disease. Your research design should focus on determining why health care workers are not receiving the vaccination for your selected vaccine-preventable...
home / study / science / biology / questions and answers / which of the following...
home / study / science / biology / questions and answers / which of the following best explains why atp is ... Question: Which of the following BEST explains why ATP is a ... Save Which of the following BEST explains why ATP is a positive regulator of aspartate transcarbamoylase (ATCase)? A. Aspartate is only available when ATP levels are high. B. ATP levels correspond to CTP levels, thus when both ATP and CTP are high, ATCase is active. C....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT