In this assignment, you will practice using recursion Assume that we have an ass
ID: 3702657 • Letter: I
Question
In this assignment, you will practice using recursion Assume that we have an assembly line that can take a picture of a machine part which moves along a conveyor belt. The picture (or image) is represented as a 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 broken part will appear as a set of black pixels which are not all connected together (i.e., there is 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 2 1 2 3 4 5 6 78 9 10 columnsExplanation / Answer
part_image_tester.java
public class part_image_tester {
public static void main(String[] args) {
PartImage pixA = PartImage.exampleA();
PartImage pixB = PartImage.exampleB();
PartImage pixC = PartImage.exampleC();
PartImage pixD = PartImage.exampleD();
System.out.println(" Part A:");
System.out.println(" starts at: " + PartImage.exampleA().findStart());
System.out.println(" size: " + PartImage.exampleA().part_size());
System.out.println(" broken: " + PartImage.exampleA().isBroken());
System.out.println(" perimeter: " + PartImage.exampleA().perimeter()+ " ");
pixA.print();
System.out.println(" Part B:");
System.out.println(" starts at: " + PartImage.exampleB().findStart());
System.out.println(" size: " + PartImage.exampleB().part_size());
System.out.println(" broken: " + PartImage.exampleB().isBroken());
System.out.println(" perimeter: " + PartImage.exampleB().perimeter()+ " ");
pixB.print();
System.out.println(" Part C:");
System.out.println(" starts at: " + PartImage.exampleC().findStart());
System.out.println(" size: " + PartImage.exampleC().part_size());
System.out.println(" broken: " + PartImage.exampleC().isBroken());
System.out.println(" perimeter: " + PartImage.exampleC().perimeter()+ " ");
pixC.print();
System.out.println(" Part D:");
System.out.println(" starts at: " + PartImage.exampleD().findStart());
System.out.println(" size: " + PartImage.exampleD().part_size());
System.out.println(" broken: " + PartImage.exampleD().isBroken());
System.out.println(" perimeter: " + PartImage.exampleD().perimeter()+ " ");
pixD.print();
}
}
PartImage.java
import javafx.geometry.Point2D;
public class PartImage {
private boolean[][] pixels;
private boolean[][] visited;
private int rows;
private int cols;
public int bordr = 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;
}
}
}
private void examplepandFrom(int r, int c) {
try {
boolean bool = pixels[r][c];if (bool) {
visited[r][c] = true;
pixels[r][c] = false;
examplepandFrom(r + 1, c);
examplepandFrom(r - 1, c);
examplepandFrom(r, c + 1);
examplepandFrom(r, c - 1);
} else if (!visited[r][c]) {
bordr += 1;
}
} catch (java.lang.ArrayIndexampleOutOfBoundsException e) {
this.bordr++;
}
}
private int perimeterOf(int r, int c) {
return 0;
}
public int perimeter() {
Point2D p = findStart();
examplepandFrom((int) p.getX(), (int) p.getY());
return bordr;
}
public boolean isBroken() {
Point2D p = findStart();
examplepandFrom((int) p.getX(), (int) p.getY());
return (part_size() != 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 part_size() {
int count = 0;
for (boolean[] pixcol : pixels) {
for (boolean pixrow : pixcol) {
if (pixrow) {
count++;
}
}
}
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);
}
}