Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: Arrays. Animal.java (extended version)

  1. #1
    Bkmz8888 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Arrays. Animal.java (extended version)

    Hello, I learn Java, the second week. Tell me who does what he can. I created a class: Animal.java,Pet.java, Fish.java, Cat.java, Spider.java.
    Next on the instructions to the class, TestAnimal.java create an array zoo type Animal 10 elements, and fill the array randomly different animals (use Match.Random). Thus determine the type of animal tracks and assign the next element. Then create a second array of domestic animals and pets such as Pet in another cycle go on all elements of zoo and identify which animals are suitable for them you can keep the house. If appropriate, enter it into an array of pets.


    TestAnimal.java

    public class TestAnimals {
    public static void main(String[] args) {
    Fish f = new Fish();
    Cat c = new Cat("Fluffy");
    Animal a = new Fish();
    Animal e = new Spider();
    Pet p = new Cat();

    f.play();
    a.walk();
    c.play();

    e.eat();
    e.walk();

    p.setName("Tom");
    System.out.println(p.getName());
    p.play();

    }
    }

  2. #2
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Arrays. Animal.java (extended version)

    Can you please elaborate your question . Step vise approach for: what you have done? and what you want to acheive? will be more helpful..

  3. #3
    Bkmz8888 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Arrays. Animal.java (extended version)

    After the first array. I do not understand how to use the "Random", randomly determine the type of animal tracks and assign the type of the next element
    Animal [] zoo = new Animal [10];
    zoo = (Animal[]) (Math.random()*10)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Arrays. Animal.java (extended version)

    Look at using the random number to chose an animal. Assign an animal for each number from 0-9.
    When the random number generator returns a number, that number represents a type of animal to be added to the zoo.
    A switch statement would be useful here. Its case statements would be where you create and add an animal to the zoo.
    Bkmz8888 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Arrays. Animal.java (extended version)

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Bkmz8888 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Arrays. Animal.java (extended version)

    I got the following:
    Animal [] zoo = {
    new Poni ("aleks"),
    new Fish(),
    new Cat(),
    new Spider(),

    Animal petChoice;
    Random select = new Random();
    for(int i = 0; i < 5; i++){
    petChoice = zoo[select.nextInt(zoo.length)];
    };
    Pet [] pets = {
    new Dogs (),
    new Cat ()
    };

    Now I need to use a loop to traverse an array zoo, pick out pets and enter them into an array pets. Please tell me how this can be implemented? Known in advance that pets are Poni, Fish.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Arrays. Animal.java (extended version)

    Please go through the links I posted. If you continue to post unformatted code, the thread may be closed and you may be banned for a period.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Animal Shelter Manager 2.8.9
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-29-2012, 06:53 PM
  2. Animal Vegetable Mineral Game
    By vuzuggu in forum New To Java
    Replies: 10
    Last Post: 12-06-2011, 03:03 PM
  3. java -version pointing to older version
    By deepakts in forum New To Java
    Replies: 4
    Last Post: 05-06-2010, 10:59 AM
  4. Animal Shelter Manager 2.0.22
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-01-2007, 10:11 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
  •