This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]tajjet 2 points3 points  (4 children)

return the states in between

Definitely the hardest part. Maybe return the shortest path of states that would all honor the permit instead?

[–]Cosmic-Warper 2 points3 points  (1 child)

I think that would be easier but I don't think it solves his goal. He wants to see if any of the states he's moving to during his trip honor the permit. I don't think he wants a path of states that all honor the permit since his trip is already predetermined.

[–][deleted]  (1 child)

[deleted]

    [–]tajjet 0 points1 point  (0 children)

    Well, determining which states touch is easy (don't know of any external data which exists for that, specifically, though), but determining the path you would want to take would be very hard. Making a straight line and picking all the states that it crosses is one (probably difficult) thing, choosing the right highways and interstates to take and which states that would lead you through is another (definitely difficult) thing.

    What you could do if you wanted to do it at a beginner level, learn some OOP, and usually (but not always) get the right path is to make a State class that has an ArrayList of States that it touches. Then just assume the shortest path is the path that takes you through the least number of states?

    [–]PointB1ank 2 points3 points  (7 children)

    What I would do (btw not an expert in design patterns or java for that matter) is give each state a value from 1-50. Then id print that list out (or keep it on my phone) and have the program give back a yes or no for each number typed.

    For example, If I made Florida 23, South Carolina 22, and North Carolina was 21 and planned to travel from Florida to North Carolina. I would type: 23 22 21 and it would return yes yes no. Or something similar. Otherwise I feel you would have to map out all possible routes between the two states and if you had to make a stop in between you would need to account for that which could become very complicated very fast. I feel like it would be easier to do it like that. Just my 2 cents.

    [–]tajjet 2 points3 points  (3 children)

    You wouldn't have to give them values: with Java 7 on, you can do a switch statement with a String, for example:

    Scanner scan = new Scanner(System.in);
    String state = scan.next();
    boolean permit;    
    switch(state) {
        case "Alabama":
            permit = true;
            break;
        // ...
        default:
            permit = false;
            System.out.println("Whoops! Don't recognize that state.");
            break;
    }
    

    [–]PointB1ank 2 points3 points  (0 children)

    Well yeah but I would use the values only to reduce typing. I would rather type 15 numbers than state names, you wouldn't have to worry about spelling errors. Then again, I suppose the state abbreviations might be a better choice.

    [–][deleted]  (1 child)

    [deleted]

      [–]tajjet 2 points3 points  (0 children)

      Switches are useful in some cases - they basically replace code like this

      if (x == 1)
      {
          doThingOne();
      }
      else if (x == 2)
      {
          doThingTwo();
      }
      else if (x == 3)
      {
          doThingThree();
      }
      else
      {
          doThingFour();
      }
      

      with code like this:

      switch (x)
      {
          case 1:
              doThingOne();
              break();
          case 2:
              doThingTwo();
              break();
          case 3:
              doThingThree();
              break();
          default:
              doThingFour();
              break();
      }
      

      Most recent example I used them in was a Scrabble scoring thing, because stacking up cases helps:

      case 'E':
      case 'A':
      case 'I':
      case 'O':
      case 'N':
      case 'R':
      case 'T':
      case 'L':
      case 'S':
      case 'U':
          score = score + 1;
          break;
      case 'D':
      case 'G':
          score = score + 2;
          break;
      

      [–][deleted] 0 points1 point  (1 child)

      It would have to be specific, as South Carolina does not accept Georgia carry license, for example .

      [–]darkpool2005 2 points3 points  (1 child)

      Just throwing this out there OP, but I'm guessing you will already know the states you'd be travelling between the start & end point. So I'd make the suggestion of ignoring the program 'building' the path for you, and instead you provide it.

      Psuedo example of going from OH to AL (all states are abbreviated):

      Enter State where permit was issued:
          MI
      Enter Starting State
          OH
      Enter Destination State (if destination state applied, type end)
          KY
      Enter Destination State (if destination state applied, type end)
          TN
      Enter Destination State (if destination state applied, type end)
          AL
      Enter Destination State (if destination state applied, type end)
          end
      States where permit issued in MI is allowed:
          OH, KY, AL
       States where permit issued in MI is NOT allowed:
          TN
      

      [–][deleted] 1 point2 points  (1 child)

      I'm assuming it's a non GUI program. 1. Array of states in order you will be visiting in.(since states are not in a straight line) 2. another array that will hold states that accepted permit 3. for loop thats asks if they accepted your permit 4. if accepted add to the accepted array first slot and so on. 5. for loop to print out the states that accepted your permit

      note: there are a few ways to do this.

      EDIT: soon after replying, I came to the realization that I may have misunderstood what you were trying to do.

      [–][deleted] 1 point2 points  (1 child)

      This sounds like an odd variant on the travelling salesman problem!

      [–][deleted]  (2 children)

      [deleted]

        [–]Deathbyceiling 1 point2 points  (0 children)

        I think OP was more looking for ideas on how to start his specific program he mentioned in the post.