# Thread: Help implementing a method

1. Member
Join Date
Jan 2008
Posts
36
Rep Power
0

## Help implementing a method

I am trying to implement my recTraveler() method in my driver. However I am unsure how to call it, can someone help?

The Recursive Traveler method should print out the final distance of a path traveled, keeping the first and last destinations constant. It needs to include every possible combination for "n" number of cities. I believe I have accomplished this.

Ex:

City1,400,200
City2,100,200
City3,300,240
City4,400,100

Final output should be

City1, City2, City3, City4 The final distance is: #
City1, City3 City2, City4 The final distance is: #

Java Code:
```import java.util.ArrayList;

/**
*
* @version 1 .0
*/
public class Traveler {

public String city;
public int x;
public int y;

public Traveler(String next, int nextInt, int nextInt2) {

city = next;
x = nextInt;
y = nextInt2;

}

public static double theDistance(Traveler first, Traveler second) {

int xPoint = second.x - first.x;
int yPoint = second.y - first.y;

return Math.sqrt(xPoint * xPoint + yPoint * yPoint);

}

public static void recTraveler(Traveler[] travelerArr, ArrayList<Traveler> theOrder, Traveler firstOne, Traveler finalOne){
int sumofDistance = 0;

ArrayList<Traveler> travelerOrd = null;
if (travelerOrd.size() == travelerArr.length)
{
if(travelerOrd.get(0) == firstOne && travelerOrd.get(travelerOrd.size()-1) == finalOne)
{
for (int j = 0; j < travelerOrd.size() - 1; j++)
{
sumofDistance += theDistance(travelerOrd.get(j), travelerOrd.get(j+1));
}
System.out.println(sumofDistance);
}
}
for(int k = 0; k < travelerArr.length; k++)
{
if (travelerArr[k] != null)
{
Traveler temp = travelerArr[k];
travelerArr[k] = null;

recTraveler(travelerArr, travelerOrd, firstOne, finalOne);

travelerOrd.remove(travelerOrd.size()-1);
travelerArr[k] = temp;
}

}

}
}//end traveler```
Java Code:
```import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScanCities {

public static void main(String[] args) {

Scanner scanFile = new Scanner(System.in);

File file = new File("cities.dat");

try {
} catch (FileNotFoundException e) {

System.exit(0);
}

while (scanFile.hasNext()) {
parseLine(scanFile.next());
}

}

private static void parseLine(String line) {
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
String name = lineScanner.next();
int x = lineScanner.nextInt();
int y = lineScanner.nextInt();

System.out.println(name + ", " + x + ", " + y);
}

}```

2. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
6
can you add some comment to the code?

Java Code:
```ArrayList<Traveler> travelerOrd = null;
if (travelerOrd.size() == travelerArr.length)
{
// run if travelerArr is empty?
}```

i have a idea
Java Code:
```function recursiveMethod(ArrayList al1)
{
for( int i = 1; i < ArrayList.size() - 2; i++)
{
ArrayList<String> al2 = (ArrayList<String>)al1.clone();
al2.remove(i);
recursiveMethod(al2);
}
}```
Last edited by mtyoung; 03-16-2009 at 04:57 AM.

3. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
Where do you suggest putting this code?

4. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Where do you suggest putting this code?
I think it is you suggest the code for someplace :) Correct your question.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•