Question

import java.util.Scanner; import java.io.*; public class P1 { static final int ROW = 1000; static final...

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]);
                                }
                        }
                }
        USING THIS SAME CODE,CONVERT THE ARRAYS THAT ARE BIENG DISPLAYED INTO FILES USING FILEWRITER
      


  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);
                
        }

}
**************************************************

Homework Answers

Answer #1
Modify the printImage method as below:

        public static void printImage(int[][][] startImage) throws FileNotFoundException {
                PrintWriter writer = new PrintWriter(new File("output.txt"));
                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]);
                                        writer.println(startImage[row][col][color]);
                                }
                        }
                }
                writer.close();
                
                System.out.println("Created output.txt in " + System.getProperty("user.dir"));
        }
**************************************************

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT