-1

This is an example of what I'm trying to do, this is hard to explain so this is the simplified version using pets:

public interface Pet {

    public String talk();
}

public class Dog implements Pet{

       public String talk()
       {
           return "Woof!";
       }
}

import java.util.ArrayList;


public class Person {

    ArrayList<Pet> pets = new ArrayList<Pet>();

       public Person()
       {
         pets.add(new Dog()); 
       }

       public void makePetsSpeak()
       {
          for(int i=0; i < pets.size(); i++)
          {
            System.out.println(pets.get(i).talk());
          }
       }
}

public class Main
{
    public static void main(String [] args)
    {
        Person p = new Person();
        p.makePetsSpeak();
    }
}

When running Person.MakePetsSpeak() it returns nothing. Is what i'm doing possible or is there another way to do it?

Sam King
  • 23
  • 3

3 Answers3

1

An interface is always abstract. So

public abstract interface Pet

will be

public interface Pet

Abstract is used in some codes (old codes) just for compatibilty, but you don't need it anymore.

List don't have a Count method. Use size

To get an item from a List you should use get method.

You should change your

System.out.println(pets(i).Talk());

to

System.out.println(pets.get(i).Talk());

And, anyway your code remember me a C# code. Follow java rules about naming conventions.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
1

This modified version of your code runs OK. Indeed MakePetsSpeak returns
nothing as it is declared void. But "Woof!" is printed out. So the answer
is you have no real problem here (once you fix the compilation errors).

import java.util.ArrayList;
import java.util.List;

abstract interface Pet {
    public String Talk();
}

class Dog implements Pet {

    public String Talk() {
        return "Woof!";
    }
}

public class Person {
    List<Pet> pets = new ArrayList<Pet>();

    public Person() {
        pets.add(new Dog());
    }

    public void MakePetsSpeak() {
        for (int i = 0; i < pets.size(); i++) {
            System.out.println(pets.get(i).Talk());
        }
    }

    public static void main(String[] args) {
        Person p = new Person();
        p.MakePetsSpeak();
    }
}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Thanks again. It was very hard for me to write out as the code i've written is nothing short of over complicated and would take me longer to write out than making a smaller example would :D – Sam King Feb 04 '14 at 21:42
0

Corrected your compilation problems, and looks like it works just fine, with the expected behaviour. Also, follow Java naming conventions - functions start with lower case.

Pet.java

public interface Pet {

    public String talk();
}

Dog.java

public class Dog implements Pet{

       public String talk()
       {
           return "Woof!";
       }
}

Person.java

import java.util.ArrayList;


public class Person {

    ArrayList<Pet> pets = new ArrayList<Pet>();

       public Person()
       {
         pets.add(new Dog()); 
       }

       public void makePetsSpeak()
       {
          for(int i=0; i < pets.size(); i++)
          {
            System.out.println(pets.get(i).talk());
          }
       }
}

Main.java

public class Main
{
    public static void main(String [] args)
    {
        Person p = new Person();
        p.makePetsSpeak();
    }
}

Output:

Woof!
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
  • Thank you. Can this work with hashmaps as well? – Sam King Feb 04 '14 at 21:37
  • I don't see the relation of this code with `hashmaps`. – Hugo Sousa Feb 04 '14 at 21:40
  • Sorry, I don't understand what you mean. That's the `Main` class. What do you mean if this can work with `hashmaps`? – Hugo Sousa Feb 04 '14 at 21:47
  • Sorry but I kept pressing enter rather than shift + enter for new line – Sam King Feb 04 '14 at 21:48
  • Yes, you could have something like that. Actually, a `Person` could have more than 1 `Pet`, so the `HashMap` should have a `List` of `Pet`. Also, to get the value from the key `Person`, you would have to override the `equals` method, used in the `HashMap` `get` method. Hope it helped. – Hugo Sousa Feb 04 '14 at 21:54
  • is there a standard way of doing this, i.e. can you point me in the direction of a tutorial or an example of overriding the get method, no idea how to do something like this? – Sam King Feb 04 '14 at 21:57
  • Override the `equals` method of `Person`, not the `get`.http://stackoverflow.com/questions/185937/overriding-the-java-equals-method-quirk – Hugo Sousa Feb 04 '14 at 21:59
  • I've had a go at the arraylist within my code but still can't get it working. I have to use definite types e.g. in the examples case I would have to give person a Dog rather than array of pets and do p.Dog.talk();. I'll keep at it there must be something I've missed – Sam King Feb 04 '14 at 22:28