Linked lists in Java

Discussion in 'Mac Programming' started by CANEHDN, Jul 6, 2006.

  1. macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #1
    I need help with some homework. I need to create a linked list of all file names in a directory that end with .class. Can anyone help?
     
  2. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #2
    Do you know what a linked list is? If you understand the concept, the code comes naturally. A linked of file names makes no sense to me, which makes me think you or your teacher doesn't know what they are. Maybe post more details?

    Off the top of my head:
    edit: insert code tags
    Code:
    class LinkedList {
      class Node {
        int data;
        Node next;
        Node(Node next) {
           this.next=next;
        }
        Node getNext() {
           return next;
        }
      }
    
      Node start;
    }
     
  3. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #3
    I have to create a linked list of all the file names in a directory ending in .class. For example: There is a directory with 5 files: 1.class, 2.class, through 5.class. I need to create a linked list that has each of those files names. I hope this makes sense. Thanks
     
  4. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #4
    This app takes the file path (where the class files are) as the first argument.

    This is about as reduced as it can be, mainly because it'll force you to pretty it up. I print the names just so you can verify that it works. There is no error checking, etc. - just a proof of concept. I assume you're using Java 5.

    I also assume you can use all of the Java classes, including LinkedList.

    Code:
    import java.io.*;
    import java.util.*;
    
    public class ClassFileLister
    {
      public static void main(String[] args)
      {
        for (String name : new LinkedList<String>(Arrays.asList((new File(args[0])).list(new FilenameFilter() {
                                                                                          public boolean accept(File directory, String name)
                                                                                          { return (name.endsWith(".class")); } }))))
        {
          System.out.println(name);
        }
      }
    }
    
    Edit: OK, if you're not sure how to use linked lists, that was probably too extreme. Here's a simplified version of exactly the same logic (again with no comments or error checking):
    Code:
    import java.io.*;
    import java.util.*;
    
    public class ClassFileLister
    {
      static class ClassFilenameFilter implements FilenameFilter
      {
        public boolean accept(File directory,
                              String name)
        {
          return (name.endsWith(".class"));
        }
      }
      public static void main(String[] args)
      {
        String   desiredPath       = args.length > 0 ? args[0] : ".";
        File     desiredPathFile   = new File(desiredPath);
        String[] classFileNameList = desiredPathFile.list(new ClassFilenameFilter());
        
        LinkedList<String> classFileList =  new LinkedList<String>(Arrays.asList(classFileNameList));
        
        for (String name : classFileList)
        {
          System.out.println(name);
        }
      }
    }
    
     
  5. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #5
    Freakin' awesome. Works great. Thanks a bunch.
     
  6. macrumors regular

    Joined:
    May 9, 2005
    Location:
    Boston, MA (I miss Willoughby, Ohio)
    #6
    I just hope you now know what linked lists really are. Especially if you are a CS major...
     
  7. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #7
    I know what they are. This is card game program we're writing and each card has to be written as it's own class. It's a pretty big project.
     
  8. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #8
    Yikes, each card is its own class? Was this your decision or the teacher's?
     
  9. macrumors regular

    Joined:
    Jun 30, 2006
    #9
    maybe he meant each card is an object.
     
  10. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #10
    I'd hope each one would be an object since it would make more sense to draw from one basic card class or four basic card suits.
     
  11. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #11
  12. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #12
    Yeah, but then the next sentence was, "It's a pretty big project." It certainly would be if you tried to write 52 classes just to data model a deck of playing cards.
     
  13. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #13
    Have you guys ever heard of Munchkin? We're writing a networking game of that. We decided to make each card it's own class because of the all the different things each card can do. We figured it would be easier to do it that way.
     
  14. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #14
    I hope you're basing it on a single class and adding special behaviours from there.
     
  15. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #15
    I own Munchkin and 3 or 4 expansions for it and also code in Java. If you're making each card its own class you're going about it in a terrible way. Set your classes up like the cards are set up... first make a treasure card class and a dungeon card class. Then bring those down into items, monsters etc... There are common properties between cards you just have to find them. Certain cards have a gold value, monsters have levels, etc... if you make a class for every card you might as well just scan them all in and have it display the images on your screen.
     
  16. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #16
    We will be scanning every card and having the images display. We will also have door cards and treasure cards. But in order to have full effects from each card the best way to do it is each card have it's own class.
     
  17. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #17
    Hey man, I guarantee you that its not the best way. I took a look at the game and perused the rules quickly. 168 cards!?! 168 classes is a *huge* application, especially for some intro CS students.

    The best way to do it would be to have a Card class, then subclass that to create the various types of Cards. Then I suggest that you define cards as data files which go into a particular directory, and your application reads them in and instantiates the right kind of Card subclass based on the data in the file. Each card can still have its own image and rules, etc. but this way you're removing the data from the application. What if you compile all 168 classes, then realize you made a typo -- one person in your group can't spell dungeon and you have to change 140 occurences of "dungoen" in 70 different classes, each one has to be recompiled. Do yourself a favor and put the game data into data files, not code files. It's a little more work on the front end but will save you hours of work on the back end and will pay for itself many times over. Not to mention it would knock the socks off of your teacher. You're working on a very ambitious project for a few novice developers, but if you nail the design you will have a very impressive program to show off at the end of it.

    Just keep in mind, your main goal should be to minimize redundancy as much as is reasonably possible. Don't have 168 different classes if they all have something in common.
     
  18. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #18
    168 cards doesn't even include the expansions if he ends up wanting those.

    But to the OP... seriously... listen to what others have said about not making 168 class files. If you spend even a day or two categorizing the cards and finding similar traits in them you will save weeks of coding. Heck one guy can put the various data bits into a data file while somebody else codes classes to interpret them.

    You can have (for instance) a method for you card class to force a player to lose X items of Y size and Z whatever. Every single card can have a gold value attribute (-1 for cards with no value). Every card can have a level (-1 for cards with no level). A method to remove levels from a given player. A method to lose X cards from a given player... it goes on and on. Then if you decide to add the expansion packs it will be a snap as most of the code will have already been written.

    Don't assign this batch of cards to this person and this other batch of cards to another person. Have somebody (or as a group) go through the cards and extract the logic and data. Then figure out what logic needs to be made into methods and with what parameters and separate those out to different people.

    Although I suppose it's your time and you're free to use it as you wish. :D
     
  19. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #19
    Our card class which all monsters will inherit from will have the similar traits such as level ups and downs but when it comes to the special things that a lot of cards have you have to do something else. This also makes it easier when adding new cards such as the expansions. You won't have to go and change code already written.
     
  20. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #20
    Agreed. A single "Card" interface (to be implemented a few times) and a good set of data will be much cleaner and much more educational than brute-forcing 160+ classes.
     
  21. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #21
    As this is probably a one-off project and you'll never touch the code again, I can understand that you don't want to think too hard about it.

    However, as many of us have learnt in many years, it's much easier to maintain code that has been properly designed. I believe that a lot of people end up re-working their base classes after a while because they find that they're consistently adding one attribute or another to almost all of their individual uses of it.

    Then again, what project is more frequently maintained than a beginner's project?
     
  22. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #22
    There's nothing more fun than looking at code you wrote "in your early years" and remembering... "it took me 3 weeks to read in a text file?"

    :)
     
  23. thread starter macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #23
    You're right on the fact that I'll never touch it again. But the "Captain" of the group probably will. He is the ultimate nerd and is thrilled to death about this project. Once this class is over, I'll post the final project, if it's complete. I'd like to get people's opinions on it. I do appreciate all the ideas and suggestions though.

    Also we won't be adding every monster in the game just a few to make sure the code works. There wouldn't be enough time to do that. Even if we did everything in a card class there would be too much code to write with the amount of time we have.

    I do have to write a shuffle() function for a linked list. Any suggestions on the best way to do that? The reason I'm asking the stuff on linked lists is I was never too good at the data structures stuff. Arrays I'm ok with. Any ideas would be great. Thanks
     
  24. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #24
    I remember waiting for the train and hearing students complaining about writing 500 lines of COBOL code and thinking to myself "what can you do with only 500 lines of COBOL?" :D

    At the time, I was porting a screen design application built in C to another platform and it only had one comment in any of the files and it was completely useless as it only contained the function name of the function it preceded.

    Poor design = waste of effort later.
     
  25. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #25
    Not to mention that it's just offensively ugly. Good design, though, is beautiful.
     

Share This Page