Hello, this is my first time in participating in this forum and also this is the first time that I've used Java for my entire life.

The objective behind my problem was:
"Create an Animal Interface that has two methods: eat and move. All these methods do not have any arguments, nor any return tree. These methods simply print out how the Animal object eats and moves"

However, I applied the abstract class for an attempt.

import javax.swing.JOptionPane;

abstract class Animals{
    String inputAnimals[];
    void setAnimals(String inputAnimals[]){
        this.inputAnimals = inputAnimals;
    }
    void printAnimals(){
        for (int i=0; i<inputAnimals.length; i++){
        JOptionPane.showMessageDialog(null,inputAnimals[i]);
        }
    }
    abstract void displayAnimal();
}

class Rabbit extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"He eats carrots!");
        JOptionPane.showMessageDialog(null,"He leaps in 2 legs");
    }
}

class Fish extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"It eats algae, plankton and other aquatic creatures");
        JOptionPane.showMessageDialog(null,"It swims");
    }
}

class Bear extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"It eats live fishes");
        JOptionPane.showMessageDialog(null,"It can walk in 4 legs");
    }
}

However, I based these codings in our teacher's photocopied J.E.D.I modules

stephen84s commented: A rare species of Newbies, using code tags correctly in the first post itself :P +7

@ClimaxBeetle: I appreciate your use of code tags in the very first post. But you haven't mentioned what your problem is, tell us why you have stuck.

Whoops, kinda vague eh.

This is actually a problem given us by our teacher:
"Create an Animal Interface that has two methods: eat and move. All these methods do not have any arguments, nor any return tree. These methods simply print out how the Animal object eats and moves".

I've tried making it, and that was through the abstract class thing provided in the code.

You need to put the two abstract methods eat and move in the Animal class like :

public abstract void eat();
public abstract void move();

This is part of the "contract" of being an "Animal". Any object that "inherits" from Animal should implement these two behaviours. So in programmatic sense any class that extends the Animal class should provide a "specific" implementation of eating and moving, so in essence you should write the eat and move methods.

So instead of putting the System.our.println statements in the display method you could put them in the eat and move method individually and then in the display method give a call to these methods.

Now here's my 2nd attempt:

import javax.swing.JOptionPane;

abstract class Animals{
    String inputAnimals[];
    void setAnimals(String inputAnimals[]){
        this.inputAnimals = inputAnimals;
    }
    void printAnimals(){
        for (int i=0; i<inputAnimals.length; i++){
        JOptionPane.showMessageDialog(null,inputAnimals[i]);
        }
    }
    abstract void displayAnimal();
      public abstract void eat();
      public abstract void move();
}
class Rabbit extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"He eats carrots!");
        JOptionPane.showMessageDialog(null,"He leaps in 2 legs");
    }

    @Override
    public void eat() {
        throw new UnsupportedOperationException("He eats carrots!");
    }

    @Override
    public void move() {
        throw new UnsupportedOperationException("He leaps in 2 legs");
    }
}

class Fish extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"It eats algae, plankton and other aquatic creatures");
        JOptionPane.showMessageDialog(null,"It swims");
    }

    @Override
    public void eat() {
        throw new UnsupportedOperationException("It eats algae, plankton and other aquatic creatures");
    }

    @Override
    public void move() {
        throw new UnsupportedOperationException("It swims");
    }
}

class Bear extends Animals{
    void displayAnimal(){
        JOptionPane.showMessageDialog(null,"It eats live fishes");
        JOptionPane.showMessageDialog(null,"It can walk in 4 legs");
    }

    @Override
    public void eat(){
        throw new UnsupportedOperationException("It eats live fishes");
    }

    @Override
    public void move(){
        throw new UnsupportedOperationException("It can walk in 4 legs");
    }
}

Why are you throwing an exception, specifically, an UnsupportedOperationException, for your Animals' move and eat methods? You should be using print statements: System.out.println("A fish eats other fish"); or using the JOptionPane.showMessageDialog method.

Why are you throwing an exception, specifically, an UnsupportedOperationException, for your Animals' move and eat methods? You should be using print statements: System.out.println("A fish eats other fish"); or using the JOptionPane.showMessageDialog method.

It was first hinted by my NetBeans 6.5, and I tried it. It had no errors, yet it didn't display anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.