954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

General Java Problem

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

ClimaxBeetle
Newbie Poster
15 posts since Mar 2009
Reputation Points: 17
Solved Threads: 0
 

@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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

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.

ClimaxBeetle
Newbie Poster
15 posts since Mar 2009
Reputation Points: 17
Solved Threads: 0
 

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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

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");
    }
}
ClimaxBeetle
Newbie Poster
15 posts since Mar 2009
Reputation Points: 17
Solved Threads: 0
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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.

ClimaxBeetle
Newbie Poster
15 posts since Mar 2009
Reputation Points: 17
Solved Threads: 0
 

That is exactly what I'm saying. . you have to use a print statement or a GUI to make it display anything. To read about what Exceptions are, look at the lectures on Exceptions. They are fairly easy to read. http://www.cs.umbc.edu/courses/undergraduate/202/spring09/MiscPages/schedule.shtml

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You