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

What will be the perimeterof method for this question : It\'s one of the 5 metho

ID: 3702850 • Letter: W

Question

What will be the perimeterof method for this question : It's one of the 5 methods required to run this code in Java CULEARN Back Forward 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 atempts 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(Le 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 0o) 10 1 2 345678 9 10 (1) The Partlmage class Start with the class defined below that represents one of these images using a 2-dimensional array of booleans import Javafk.geonetry.Point2D public claas Paztinage private booleann pixels private booleanvisited Private int public Fart Image (int r int c) new booleanrIc]a pixels-new boolean riiola

Explanation / Answer

Source Code

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

private void expandFrom(int r, int c) {

try {
boolean bool = pixels[r][c];
if (bool) {
visited[r][c] = true;
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++;
}

}

private int perimeterOf(int r, int c) {
return 0;
}

public int perimeter() {
Point2D p = findStart();
expandFrom((int) p.getX(), (int) p.getY());
return perimeterOf((int) p.getX(), (int) p.getY());
return border;
}


public boolean isBroken() {
Point2D p = findStart();
expandFrom((int) p.getX(), (int) p.getY());
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++;
}
}
}
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