Help with class/applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Help with class/applet

  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
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here are the preliminaries:

    The code is on the brink of 100% functionality..... I just have to tweak
    the biotope parameters Currently the Deers wont die, or is it that the
    hunters are blind ? Hmm, obviously a bug that I must have daylight to hunt
    down.

    Anyway, I have to run now but check the attatched screenshot.
    I'll post the code tomorrow
    Attached Images Attached Images

  9. #9
    Join Date
    Mar 2005
    Posts
    27

    That looks Great! Thank You!

    The screen shot looks great. You are right about the deer not dying. I totally understand about the bug and about you needing daylight to work on it. You can take your time and I will wait until tomorrow. Once again, thank you very much for you help. You have really helped me so much and I really appreciate it much. I will be back tomorrow to check on it. Thanks!

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

    Ready to roll...

    Its 99.314% ready and here it is.

    The demo:
    http://home.no.net/sjalle/forestPage.html

    and/or download the attatched source. Be warned, coding something like
    this is not for the feeblehearted. The code if fairly well commented and
    rather object oriented..I dear say.

    The applet can run standalone (from the console) or as an applet.
    It takes 3 parameters: rows, cols and debug. The debug option will turn out
    quite an extensive output to the java console telling of each move, birth,
    death and "eat".

    If you run it from the console you do it like:
    java forest.Forest 10 10 0

    debug: 0=no debugging, 1=debug, 3=stepwise execution

    E.g. with debug=3 you "run" the forest by clicking the "Tick" button.

    The deers and hunters also have two bars on them, a blue and a white.
    The white indicates time left to live, the blue indicates time left before
    reproduction. They also get an id that is unique within the species. A plain
    grass square displays its grid coordinates.

    Buggies:
    1: Sometimes, when the forest gets overpopulated (by deers or hunters) the
    display doesn't always tell you that, the forest just stops.
    2: There is a little issue with the hunter; on rare occations he does not move. I know the bug is in the random picking of a free space when there
    are very few places left to move.
    3: Sometimes it signals that the deers are extinct, when there is one left.

    That is, as far as I can see, what is left to tweak.

    The assignment objective: "make forest live for 100 ticks" is not easy. I
    have managed 182 once, but usually its over somewhere between 50 and
    100 ticks.

    Check it out.
    Attached Files Attached Files
    Last edited by sjalle; 04-27-2005 at 02:01 PM.

  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.

  15. #15
    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.

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