Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default fish constructor

    hello,
    i have to ceate a constructor named fish that randomly initializes species to “shark”, “gold fish”, or “cat fish”, randomly initializes size and speed to a number between 1 and 10 inclusive, sets condition to true and randomly sets type to “herbivore” or “carnivore”.

    these r my instance variables:

    String species // indicates the name of the fish species (shark, gold fish,…)
    int size // size should be between 1 and 10 inclusive.
    int speed // speed should be between 1 and 10 inclusive.
    boolean condition // to indicate whether the fish is dead (false) or alive (true)
    String type // to indicate whether the fish is “herbivore” or “carnivore”

    any idea what should i read from the java docs, i though of reading the object class but i dont think i found what i want

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    what is the problem you faced?

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    at first i though that maybe i should store the shark, gold fish, and cat fish in something that i will then call it later to put them randomly to any new fish object created, if that is what im supposed to do what should my first step be?

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by aizen92 View Post
    at first i though that maybe i should store the shark, gold fish, and cat fish in something that i will then call it later to put them randomly to any new fish object created, if that is what im supposed to do what should my first step be?

    first learn how classes are created. for your problem i would create one class and each time you instantiate it all random members will be initialized.

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    i know how to create classes and everything, all what im asking is, is there like something that i should store the vlaues i want in it, that will allow me to call them later???

    like i store shark, cat fish, ... somehwere and when i want to set value to the fish constructor to take a random from the place where i saved the values before in?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    I dislike the idea of implementing different kind of fish as one single class; imho it should be modelled as follows:

    Java Code:
    public abstract class Fish { ... }
    public class Shark extends Fish { ... }
    public class GoldFish extends Fish { ... }
    That way we don't stand the risk of creating giant gold fish that are carnivorous. Creating a random fish will be just selecting among a Shark, GoldFish etc. I don't know if this is in the spirit of the exercise though ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I have the following uml to be done

    i did everythibng in this uml except the fish where it needs random

    heres the uml :

    @JosAH: I dont think they have to be like in corespondance with reality, it can be anything, from the random, so it wouldnt matter if it was a big golden carnivorus fish

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    @JosAH: I dont think they have to be like in corespondance with reality, it can be anything, from the random, so it wouldnt matter if it was a big golden carnivorus fish
    Ok, so what's your problem? I can almost see the code shine through that uml class description ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    my problem here, is how can i make the species for the Fish constuctor to be chosen at random?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by aizen92 View Post
    my problem here, is how can i make the species for the Fish constuctor to be chosen at random?
    In the Fish constructor use a Random object and randomly select a number between 0 and the size of the species String array. Random has a method, nextInt(...) that will help you out here.

  11. #11
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    but the random object only give int, i want it to return one of the three things, shark, gold fish, cat fish?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by aizen92 View Post
    but the random object only give int, i want it to return one of the three things, shark, gold fish, cat fish?
    Which are held in an array. Is there a way of using an int to get an item out of an array?

  13. #13
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok, then if i put the species in an array, still the random gives me number from 0 to 1, how can i make ti to five a number from 0 to array.length - 1 ???? so that the number it returns with be the index of the random species ....

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    ok, then if i put the species in an array, still the random gives me number from 0 to 1, how can i make ti to five a number from 0 to array.length - 1 ???? so that the number it returns with be the index of the random species ....
    Read the API documentation for the Random class. If you want to select a random String, put them in an array and select a random int in the range of the size of the array. A little imagination could've told you that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by JosAH View Post
    I dislike the idea of implementing different kind of fish as one single class; imho it should be modelled as follows:

    Java Code:
    public abstract class Fish { ... }
    public class Shark extends Fish { ... }
    public class GoldFish extends Fish { ... }
    That way we don't stand the risk of creating giant gold fish that are carnivorous. Creating a random fish will be just selecting among a Shark, GoldFish etc. I don't know if this is in the spirit of the exercise though ...

    kind regards,

    Jos
    As with all things it depends what you plan on using it for. A simgle Fish class is fine for a fish-shop catalogue for example.

    Anyway, don't you dare prevent me from achieving my dream of creating man-eating giant goldfish!

    Bwah-ha-ha-HAAA!
    :p

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    As with all things it depends what you plan on using it for. A simgle Fish class is fine for a fish-shop catalogue for example.

    Anyway, don't you dare prevent me from achieving my dream of creating man-eating giant goldfish!

    Bwah-ha-ha-HAAA!
    :p
    Oh dear, the man has gone completely insane, lost his mind, gone totally gaga; I'm sorry Sir, I'm afraid we'll have to shoot you now; please stand back and close your eyes ;-)

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  18. #18
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Read the API documentation for the Random class. If you want to select a random String, put them in an array and select a random int in the range of the size of the array. A little imagination could've told you that.

    kind regards,

    Jos
    There's a random class, i didnt see that, lol ill read it and see what it will do to my code, thnx for the hint :P

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by JosAH View Post
    Oh dear, the man has gone completely insane, lost his mind, gone totally gaga; I'm sorry Sir, I'm afraid we'll have to shoot you now; please stand back and close your eyes ;-)

    kindest regards,

    Jos
    Oh well, it was fun while it lasted.

  20. #20
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok, here is my attempt, plz can u tell me if this is correct or what ...

    Java Code:
    public Fish ()
        {
            Random r = new Random ();
            int rand = r.nextInt();
            
            String [] species = {"shark", "gold fish", "cat fish"};
            int [] size = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int [] speed = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            String [] type = {"herbivore", "carnivore"};
            
            if ( rand > 0 && rand <= 3 )
            this.species = species[rand];
            
            if ( rand > 0 && rand <= 10 )
            this.size = size[rand];
            
            if ( rand > 0 && rand <= 10 )
            this.speed = speed[rand];
            
            if ( rand > 0 && rand <= 2 )
            this.type = type[rand];
        }

    EDIT: Okay, i did some debugging here, and found out that if the number generated is not between the intervals above it will keep the fish constructor empty, how can i avoid that?

    EDIT2: ok, i did it finally, heres the code to it

    Java Code:
    public Fish ()
        {
            Random r = new Random ();
            
            String [] species = {"shark", "gold fish", "cat fish"};
            int [] size = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int [] speed = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            String [] type = {"herbivore", "carnivore"};
            
            int rand1 = r.nextInt(species.length);
            this.species = species[rand1];
            
            int rand2 = r.nextInt(size.length);
            this.size = size[rand2];
            
            int rand3 = r.nextInt(speed.length);
            this.speed = speed[rand3];
            
            int rand4 = r.nextInt(type.length);
            this.type = type[rand4];
        }
    Last edited by aizen92; 01-22-2011 at 05:54 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Constructor
    By arefeh in forum New To Java
    Replies: 9
    Last Post: 01-11-2010, 11:38 AM
  2. Fish animation
    By SwEeTAcTioN in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2009, 10:56 AM
  3. Fish animation
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 06:23 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •