How do I create a counter method? (Java)

Discussion in 'Mac Programming' started by DesertFox, Mar 8, 2007.

1. DesertFox macrumors member

Joined:
Nov 25, 2004
#1
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!

2. lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#2
Hi

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

Code:
```if ( a[ row ][ column ] == min )
```
If you are looking for the column with the minimum value then you need something like this:-

Code:
```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?

Code:
```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

3. DesertFox thread starter macrumors member

Joined:
Nov 25, 2004
#3
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.

4. MrBubbles macrumors member

Joined:
Apr 8, 2003
Location:
Mad City
#4
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.

5. scan macrumors 6502

Joined:
Oct 24, 2005
#5
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.

6. scan macrumors 6502

Joined:
Oct 24, 2005
#6
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.

7. MarkCollette macrumors 68000

Joined:
Mar 6, 2003
Location:
Toronto, Canada
#7
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.

Joined:
Jan 16, 2002