Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 6, 2006, 02:26 PM   #1
CANEHDN
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, UT
Linked lists in Java

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?
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 6, 2006, 02:54 PM   #2
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by CANEHDN
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;
}
__________________
Mehce
savar is offline   0 Reply With Quote
Old Jul 6, 2006, 03:02 PM   #3
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 6, 2006, 03:53 PM   #4
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: 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);
    }
  }
}
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)

Last edited by jsw; Jul 6, 2006 at 04:02 PM.
jsw is offline   0 Reply With Quote
Old Jul 6, 2006, 04:20 PM   #5
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, UT
Freakin' awesome. Works great. Thanks a bunch.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 7, 2006, 12:15 AM   #6
aquanutz
macrumors regular
 
Join Date: May 2005
Location: Boston, MA (I miss Willoughby, Ohio)
I just hope you now know what linked lists really are. Especially if you are a CS major...
aquanutz is offline   0 Reply With Quote
Old Jul 7, 2006, 09:45 AM   #7
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 7, 2006, 11:14 AM   #8
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by CANEHDN
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?
__________________
Mehce
savar is offline   0 Reply With Quote
Old Jul 8, 2006, 09:44 AM   #9
rand0m3r
macrumors regular
 
Join Date: Jun 2006
maybe he meant each card is an object.
rand0m3r is offline   0 Reply With Quote
Old Jul 8, 2006, 10:14 AM   #10
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by rand0m3r
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.
bousozoku is offline   0 Reply With Quote
Old Jul 8, 2006, 11:35 AM   #11
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
Speaking of playing cards and Java....

http://java.sun.com/j2se/1.5.0/docs/...age/enums.html
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Jul 8, 2006, 03:26 PM   #12
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by rand0m3r
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.
__________________
Mehce
savar is offline   0 Reply With Quote
Old Jul 10, 2006, 04:13 PM   #13
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 10, 2006, 04:48 PM   #14
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by CANEHDN
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.
bousozoku is offline   0 Reply With Quote
Old Jul 10, 2006, 05:13 PM   #15
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: 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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Jul 10, 2006, 07:00 PM   #16
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 10, 2006, 10:55 PM   #17
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by CANEHDN
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.
__________________
Mehce
savar is offline   0 Reply With Quote
Old Jul 10, 2006, 11:12 PM   #18
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
Quote:
Originally Posted by savar
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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Jul 11, 2006, 10:05 AM   #19
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Jul 11, 2006, 10:33 AM   #20
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Quote:
Originally Posted by SilentPanda
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.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Jul 11, 2006, 10:44 AM   #21
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by CANEHDN
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?
bousozoku is offline   0 Reply With Quote
Old Jul 11, 2006, 10:46 AM   #22
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
Quote:
Originally Posted by bousozoku
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?"

__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Jul 11, 2006, 01:16 PM   #23
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, 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
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!

Last edited by CANEHDN; Jul 11, 2006 at 01:31 PM.
CANEHDN is offline   0 Reply With Quote
Old Jul 11, 2006, 01:28 PM   #24
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by SilentPanda
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?"

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.
bousozoku is offline   0 Reply With Quote
Old Jul 11, 2006, 01:58 PM   #25
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
Quote:
Originally Posted by bousozoku
Poor design = waste of effort later.
Not to mention that it's just offensively ugly. Good design, though, is beautiful.
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Overloading the + operator for linked lists(binary numbers) computerexpert Mac Programming 22 Mar 1, 2013 09:56 PM
why sometimes 'save linked file' and sometimes 'download linked file'? lloyd709 OS X 10.8 Mountain Lion 1 Jan 18, 2013 03:23 AM

Forum Jump

All times are GMT -5. The time now is 01:27 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC