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.
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.
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.
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).
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.
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
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!
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
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.
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.
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.
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
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
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 06:35 PM.
: Beware of bugs in the above code - I have only proved it correct, not tried it.
Bookmarks