Search code examples
javadrjava

I am confuse on with where to start with this method?


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

Solution

  • 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

    1. 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.

    2. 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

    1. (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.

    2. 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:

    1. I need a reusable piece of code that will determine the maximum height out of all pictures. It will also get a total width of all pictures.
    2. Therefore I will need two variables: maxHeight and totalWidth.
    3. From this code I need iterate over an array of pictures.
    4. In that loop, I need to check to see if the picture's height is greater then maxHeight. If so, I need to set maxHeight to the picture's height
    5. To access the picture's height, I would need to use the (I don't know what the picture's object structure is so I'm guessing) picture.height property.
    6. Regardless of the height of the picture, I need to add the width of the picture to a running total. So I would use totalWidth = totalWidth + picture.width

    Hope this helps you to get your code cleaned up and working better.