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

Images in computers are stored as two-dimensional arrays, each grid cell storing

ID: 3688300 • Letter: I

Question

Images in computers are stored as two-dimensional arrays, each grid cell storing a number for a color.

For this lab, we are going to create a class called Bitmap, which stores a simple black and white image.

Because different fonts display spaces at different widths, we are going to use "-" for the white squares and "*" for the black squares. If you are using DrJava, you can also change the output font to Courier New by going to the Edit menu, selecting Preferences -> Display Options -> Fonts, and setting the Main Font to Courier New.

Variables

String image[][] - A 10x10 two-dimensional array of Strings to hold the symbols. Because different fonts display spaces at different widths, we are going to use "-" for the white squares and "*" for the black squares.

Methods

Bitmap(int a[]) - This constructor initializes the images to all "-" symbols. The input parameter array has the coordinates of the "*" symbols listed as pairs. So elements a[0] and a[1] hold the coordinates for the row, column for the first "*", elements a[2] and element a[3] are the coordinates for the second "*", etc. Before processing, the constructor needs to check that all the coordinates listed in the array a are between 0 and 9 inclusive. The constructor also needs to check that the array consists of an even number of elements. This will ensure that there are no unmatched points. If either of these is not true, then the array should not have any "*" symbols stored.

public void flipHorizontal() - The "*" symbols in the array are flipped horizontally, or in others words, from left to right.

public void flipVertical() - The "*" symbols in the array are flipped vertically, or in other words, from top to bottom.

public void reverse() - Changes the "-" symbols to "*" symbols, and vice versa.

public String toString() - Returns a multiline String representing the image.

Please download the runner class http://term.2.s3.amazonaws.com/student_Bitmap_runner.java and verify that the class output matches the sample run that follows. We will use a different runner to grade the program. Don't forget to change the runner to test different values to make sure your program fits the requirements.

Sample Run:

----------
-*-**-----
-**--*----
-*----*---
**----**--
-*-*--*---
-*-*--*---
-*-*--*---
-******---
----------

after horizontal flip:
----------
-----**-*-
----*--**-
---*----*-
--**----**
---*--*-*-
---*--*-*-
---*--*-*-
---******-
----------

after vertical flip:
----------
-******---
-*-*--*---
-*-*--*---
-*-*--*---
**----**--
-*----*---
-**--*----
-*-**-----
----------

after reverse flip:
**********
*-*--*****
*--**-****
*-****-***
--****--**
*-*-**-***
*-*-**-***
*-*-**-***
*------***
**********

PLEASE WRITE YOUR OWN CODE, I AM STRUGGLING WITH THIS ASSIGMENT, AND EVERY ANSWER I HAVE BEEN GIVEN SO FAR AS BEEN INCORRECT.

Explanation / Answer

student_Bitmap_runner.java

import java.io.IOException;

public class student_Bitmap_runner
{

public static void main(String arg[]) throws IOException
{
int points[] = { 1, 1, 1, 3, 1, 4, 2, 1, 2, 2, 2, 5, 3, 1, 3, 6, 4, 0,
    4, 1, 4, 6, 4, 7, 5, 1, 5, 3, 5, 6, 6, 1, 6, 3, 6, 6, 7, 1, 7,
    3, 7, 6, 8, 1, 8, 2, 8, 3, 8, 4, 8, 5, 8, 6

};
Bitmap b = new Bitmap(points);

System.out.println(b);
System.out.println("after horizontal flip: ");
        
        b.flipHorizontal();
System.out.println(b);
      
      
System.out.println("after vertical flip: ");
        //flip back before flip Vertical
        b.flipHorizontal();
b.flipVertical();
System.out.println(b);

System.out.println("after reverse flip: ");
        //flip back before reverse
        b.flipVertical();
b.reverse();
System.out.println(b);

}

}
Bitmap.java
import java.util.Arrays;

class Bitmap {
    String[][] image = new String[10][10];

    Bitmap(int a[]) {
        populate(image);

        for (int i = 0; i < a.length - 1 && a.length % 2 == 0; i+= 2) {
            if (a[i] >= 0 && a[i] <= 9 && a[i+1] >= 0 && a[i + 1] <= 9)
                image[a[i]][a[i+1]] = "*";
            else {
                populate(image);
                break;
            }
        }
    }

    public void flipHorizontal() {
        for (int i = 0; i < image.length; i++) {
            String[] result = new String[image.length];
            for (int j = 0; j < image.length; j++) {
                result[image.length - 1 - j] = image[i][j];
            }
            image[i] = result;
        }
    }

    public void flipVertical() {
        String[][] result = new String[image.length][image.length];
        for (int i = 0; i < image.length; i++)
            result[image.length - 1 - i] = image[i];
        image = result;
    }

    public void reverse() {
        for (int i = 0; i < image.length; i++)
            for (int j = 0; j < image[i].length; j++)
              image[i][j] = (image[i][j] == "-") ? "*" : "-";
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (String[] i : image) {
            for (String p : i) {
                sb.append(p);
            }
            sb.append(" ");
        }
        return sb.toString();
    }

    private void populate(String[][] a) {
        for (String[] i : image)
           Arrays.fill(i, "-");
    }
}


Sample Run:

----------
-*-**-----
-**--*----
-*----*---
**----**--
-*-*--*---
-*-*--*---
-*-*--*---
-******---
----------

after horizontal flip:
----------
-----**-*-
----*--**-
---*----*-
--**----**
---*--*-*-
---*--*-*-
---*--*-*-
---******-
----------

after vertical flip:
----------
-******---
-*-*--*---
-*-*--*---
-*-*--*---
**----**--
-*----*---
-**--*----
-*-**-----
----------

after reverse flip:
**********
*-*--*****
*--**-****
*-****-***
--****--**
*-*-**-***
*-*-**-***
*-*-**-***
*------***
**********