Objectives

  • Write Java statements to create and use 2dimensional arrays
  • Write Java statements to process elements in a 2dimensional array

Overview

This week we'll build on what we did last week using simple arrays and talk about the concept of a multi dimensional array. That is, where the arrays last week could be visualized as a row or a column of values, a 2dimensional array could be visualized as a table of values.

With that in mind, this week we'll consider a situation where we have an image scanner that will record grayscale images between 0 (white) and 255 (black). The scanner will record the data in a 1dimensional array of int values.

In order to get this data, the image scanner will have a recording head that moves a set distance, captures a value, moves the head slightly, captures the next value, and so on. Now eventually the recording head will get to the edge of the area it is recording and have to go back to the start of the next line. This is the traditional way to read the data.

Figure: see image.

Reading data in the traditional fashion would produce an array of values:

1 2 3 4 5 6 7 8 9 10 11 12

However, some scanners are optimized to read the image faster. In order to do that, when the scan head reaches the end of the line, the head simply moves down to the next row and start recording in the opposite direction.

Figure: see image.

Reading the same image in the optimized manner would produce an array of values:

1 2 3 6 5 4 7 8 9 12 11 10

Because this exercise is about 2dimensional arrays, we won't worry about how the data is collected. Additionally because we want you to be able to more easily test your code, we'll provide a method that will allow you to read the contents of a plaintext file where the values are comma delimited. For example, the above data from a traditional reading would be in a file that looks like:

1,2,3,4,5,6,7,8,9,10,11,12

This method will simply return a 1dimensional array of int values.

The goal of this exercise is to write an application that allows the user to read in a file full of grayscale image data, then produce the corresponding 'image' file as a 2dimensional array in the output.

The Problem Statement GrayscaleImage

Develop a class named GrayscaleImage that will keep up with the raw data, a 1dimensional array of int values. This object will need a constructor that accepts the data to be stored. It will also need to define two different methods named, getTraditionalOutput and getOptimizedOutput. To make these methods more flexible, have each accept a number of columns that are used to create the table of values. Each method will return a 2dimensional array of int values containing the data as a table of values.

Examples

For example, if we were to use the file described above:

1 2 3 4 5 6 7 8 9 10 11 12

And we called getTraditionalOutput(3), then the method would create and return the following 2dimensional array with the values:

1 2 3
4 5 6
7 8 9
10 11 12

Similarly, if we called getTraditionalOutput(5), then the method would create and return:

1 2 3 4 5
6 7 8 9 10
11 12 0 0 0

(Note that the last three elements are 0 because we ran out of values from the raw data and there were still elements in our 2dimensional array)

Likewise, calling getOptimizedOutput(3) would produce

1 2 3
6 5 4
7 8 9
12 11 10

And getOptimizedOutput(5) would produce

1 2 3 4 5
10 9 8 7 6
11 12 0 0 0

Although we are providing this data file to you, your code should be able to handle ANY data file containing valid int data

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.