-4

I am currently in the design mode for this problem:

Implement the Speaker interface that is predefined. Create three classes that implement Speaker in various ways. Create a driver class whose main method instantiates some of these objects and tests their abilities.

How would I go about designing this program and them moving into the coding stage. I want to use these three classes to implement the Speaker interface class: Politician, Lecturer, and Pastor class. The methods I want to use are:

public void speak(); public void announce (String str);

Now for my design and coding, how would I go about to declare and object reference variable and have that variable have multiple references?

brennaboo
  • 9
  • 1
  • 1
  • 8
  • Just a guess: `implements Speaker` – Hot Licks Nov 25 '12 at 02:05
  • 4
    Have you gone through the tutorials on how to use interfaces? It will all be explained there. We're not too good at re-writing these tutorials and are much better equipped at helping you with errors once you've made an attempt -- something I strongly urge you to do. – Hovercraft Full Of Eels Nov 25 '12 at 02:07
  • (Hint: Conceive of implementing an interface as being similar to subclassing a parent class.) – Hot Licks Nov 25 '12 at 02:07
  • How about declaring an object reference variable? Any sample code for an example? – brennaboo Nov 25 '12 at 02:10

3 Answers3

1

It's simple really. In brief:

class ClassA implements Speaker
{
   public void speak(){
          System.out.println("I love Java") ; //implement the speak method
    }
}
class ClassB implements Speaker //follow the example of ClassA
class ClassC implements Speaker //same as above

Speaker[] speakers = new Speakers{new ClassA(),new ClassB(),new ClassC()} ;

for(Speaker speaker: speakers)
   speaker.speak(); //polymorphically call the speak() method defined in the contract.
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • So for my 3 speaker classes, how would I have each be more specific but still use the speak() and announce() methods. – brennaboo Nov 25 '12 at 02:12
  • When your speaker class implements an interface, they have to provide an implementation. In the example of ClassA I am saying ClassA will speak by printing out "I love Java".. – Lews Therin Nov 25 '12 at 02:13
  • Also if you notice the way I'm creating an array of Speakers, not an array of ClassA/B/C. – Lews Therin Nov 25 '12 at 02:15
0

See "What is an Interface?" http://docs.oracle.com/javase/tutorial/java/concepts/interface.html This will hopefully get you started with the basics you're looking for.

The start of the implementation would look something like the following...

class Politician implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}
class Lecturer implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}
class Lecturer implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}

public static void main(String [] args)
{
   Speaker s1 = new Politician();
   Speaker s2 = new Pastor();
   Speaker s3 = new Lecturer();
   // Do something...
}
bn.
  • 7,739
  • 7
  • 39
  • 54
-1

Use a Factory Method Design Pattern.. Check this article at http://en.wikipedia.org/wiki/Factory_method_pattern

Your code could look something like this, if you use the Factory pattern

public class SpeakerFactory {

      enum SpeakerEnum { POLITICIAN, LECTURER, PASTOR} ;
      Speaker getSpeaker(SpeakerEnum speakerType) {
          switch (speakerType) {

          case POLITICIAN : return new Politician();

          ....

          }
      }

}

austindz
  • 71
  • 1
  • 6