14 Replies - 797 Views - Last Post: 06 June 2010 - 09:21 PM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Packaging Classes

Posted 04 June 2010 - 11:34 PM

Sorry for posting so many new topics. I am making my first game and am in need of guidance left and right as I get more into the mechanics of the game itself.

This on the other hand, is more of an organizational help request.

So my game involves 151 separate classes of Pokemon, each having their own point value, move-sets, and difficulty level to catch. Thinking about this, I figured it would be easiest to package all the Pokemon classes, along with all their related necessary files up and then just do imports instead of blustering through 160+ classes on my screen. It just seems a little excessive and overwhelming how I have it right now.

Now I have tried making a package, but failed miserably. I read manuals online and tried but readily understood nothing of what they were talking about. I have seen it done as folders of files (which is how I prefer to do it if possible) and also done as jar files in a +libs folder, though the classes were still packaged.

I know this isn't rocket science, but could someone please explain this to me so I can understand it?

Is This A Good Question/Topic? 0
  • +

Replies To: Packaging Classes

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Packaging Classes

Posted 04 June 2010 - 11:49 PM

First, I would note that 151 classes of different Pokemon is not very good design.
I mean, Imagine a RPG game for example, where you have tons of items\spells\monsters\levels and far more...
If you'll try to create a class for each individual type of the above, you'll end up with tons of classes, leaving you unable to manage your code, and compile time of couple of hours...
also, each minor change in your code for something as small as Pokemon state, will require you to compile the whole code again! (another couple of hours of waiting...)

A better approach is to create a single Pokemon class, and retrieve the data for each new Pokemon via txt file or database.

Anyways, what is it exactly you find hard at creating a package?

inside your source folder, you can create a new folder (with the name of your package, and create (or copy paste) classes inside it.
you will have to update all source files you move there with this line (as the very first line of the code):

for example, you create package mypackage:
package mypackage;

Was This Post Helpful? 0
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 05 June 2010 - 06:56 AM

So a text files would be what?

I would have to have a getImage method, along with getName, getScore, and all other assorted getters? Then just use a scanner to isolate data for each pokemon in a loop or something? Yes?

Also, if I need images packaged classes, do I need to include the resources within the package?
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Packaging Classes

Posted 05 June 2010 - 07:16 AM

What IDE are you working with?
For example, in eclipse, if you want to add images, you press:
src -> new -> folder -> (name the folder)
then just copy paste the images to that folder.
You then can retireve the images using:
getClass().getResource("path/imageName");


as for:

Quote

I would have to have a getImage method, along with getName, getScore, and all other assorted getters? Then just use a scanner to isolate data for each pokemon in a loop or something? Yes?


your txt file can look like (line for each pokemon):
name1, description1, attack1, hp1, etc...
name2, description2, attack2, hp2, etc...

then the Pokemon class would be:

public class Pokemon {
   //members..
   public Pokemon(String name, String description, int attack){
      //assign values..
   } 

   //get\ set methods...
   //other methods...
}


you can load your Pokemon Objects from the txt to a HashMap, where the key would be a unique id\code for each pokemon,
whenever you want to retieve a Pokemon you can use the get method of the HashMap.
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 05 June 2010 - 02:29 PM

Could I also load them into an ArrayList as well? And then just access them by using a getName() method?

Each one would be uniquely defined that way then, correct?

Plus I need the getName() method for later in the battle sequence.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: Packaging Classes

Posted 05 June 2010 - 02:33 PM

Yeah why not?

ArrayList<Pokemon>;



Or access whatever you want from each object.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Packaging Classes

Posted 05 June 2010 - 02:34 PM

View Postzim1985, on 05 June 2010 - 03:29 PM, said:

Could I also load them into an ArrayList as well? And then just access them by using a getName() method?

Each one would be uniquely defined that way then, correct?

Plus I need the getName() method for later in the battle sequence.

Yes. You can even save the ArrayList to a file and read it back to continue a game later.
You can also have an ArrayList beginGame that you can read when you start a new game
Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Packaging Classes

Posted 05 June 2010 - 02:51 PM

Quote

Yes. You can even save the ArrayList to a file and read it back to continue a game later.


Might wanna check out my Object Serialization tutorial for that.
Was This Post Helpful? 1
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 05 June 2010 - 10:15 PM

Instead of making a completely new thread, I'll just post it here. I am getting some run-time errors in my PokemonList class and I need help finding my errors. Maybe I could get some tips on construction of my game as well, but right now I just want to get it up and running again (it's not working since I implemented the textfile workaround for the classes of pokemon). I've been trying to work these issues for the better part or 2 hours since I implemented the stuff.

I'll attach the zipped file of my project below. All the library files are also included in there, though I use a jarred version in the game.

All help and tips are much appreciated, but keep in mind, the game isn't amazing being that it is my first attempt at one thus far in my programming career, along with the fact that it's still incomplete. I'm just looking to finalize the first part by the end of the weekend and then the next by next weekend.

Attached File(s)


This post has been edited by zim1985: 05 June 2010 - 10:17 PM

Was This Post Helpful? -1
  • +
  • -

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Packaging Classes

Posted 06 June 2010 - 04:29 PM

Better if you could post the relevant classes where you get those errors, or problems, along with the error messages as well.
Was This Post Helpful? 0
  • +
  • -

#11 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 06 June 2010 - 05:48 PM

Ok, so the NullPointerException


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import java.io.File;
import java.io.FileNotFoundException;

import gameFiles.actor.Pokemon;
import gameFiles.util.Stage;

public class PokemonList
{
    private ArrayList<Pokemon> pkmn = new ArrayList<Pokemon>();
    private Random ranGen = new Random();
    private Scanner sc;
    
    public PokemonList(Stage stage)
    {
        try
        {
            sc = new Scanner(loadFile("Pokemon.txt"));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File not found!");
        }
        
        while(sc.hasNextLine())
        {
            pkmn.add(new Pokemon(stage, sc.next(), sc.next(), sc.nextInt(), sc.nextInt(), sc.nextBoolean()));
            sc.nextLine();
        }
    }
    
    /**
     * Gets a random Pokemon from the available Pokemon.
     * @return A random Pokemon.
     */
    public Pokemon getPokemon()
    {
        return pkmn.get(ranGen.nextInt(pkmn.size()));
    }
    
    /**
     * Gets a specific Pokemon from the available Pokemon.
     * @param i The number Pokemon in the encapsulated list of Pokemon to be gotten.
     * @return The Pokemon in the encapsulated list as indicated by the value of the int parameter.
     */
    public Pokemon selectPokemon(int i)
    {
        return pkmn.get(i);
    }
    
    /**
     * Removes a Pokemon from the available Pokemon.
     * @param name The name of the Pokemon to be found and removed.
     */
    public void removePokemon(String name)
    {
        for(int i = 0; i < pkmn.size(); i++)
        {
            if(pkmn.get(i).getName().equalsIgnoreCase(name))
                pkmn.remove(i);
        }
    }
    
    /**
     * Encapsulated list size getter.
     * @return The length of the encapsulated list.
     */
    public int getSize()
    {
        return pkmn.size();
    }
    
    public File loadFile(String name)
    {
        return new File("resources/files/"+name);
    }
}

java.lang.NullPointerException
	at PokemonList.<init>(PokemonList.java:28)
	at PokemonBananza.<init>(PokemonBananza.java:46)
	at PokemonBananza.main(PokemonBananza.java:321)



The first one is the one I'm worried about, the last two just deal with the object not being able to compile, so it messes up the class that calls it. (I think.)

My other error is the file I'm calling not being found which, I think, has to do with where it is located in my project file, though I can't necessarily post that here, which is why I initially attached a above in my post.

This post has been edited by zim1985: 06 June 2010 - 05:56 PM

Was This Post Helpful? 0
  • +
  • -

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Packaging Classes

Posted 06 June 2010 - 07:52 PM

If the File wasn't found, The Scanner Object wasn't instaniated correctly and remains null.
Then when you attempt to call method from a null Object as here:
while(sc.hasNextLine()) 

you'll get the NullPointerException.

I am not sure that this is the problem, but here:
return new File("resources/files/"+name); 

you should add "/" before the "resources, like:
return new File("/resources/files/"+name); 


Again, I am not sure it will solve the problem, because perhaps you are not adding the file resource to your project correctly..

This post has been edited by japanir: 06 June 2010 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#13 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 06 June 2010 - 08:30 PM

I didn't have to do that with the images I loaded...

Maybe it's because in my library files the resource folder is also called "resource". Would that cause problems?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Packaging Classes

Posted 06 June 2010 - 08:38 PM

View Postzim1985, on 06 June 2010 - 09:30 PM, said:

I didn't have to do that with the images I loaded...

Maybe it's because in my library files the resource folder is also called "resource". Would that cause problems?

No
Was This Post Helpful? 0
  • +
  • -

#15 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Packaging Classes

Posted 06 June 2010 - 09:21 PM

Ok then I don't know why my file isn't loading. I checked the file path and it checks out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1