| | |
General Java Problem
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
However, I based these codings in our teacher's photocopied J.E.D.I modules
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.
java Syntax (Toggle Plain Text)
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
Last edited by ClimaxBeetle; Mar 21st, 2009 at 9:44 am.
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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 :
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.
java Syntax (Toggle Plain Text)
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
Now here's my 2nd attempt:
Java Syntax (Toggle Plain Text)
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"); } }
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
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.
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
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/under...schedule.shtml
![]() |
Similar Threads
- Static or not problem. (Java)
- help me solve my virus problem (Viruses, Spyware and other Nasties)
- IE and Firefox problems (Windows NT / 2000 / XP)
- Syntax Analyser and General Java Help (Java)
- hijacker.exe help, have no clue what's causing problem and don't want to delete good (Viruses, Spyware and other Nasties)
- needed big time hw due and late (C)
- Problem's checking e-mail (Viruses, Spyware and other Nasties)
- On Internet Explorer, My Java does not work! (Web Browsers)
- Another problem with BRIDGE.DLL (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: JComboBox help
- Next Thread: File Question
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online pearl problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






