PDA

View Full Version : How do I create a counter method? (Java)




DesertFox
Mar 9, 2007, 12:32 AM
Hello,

Im trying to solve the infamous Traveling Salesman problem for my java class.

Basically what im doing is looping through the columns and rows of a 2-dimensional array and then trying to store the smallest value in the row/column in a method that i can later print to screen.

Here is the portion of the loop that is passing the info to the distanceTraveled method to add to the total distance travaled:

if ( a[ row ][ column ] == min )
{
int tempDist= a[ row ][ column ];
distanceTraveled( tempDist );
return column;
}

And here is the distanceTraveled method:

public static int DistanceTraveled ( int distance )
{
int total =+ distance;
return total;
}

...both files are in the same class. I would like to have the DistanceTraveled method add up the distances, but this is not happening since it is expecting an argument and if I pass a 0 just to call it it will return a 0.

So, my question, is how do I implement a counter that adds up the distances so I can return the total distance later in my program.

Thanks in advance!



lazydog
Mar 9, 2007, 06:48 AM
Hi

I'm not entirely sure what your code snippet is trying to do but a couple of things struck me about your code.


if ( a[ row ][ column ] == min )


If you are looking for the column with the minimum value then you need something like this:-


int minColumn = 0 ;
int min = Integer.MAX_VALUE ;

for ( column = 0 ; < numberOfColumns ; ++ column )
if ( a[ row ][ column ] < min )
{
min = a[row][column] ;
minColumn = column ;
}

return minColumn ;



Also the way you are calling DistanceTraveled() discards the calculated result for the total distance.

So something like this perhaps?



int minColumn = 0 ;
int min = Integer.MAX_VALUE ;

for ( column = 0 ; < numberOfColumns ; ++ column )
if ( a[ row ][ column ] < min )
{
min = a[row][column] ;
minColumn = column ;
}

//
// Assuming totalDistance is declared somewhere outside this scope!
//
totalDistance = distanceTraveled( min ) ;

return minColumn ;



I hope this helps.

b e n

DesertFox
Mar 9, 2007, 11:39 AM
Thanks for the reply.

My for loops are correct, i just didnt want to post 2 big nested for loops. My problem is taking the values in array[ row ][ column ] and sending them to a method that will update the total distance.

in the top i have:
public static void main( String args[] )
{
int array1[][] = { { 0, 10, 15, 5, 12 }, { 10, 0, 25, 16, 28 }, { 15, 25, 0, 6, 7 }, { 5, 16, 6, 0, 19}, { 12, 18, 7, 19, 0} };

int totalDistance = 0;

then in the for loop im valling the method DistanceTraveled by: (this is not the whole for loop, only the calling part since this is where its giving me errors.

int tempDist= a[ row ][ column ];
DistanceTraveled( tempDist );


and then the DistanceTraveled method:
public static void DistanceTraveled ( int distance )
{
totalDistance =+ distance;
}


and the error im getting:
Can not find symbol: Variable totalDistance.

If someone knows whats going wrong here, please help.

MrBubbles
Mar 9, 2007, 12:31 PM
The issue is that totalDistance is declared in the method "main" which means it only exists within that method. Your "distanceTraveled" method is looking for the variable either within itself or within the class -- it can't see anything that exists solely within "main". The easiest solution would be to declare totalDistance as a private member of your class and initialize it to zero in a constructor (you can't do it from within "main" because it's an instance variable and "main" is static).

Edit: I was looking back over your original code and saw that everything was declared static, so just declare totalDistance as private static. Then you should be able to initialize it and access it from anywhere.

scan
Mar 9, 2007, 02:23 PM
Im trying to solve the infamous Traveling Salesman problem for my java class.


Could you explain this infamous problem. I've never heard of the Traveling Salesman before. I think I have an idea what you want, but just to be sure, explain the problem.

scan
Mar 9, 2007, 02:26 PM
So, my question, is how do I implement a counter that adds up the distances so I can return the total distance later in my program.

From reading this, I think you what you want is an object with a static variable (counter in this case) so when you create an instances only 1 counter will be shared between these objects.

MarkCollette
Mar 9, 2007, 05:59 PM
I'm not sure why you're trying to solve the travelling salesman problem as a beginner problem. This is definitely a 4th year university problem.

But anyways, you don't want to be calculating the distance travelled on the stack, but should instead make another table, where each cell is the minimum cumulative distance travelled.

bbarnhart
Mar 9, 2007, 08:30 PM
http://en.wikipedia.org/wiki/Traveling_salesman_problem