I am having trouble with my explore method() I have to create a picture whose height is the max of the heights, and width is the sum of the widths of the stored pictures.The pictures in the Album should be copied so far are copied into a new picture and that picture is displayed by calling the explore() method on it. This is what I have:
public boolean addPicture( Picture thePicture, int where )
{
int index = nPictsInAlbum;
pictArray[index] = pictArray[index-1];
while( index > where )
{
pictArray[where] = thePicture;
nPictsInAlbum ++;
}
return true;
}
public void explore()
{
int maxHeight = 0; //max height for the picture
int value = 0;
int biggest = 0;
for(int i = 0; i < pictArray.length; i++)
{
nPictsInAlbum = pictArray[i];
if(value > maxHeight)
{
biggest = value;
maxHeight = i;
}
}
I'm assuming that the variable biggest is the variable that is holding the total width.
You are doing a few things wrong with your code, and you'll have to fix these first
You are reusing the nPictsInAlbum multiple times for different types of variables. You shouldn't do that. I'm not a java programmer but it is at least a bad practice in other languages because it causes confusion. You use that variable in the addPicture
method to track the latest index. But in the explore
method, you use it to temporarily store an individual picture that you're accessing.
In the explore
method, you are not accessing the properties\fields of the picture object. Without knowing what properties\fields a picture object has, we cannot correct your code entirely.
If you fix those two problems, then the logic errors you have in the explore
method are
(assuming that the variable biggest
is the total width, if so you should rename it to be more descriptive) You are only setting the total width when you have figured out that the height of the current picture you have is bigger in height than a previously stored picture. You need to always set total width when you loop each picture regardless.
When you set the total width of the picture, you are replacing the total, not adding it. in your example biggest = value
would not add it. You would want biggest = biggest + value
to do that
You want to make sure to take your time when you make the methods. Tactfully think about what is it going to do, what variables will you need, what other variables will you access, and what will you do with them. For example, if the point of explore
is to determine the max height and total width you would think like this:
totalWidth = totalWidth + picture.width
Hope this helps you to get your code cleaned up and working better.