Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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?
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
CANEHDN said:
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?

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;
}
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
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);
    }
  }
}
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
CANEHDN said:
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.

Yikes, each card is its own class? Was this your decision or the teacher's?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
rand0m3r said:
maybe he meant each card is an object.

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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
rand0m3r said:
maybe he meant each card is an object.

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.
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
CANEHDN said:
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.

I hope you're basing it on a single class and adding special behaviours from there.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
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.
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
CANEHDN said:
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.

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.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
savar said:
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.

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
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
SilentPanda said:
But to the OP... seriously... listen to what others have said about not making 168 class files.
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
CANEHDN said:
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.

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?
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
bousozoku said:
Then again, what project is more frequently maintained than a beginner's project?

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?"

:)
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
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
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
SilentPanda said:
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?"

:)

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.