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

Here are the clases so you can just add on to it!! PartImage class PartImageTest

ID: 3702275 • Letter: H

Question

Here are the clases so you can just add on to it!!

PartImage class

PartImageTester class

Assume that we have an assembly line that can take a picture of a machine part 2D grid of pixels which are either black or white. The pixels can be accessed by specifying the row and column of the pixel where rows and columns are specified by an integer value The machine examines the images and attempts to determine whether or not the parts are broken. A separation between one or more sets of black pixel groups. Here are some examples of four possible images. Note that (c) and (d) represent images of broken parts. The red border represents the perimeter of a part composed of black pixels 10 rows 1 2 3 4 5 6789 10 columns

Explanation / Answer

ANSWER:

PartImageTester.java

public class PartImageTester {

public static void main(String[] args) {

PartImage piA = PartImage.exampleA();

PartImage piB = PartImage.exampleB();

PartImage piC = PartImage.exampleC();

PartImage piD = PartImage.exampleD();

System.out.println(" Part A:");

System.out.println(" starts at: " + PartImage.exampleA().findStart());

System.out.println(" size: " + PartImage.exampleA().partSize());

System.out.println(" broken: " + PartImage.exampleA().isBroken());

System.out.println(" perimeter: " + PartImage.exampleA().perimeter()+ " ");

piA.print();

System.out.println(" Part B:");

System.out.println(" starts at: " + PartImage.exampleB().findStart());

System.out.println(" size: " + PartImage.exampleB().partSize());

System.out.println(" broken: " + PartImage.exampleB().isBroken());

System.out.println(" perimeter: " + PartImage.exampleB().perimeter()+ " ");

piB.print();

System.out.println(" Part C:");

System.out.println(" starts at: " + PartImage.exampleC().findStart());

System.out.println(" size: " + PartImage.exampleC().partSize());

System.out.println(" broken: " + PartImage.exampleC().isBroken());

System.out.println(" perimeter: " + PartImage.exampleC().perimeter()+ " ");

piC.print();

System.out.println(" Part D:");

System.out.println(" starts at: " + PartImage.exampleD().findStart());

System.out.println(" size: " + PartImage.exampleD().partSize());

System.out.println(" broken: " + PartImage.exampleD().isBroken());

System.out.println(" perimeter: " + PartImage.exampleD().perimeter()+ " ");

piD.print();

}

}

PartImage.java

import javafx.geometry.Point2D;

public class PartImage {

private boolean[][] pixels;

private boolean[][] visited;

private int rows;

private int cols;

public int border = 0;

public int runamount = 0;

public PartImage(int r, int c) {

rows = r;

cols = c;

visited = new boolean[r][c];

pixels = new boolean[r][c];

}

public PartImage(int rw, int cl, byte[][] data) {

this(rw, cl);

for (int r = 0; r < 10; r++) {

for (int c = 0; c < 10; c++) {

if (data[r][c] == 1)

pixels[r][c] = true;

else

pixels[r][c] = false;

}

}

}

// You will re-write the 5 methods below

private void expandFrom(int r, int c) {

try {

//System.out.print(runamount++ + " ");

//Worse case is all parts are connected it will run 4n times where n is the amount of blocks.

//Best case is if the start position is disconnected from everything, it will run 4 times.

boolean bool = pixels[r][c];

//System.out.print(bool);

if (bool) {

visited[r][c] = true;

//System.out.print(connected);

pixels[r][c] = false;

expandFrom(r + 1, c);

expandFrom(r - 1, c);

expandFrom(r, c + 1);

expandFrom(r, c - 1);

} else if (!visited[r][c]) {

border += 1;

}

} catch (java.lang.ArrayIndexOutOfBoundsException e) {

this.border++;

//System.out.println("Here's my border: " + border);

}

}

private int perimeterOf(int r, int c) {

return 0;

}

public int perimeter() {

Point2D p = findStart();

expandFrom((int) p.getX(), (int) p.getY());

//System.out.println("Here's my border: " + border);

//Changed the following return statement

//return perimeterOf((int) p.getX(), (int) p.getY());

//In this return statement

return border;

}

public boolean isBroken() {

Point2D p = findStart();

expandFrom((int) p.getX(), (int) p.getY());

//System.out.println("Here is my part size: " + partSize());

//System.out.println("Here is my connected: " + connected);

//System.out.println("Here's my border: " + border);

return (partSize() != 0);

}

public void print() {

String string = "";

for (boolean[] pixcol : pixels) {

for (boolean pixrow : pixcol) {

if (pixrow) {

string += "*";

} else {

string += "-";

}

}

string += " ";

}

System.out.printf(string);

}

public Point2D findStart() {

for (int y = 0; y < cols; y++) {

for (int x = 0; x < rows; x++) {

if (pixels[y][x]) {

return new Point2D(x, y);

}

}

}

return null;

}

public int partSize() {

int count = 0;

for (boolean[] pixcol : pixels) {

for (boolean pixrow : pixcol) {

if (pixrow) {

count++;

}

}

}

//System.out.print(count + " " + connected );

//System.out.println("Here is my conected" + connected);

return count;

}

public static PartImage exampleA() {

byte[][] pix = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

{0, 1, 1, 1, 1, 1, 1, 0, 0, 0},

{0, 1, 1, 1, 1, 1, 1, 0, 0, 0},

{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},

{0, 0, 0, 1, 1, 1, 1, 1, 1, 0},

{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},

{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},

{0, 1, 1, 1, 1, 1, 1, 0, 0, 0},

{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

return new PartImage(10, 10, pix);

}

public static PartImage exampleB() {

byte[][] pix = {{1, 0, 1, 0, 1, 0, 1, 0, 0, 0},

{1, 0, 1, 0, 1, 0, 1, 1, 1, 1},

{1, 0, 1, 0, 1, 0, 1, 0, 0, 0},

{1, 0, 1, 0, 1, 0, 1, 1, 1, 1},

{1, 0, 1, 0, 1, 0, 1, 0, 0, 0},

{1, 0, 1, 0, 1, 0, 1, 1, 1, 1},

{1, 1, 1, 1, 1, 1, 1, 0, 0, 0},

{0, 1, 0, 1, 0, 0, 1, 1, 1, 1},

{0, 1, 0, 1, 0, 0, 1, 0, 0, 0},

{0, 1, 0, 1, 0, 0, 1, 0, 0, 0}};

return new PartImage(10, 10, pix);

}

public static PartImage exampleC() {

byte[][] pix = {{1, 1, 1, 0, 0, 0, 1, 0, 0, 0},

{1, 1, 1, 1, 0, 0, 1, 1, 1, 0},

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},

{0, 1, 1, 1, 0, 0, 1, 0, 0, 0},

{0, 0, 1, 0, 0, 0, 0, 0, 0, 0},

{1, 0, 0, 0, 1, 1, 0, 1, 1, 1},

{1, 1, 0, 1, 1, 1, 1, 1, 1, 1},

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},

{0, 0, 1, 1, 0, 1, 1, 1, 1, 1},

{0, 0, 1, 0, 0, 0, 1, 1, 0, 0}};

return new PartImage(10, 10, pix);

}

public static PartImage exampleD() {

byte[][] pix = {{1, 0, 1, 0, 1, 0, 1, 1, 0, 0},

{1, 0, 1, 0, 0, 0, 1, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},

{1, 0, 1, 1, 1, 1, 1, 1, 1, 0},

{1, 0, 0, 1, 0, 0, 1, 0, 0, 0},

{1, 1, 0, 0, 0, 1, 1, 0, 0, 1},

{0, 1, 0, 0, 0, 0, 0, 0, 1, 1},

{0, 1, 0, 1, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 1, 1, 0, 0, 0}};

return new PartImage(10, 10, pix);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote