# Help implementing a method

• 03-16-2009, 02:19 AM
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: #

Code:

```import java.util.ArrayList; /**  *  * @author Vince Guadalupe  * @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;                                 travelerOrd.add(temp);                                 recTraveler(travelerArr, travelerOrd, firstOne, finalOne);                                 travelerOrd.remove(travelerOrd.size()-1);                                 travelerArr[k] = temp;                         }                 }         } }//end traveler```
Code:

```import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; 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 {                         scanFile = new Scanner(new FileReader(file));                 } 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);         } }```
• 03-16-2009, 04:32 AM
mtyoung
can you add some comment to the code?

Code:

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

i have a idea
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);   } }```
• 03-16-2009, 06:06 AM