Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class MyImage extends Image{ public int counter =1; public Image( int rows, int cols){ super (rows, cols); this . rows =rows; this . cols

 

image text in transcribed

image text in transcribed

public class MyImage extends Image{ public int counter=1; public Image(int rows, int cols){ super(rows, cols); this.rows=rows; this.cols=cols; } public Image(boolean[][] data){ super(data); } @Override public Position find() { for(int i=0;ithis.pixels.length;i+=1){ for(int j=0;jthis.pixels[0].length;j+=1){ if(this.pixels[i][j]==true){ Position a=new Position(i,j); return a; } } } return null; } @Override public int sizeOfConnectedComponent(Position p){ int rows=p.getX(); int cols=p.getY(); // check up;  if(cols>0 && pixels[rows][cols]==pixels[rows][cols-1]){ counter+=1; Position newp=new Position(rows,cols-1); if(cols>0){ return sizeOfConnectedComponent(newp); } } // check down;  if(colspixels[rows].length && pixels[rows][cols]==pixels[rows][cols+1]){ counter+=1; Position newp=new Position(rows,cols+1); if(colspixels[rows].length){ return sizeOfConnectedComponent(newp); } } // check left;  if(rows>0 && pixels[rows][cols]==pixels[rows-1][cols]){ counter+=1; Position newp=new Position(rows-1,cols); if(rows>0){ return sizeOfConnectedComponent(newp); } } // check right;  if(colspixels.length &&pixels[rows][cols]==pixels[rows+1][cols]){ counter+=1; Position newp=new Position(rows+1,cols); if(colspixels.length){ return sizeOfConnectedComponent(newp); } } return counter; } @Override public boolean isConnectedWithoutHoles() { boolean firstcolour=this.pixels[0][0]; boolean secondcolour; for(int i=0;ithis.pixels.length;i+=1){ for(int j=0;jthis.pixels[0].length;j+=1){ if(pixels[i][j]!=firstcolour){ secondcolour=pixels[i][j]; } } } return false; }

In java Se8, I am trying to do a pixel checker where true is black and false is white, however , base on the copyable code i pose above, I realize that it will count pixel more than one time but i do not know how to set the start point of four check direction at the beginning point since the method has to be recursive. And since this Myimage class is extended from image class, it shows there is no default constructor available in image which i could not figure out why. And the final question is how could i check whether or not there is holes in the image? since the image could have both color but not one colour in the component of the other colour?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions