Results 1 to 4 of 4
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question 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;
    
    /**
     * 
     * @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
    Java 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);
    	}
    
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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. #3
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question

    Where do you suggest putting this code?

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by VinceGuad View Post
    Where do you suggest putting this code?
    I think it is you suggest the code for someplace :) Correct your question.

Similar Threads

  1. implementing a button instead.
    By birdofprey in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2008, 04:21 AM
  2. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  3. Implementing more than one interfaces
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:50 AM
  4. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:49 AM
  5. Help implementing JDBC
    By mooey1232003 in forum New To Java
    Replies: 6
    Last Post: 07-11-2007, 11:15 PM

Posting Permissions

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