# Homework Help: Showing that matter has an atomic nature using Java program

1. May 10, 2015

### s3a

1. The problem statement, all variables and given/known data
http://pastebin.com/aS3vTR2V [Broken]

2. Relevant equations
N/A

3. The attempt at a solution
I'm confused about what I need to do for the BlobFinder constructor. Someone told me that I should “initialize the ArrayList blobs”, then “create a 2D array of booleans called marked, having the same dimensions as picture.” then “enumerate the pixels of picture, and for each pixel (i, j): 1. Create a Blob object called blob., 2. Call findBead() with the right arguments. and 3. Add blob to blobs if it has a non-zero mass.”,

but I'm confused as to why that is what I need to do. Don't I only need to do the following for the BlobFinder contructor?:

Code (Java):

for(int i = 0; i < picture.height(); i++) {

for(int j = 0; j < picture.width(); j++) {

Blob blob = new Blob();
blob.add(i, j);
blobs.add(blob);
}
}
I don't even need someone to write Java code for me, but if someone could just explain to me in words and/or pseudocode what I need to do, it would help a lot.

Any help would be greatly appreciated!

Last edited by a moderator: May 7, 2017
2. May 11, 2015

### Staff: Mentor

With respect to ArrayList objects, yes you need to initialize them before you add something to them

Code (Java):

ArrayList blobs = new ArrayList();

If you don't do that then you'll get a NullPointerException thrown when you try to execute the blobs.add(blob) in your "for" loop.

3. May 11, 2015

### Zondrina

According to the instructions, the constructor looks like this:

Code (Text):

// find all blobs in the picture using the luminance threshold tau
public BlobFinder(Picture picture, double tau)

So the constructor accepts the picture, and the constant $\tau$, which is used to classify the pixels as background or foreground. You will classify each pixel as it is read from the picture as background or foreground inside the constructor itself.

You also know a polystyrene bead (a blob) is represented by a disc-like shape, which is composed of 25 connected foreground pixels.

Once you have found 25 connected foreground pixels, you have found a blob. A new field: ArrayList<Blob> blobs, should be used to hold onto the blobs once you've found them.

4. May 11, 2015

### s3a

Thanks for the replies.

I thought that a blob was one pixel and a bead was 25+ blobs, but I misread the instructions; 1 blob = 1 bead.

I'm currently confused as to what the purpose of the array pointed to by the variable named "marked" (in the BlobFinder class) is for.

Should the findBead method (in the BlobFinder class) have a return type of Blob?

Also, are the int i and int j parameters of the findBead method (in the BlobFinder class) supposed to be the horizontal and vertical coordinates of the center of mass of a Blob, or can it be any point and not have to be a center of mass?

5. May 11, 2015

### Zondrina

There is no findBead() method.

If you tried to understand the instructions more carefully, you would see you were given an API for the BlobFinder class:

Code (Text):

Next, write a data type BlobFinder that has the following API. Use depth-first search to efficiently identify the blobs.

public class BlobFinder
------------------------------------------------------------------------------------------------
// find all blobs in the picture using the luminance threshold tau
public BlobFinder(Picture picture, double tau)

// return the number of beads with >= P pixels
public int countBeads(int P)

// return all beads with >= P pixels
public Blob[] getBeads(int P)

The details about the BlobFinder constructor were mentioned in post #3. Make it so the constructor initializes the ArrayList<Blob> blobs field. Use depth-first-search to efficiently identify the blobs.

This field will be useful in the following methods.

Using a for each construct, or an iterator if you prefer, count the number of beads with $\geq P$ pixels and return the result. Remember $P$ is a command line argument.

Using the loop of your choice, return a reference to a Blob[] array containing all the blobs with $\geq P$ pixels and return the result.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted