Help with class/applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 18

Thread: Help with class/applet

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    27

    Help with class/applet

    Hi, everyone! I was wondering if anyone can assist me with a problem that I have with my program. I have posted this problem before and took it off because I thought I finally got it. I did not realize however, that my main struggle was with the move() methods of the Deer and Hunter. I also did not realize that the program had to be an applet. I am really not familiar with java classes or applets that much. If anyone can assist me with the methods that I mentioned and the applet itself I would appreciate it. I have posted the document which has the requirements for the program and I have also posted what I have done so far(java file) on the zip file. I think I am pretty much done except for the two methods that I said earlier and the applet. Once again, I would appreciate it if anyone can help me with this. Thank you.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you can wait till tomorrow night (24 hrs) I will check this for you.

  3. #3
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100

    Hello again

    Hehe, didn't I say sjalle had something for you? Just a little patience...

    BTW: have you noticed that this thread where you originally posted your problem and where you took it off has become the thread with the second most views, ie >1300?
    So you know what I think public forums should be like AND can be good for + that completely deleting posts needs a VERY good reason...
    : Beware of bugs in the above code - I have only proved it correct, not tried it.

  4. #4
    Join Date
    Mar 2005
    Posts
    27

    Thank You! I will wait.

    OK, I really appreciate your help sjalle. I will definately wait the 24 hours. Once again thanks for the help.
    Last edited by none_none; 04-25-2005 at 08:10 PM.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Uhhh,...just for the record meisl..

    completely deleting posts needs a VERY good reason
    Till now, in my so far short career as moderator, I have never deleted any posts.
    Here in Norway we have no problems with four letter words, not even
    ten-letters, so anyone who curses/swears in desperation will be spared.
    Flamers however, will get flushed, along with those that invite to immoral activity,
    like cracking licenced software.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I have a few objections to the assignment...

    The specs are ok except for one vital detail, the hierachy restrictions.

    I'll summarize:
    We have a forest (grid) with creatures (grass, tree, deer, hunter) that
    inhabitate the cells of the grid. The creatures can be subdivided into two
    groups: fixed and moving creatures.

    For me the natural approach is to regard the grid as the "world" (applet) and
    then create a superclass:

    Square: has only position and dimension, and possibly an inhabitant.
    A bare implementation of this one is the Grass extension, but not very useful
    (its a copy really) so instances of Square is "grass". The thing is that
    the inhabitant, as an extension of Square, cannot use the Square's positional
    data, - hunters or deers dont drag turfs of grass around....

    Tree: a Square extension cannot be moved to (no Squirrels yet)

    And here is my twist:

    MovingCreature: an abstract Square extension, abstract methods
    are move() and doAfterMoveAction(). MovingCreatures are alive or dead,
    they can move (if alive), they have a time to reproduce and they die
    according to specfic rules.

    Deer: a MovingCreature extension, does not eat hunters, doesn't bother
    eating (logically). Dies of age if not eaten.
    Hunter: a MovingCreature extension, eats Deers, must feed or die

    The reason for my approach is this: we have a container of objects, and it will
    complicate things if this container (forest) needs to know detailed info
    about its creatures. Its should really just tell the moveable creatures to
    "do their thing" at each tick. These "things" are moving and creature-
    specific actions, like eat, die, feed, reproduce. I have put these actions
    into the doAfterMoveAction() method of Deers and Hunters.

    Using this approach the forest doesn't need to "know" anything more than
    that a creature is a MovingCreature and then tell it to move and do its
    specifics afterwards (doAfterMoveAction).

    This may seem like an overkill, but it really isn't, try the other (assignment)
    approach and I can guarantee that the code will be bloated in ceratin
    areas and far less readable.

    If we foresee an "enhanced" Forest with lions, cannibals,disease ridden
    working girls (that won't plague the gay Hunters) plus some foot&mouth
    viruses floating around, - then an abstract approach is a must.

    I hope this is OK, I havent totally rehashed the assignment specs, all I have
    done is inserting the MovingCreature abstract class between the Square
    class and the Deers and Hunters, they are still subclasses of Square (and
    MovingCreature).
    Last edited by sjalle; 04-26-2005 at 10:11 AM.

  7. #7
    Join Date
    Mar 2005
    Posts
    27

    Thank you!

    I totally agree on what you are doing. It is definately ok and it would definately inprove the program structure and its usability. Thanks for speding the time and assisting me with my program. You can take your time in looking at all of the aspects and I will wait to see what you came up with once you are finished. Thanks once more for the help. I really appreciate it. Let me know when you get further on it. I just cannot picture in my mind on how the applet will run and make all of the objects on the screen move. Hope to hear from you. Talk to you then.

  8. #8
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    Quote Originally Posted by sjalle
    Till now, in my so far short career as moderator, I have never deleted any posts.
    Here in Norway we have no problems with four letter words, not even
    ten-letters, so anyone who curses/swears in desperation will be spared.
    Flamers however, will get flushed, along with those that invite to immoral activity,
    like cracking licenced software.
    Ah no, sjalle, nothing that serious. I was just referring to none_none's deleting his own post in the thread now-known as "the minesweeper thread" ('the thread formerly known as "Help with Java program!"' = TTFKAHWJP, haha). You know, none_none and me, we've been having a little private conversation and in that I had emphasized that a public forum is at first - public.
    Last edited by meisl; 04-27-2005 at 07:35 PM.
    : Beware of bugs in the above code - I have only proved it correct, not tried it.

  9. #9
    Join Date
    Mar 2005
    Posts
    27

    Thank you so much!

    Hello, once more. Once again, just like before, I really appreciate the help that you have given me. You will be rewarded in the future and I know that for sure. Well, I tried to see the link that you posted but it did not show anything and therefore I looked at the code. I did what you told me and it did work. I have never compiled my programs like that so I was wondering if you could explain why it should be that way. I was wondering about why you should compile the folder name and then the Forest file(\javac forest\Forest.java). I know you said because of the package that you place at the beginning of the code but I am still not understanding that process. Is the forest package one that comes integrated in java or did you create that one yourself? Just wondering I am not sure if these questions are good or not but like I said, I have never dealt with something like this. Well, if you get the chance to answer these questions for me the I would appreciate it. Thanks once more for the help. I really appreciate it much. Talk to you later.

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Your pc/browser is not set up for swing applets.

    I have set it up as an awt version also.

    check here:
    http://home.no.net/sjalle/demos/tlist.html

    Note: the awt version uses forest1 as its package

    If you want to make a move into the modern world and use swing
    (and above) then do the following (for IE):

    Tools->Internet Options->advanced.
    There you must check the option:
    Java (sun) -> use JRE 1.5.0 (something) .
    If you can't find that option you must download the JDK from sun and install
    it.

    As for the package/folder issue, check out the sun documentation for this.

    Good luck !
    Last edited by sjalle; 04-28-2005 at 05:46 AM.

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Oh.. and i seem to have switched the hunters and deers lifespan in the initialization,
    just switch it back again.

    Btw; Im doing this not only because i like to help people out,... I am building
    myself an online library of java how-to examples and I will incorporate this
    app/applet when its totally bug free and using images.
    Last edited by sjalle; 04-27-2005 at 02:15 PM.

  12. #12
    Join Date
    Mar 2005
    Posts
    27

    Ran the program

    First and foremost, I really appreciate the help that you have given me. I looked over the program and tried to compile it but it did not compile. I have jcreator installed on my computer and ran it from there. I also ran it from the command line and this is what it gave me back on both (jcreator and command line):

    D:\Final_Deer_Hunter\Forest.java:37: cannot resolve symbol
    symbol : class Square
    location: class forest.Forest
    private Square [][] squares=null;
    ^
    D:\Final_Deer_Hunter\Forest.java:43: cannot resolve symbol
    symbol : class ForestPanel
    location: class forest.Forest
    private ForestPanel forestPanel = null;
    ^
    D:\Final_Deer_Hunter\Forest.java:214: cannot resolve symbol
    symbol : class Square
    location: class forest.Forest
    public Square [][] getSquares() { return squares; }
    ^
    D:\Final_Deer_Hunter\Forest.java:332: cannot resolve symbol
    symbol : class Creature
    location: class forest.Forest
    public void putForestCreature(Creature aCreature) throws Exception {
    ^
    D:\Final_Deer_Hunter\Forest.java:156: cannot resolve symbol
    symbol : class ForestPanel
    location: class forest.Forest
    forestPanel=new ForestPanel(this);
    ^
    D:\Final_Deer_Hunter\Forest.java:313: cannot resolve symbol
    symbol : class Hunter
    location: class forest.Forest
    Hunter aHunter=new Hunter(this, hunterTimeToReproduce,
    ^
    D:\Final_Deer_Hunter\Forest.java:313: cannot resolve symbol
    symbol : class Hunter
    location: class forest.Forest
    Hunter aHunter=new Hunter(this, hunterTimeToReproduce,
    ^
    D:\Final_Deer_Hunter\Forest.java:314: cannot resolve symbol
    symbol : class GridPosition
    location: class forest.Forest
    new GridPosition(), hunterTimeToLive);
    ^
    D:\Final_Deer_Hunter\Forest.java:318: cannot resolve symbol
    symbol : class Deer
    location: class forest.Forest
    Deer aDeer=new Deer(this, deerTimeToReproduce,
    ^
    D:\Final_Deer_Hunter\Forest.java:318: cannot resolve symbol
    symbol : class Deer
    location: class forest.Forest
    Deer aDeer=new Deer(this, deerTimeToReproduce,
    ^
    D:\Final_Deer_Hunter\Forest.java:319: cannot resolve symbol
    symbol : class GridPosition
    location: class forest.Forest
    new GridPosition(),
    ^
    D:\Final_Deer_Hunter\Forest.java:324: cannot resolve symbol
    symbol : class Tree
    location: class forest.Forest
    Tree aTree=new Tree(this, new GridPosition());
    ^
    D:\Final_Deer_Hunter\Forest.java:324: cannot resolve symbol
    symbol : class Tree
    location: class forest.Forest
    Tree aTree=new Tree(this, new GridPosition());
    ^
    D:\Final_Deer_Hunter\Forest.java:324: cannot resolve symbol
    symbol : class GridPosition
    location: class forest.Forest
    Tree aTree=new Tree(this, new GridPosition());
    ^
    D:\Final_Deer_Hunter\Forest.java:340: package Square does not exist
    aCreature.setBounds(row,col,Square.DIM.width, Square.DIM.height);
    ^
    D:\Final_Deer_Hunter\Forest.java:340: package Square does not exist
    aCreature.setBounds(row,col,Square.DIM.width, Square.DIM.height);
    ^


    The system is out of resources.
    Consult the following stack trace for details.
    java.lang.OutOfMemoryError

    Process completed.



    From what you told me I know that you ran it successfully. Where did you run it? Do you know why it's giving me these errors? I have never seen the message at the end of these errors (The system is out of memory ... ) .Hope to hear from you soon. Thanks.

  13. #13
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    It runs allright, did you try the applet, the lin ki s in my post.

    I guess this has to to with the folder you are keeping the source files in. The
    source I posted has the package "forest" specified on top of the code. You should
    make a folder of that name to compile them in

    check this console output, it should give you the general idea
    Code:
    C:\java\JBuilder8\dev\forest\src>dir forest\
     Volume in drive C has no label.
     Volume Serial Number is 0C79-EC15
    
     Directory of C:\java\JBuilder8\dev\forest\src\forest
    
    28.04.2005  00:00       <DIR>          .
    28.04.2005  00:00       <DIR>          ..
    27.04.2005  18:05                  417 Creature.java
    27.04.2005  18:08                1*991 Deer.java
    21.04.2005  11:40               23*552 deer_hunter_problem.doc
    27.04.2005  22:59               20*586 Forest.java
    27.04.2005  20:33               20*863 Forest.java~1~
    27.04.2005  23:46               18*694 forest.zip
    27.04.2005  22:06                3*194 ForestPanel.java
    27.04.2005  18:08                  646 GridPosition.java
    27.04.2005  18:08                3*047 Hunter.java
    27.04.2005  18:08                2*157 MovingCreature.java
    27.04.2005  18:08                6*192 Square.java
    27.04.2005  18:08                  397 Tree.java
    27.04.2005  19:08                  459 Virus.java
    27.04.2005  23:47                  112 WS_FTP.LOG
                  14 File(s)        102*307 bytes
                   2 Dir(s)  27*004*391*424 bytes free
    
    C:\java\JBuilder8\dev\forest\src>C:\java\j2sdk1.4.1_01\bin\javac forest\Forest.java
    
    C:\java\JBuilder8\dev\forest\src>java forest.Forest 10 10 0
    Setup 10X10

  14. #14
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    PS: for the "OutOfBrains" -exception, I get that sometimes too in JBuilder when I have
    been crashing around for while.
    AND the Virus -class in my list is not a data virus. It is a little
    contagious thing I was playing with in my program, it will work like :
    virus class #1-5: reduces life expectancy by 10 to 50 %. and has a
    probability of 10 to 50% for infecting neighbours
    virus class #6-10: reduces life expectancy by 10 to 50 % and will infect all
    neighbours of same species. Also - it is carried on to the offspring.

    When I have my images ready this will look really nasty
    Last edited by sjalle; 04-27-2005 at 07:19 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center