943,673 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 708
  • Java RSS
Mar 21st, 2009
1

General Java Problem

Expand Post »
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.

java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. abstract class Animals{
  4. String inputAnimals[];
  5. void setAnimals(String inputAnimals[]){
  6. this.inputAnimals = inputAnimals;
  7. }
  8. void printAnimals(){
  9. for (int i=0; i<inputAnimals.length; i++){
  10. JOptionPane.showMessageDialog(null,inputAnimals[i]);
  11. }
  12. }
  13. abstract void displayAnimal();
  14. }
  15.  
  16. class Rabbit extends Animals{
  17. void displayAnimal(){
  18. JOptionPane.showMessageDialog(null,"He eats carrots!");
  19. JOptionPane.showMessageDialog(null,"He leaps in 2 legs");
  20. }
  21. }
  22.  
  23. class Fish extends Animals{
  24. void displayAnimal(){
  25. JOptionPane.showMessageDialog(null,"It eats algae, plankton and other aquatic creatures");
  26. JOptionPane.showMessageDialog(null,"It swims");
  27. }
  28. }
  29.  
  30. class Bear extends Animals{
  31. void displayAnimal(){
  32. JOptionPane.showMessageDialog(null,"It eats live fishes");
  33. JOptionPane.showMessageDialog(null,"It can walk in 4 legs");
  34. }
  35. }

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.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
ClimaxBeetle is offline Offline
15 posts
since Mar 2009
Mar 21st, 2009
0

Re: General Java Problem

@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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 21st, 2009
0

Re: General Java Problem

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.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
ClimaxBeetle is offline Offline
15 posts
since Mar 2009
Mar 21st, 2009
0

Re: General Java Problem

You need to put the two abstract methods eat and move in the Animal class like :
java Syntax (Toggle Plain Text)
  1. public abstract void eat();
  2. 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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Apr 9th, 2009
0

Re: General Java Problem

Now here's my 2nd attempt:
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. abstract class Animals{
  4. String inputAnimals[];
  5. void setAnimals(String inputAnimals[]){
  6. this.inputAnimals = inputAnimals;
  7. }
  8. void printAnimals(){
  9. for (int i=0; i<inputAnimals.length; i++){
  10. JOptionPane.showMessageDialog(null,inputAnimals[i]);
  11. }
  12. }
  13. abstract void displayAnimal();
  14. public abstract void eat();
  15. public abstract void move();
  16. }
  17. class Rabbit extends Animals{
  18. void displayAnimal(){
  19. JOptionPane.showMessageDialog(null,"He eats carrots!");
  20. JOptionPane.showMessageDialog(null,"He leaps in 2 legs");
  21. }
  22.  
  23. @Override
  24. public void eat() {
  25. throw new UnsupportedOperationException("He eats carrots!");
  26. }
  27.  
  28. @Override
  29. public void move() {
  30. throw new UnsupportedOperationException("He leaps in 2 legs");
  31. }
  32. }
  33.  
  34. class Fish extends Animals{
  35. void displayAnimal(){
  36. JOptionPane.showMessageDialog(null,"It eats algae, plankton and other aquatic creatures");
  37. JOptionPane.showMessageDialog(null,"It swims");
  38. }
  39.  
  40. @Override
  41. public void eat() {
  42. throw new UnsupportedOperationException("It eats algae, plankton and other aquatic creatures");
  43. }
  44.  
  45. @Override
  46. public void move() {
  47. throw new UnsupportedOperationException("It swims");
  48. }
  49. }
  50.  
  51. class Bear extends Animals{
  52. void displayAnimal(){
  53. JOptionPane.showMessageDialog(null,"It eats live fishes");
  54. JOptionPane.showMessageDialog(null,"It can walk in 4 legs");
  55. }
  56.  
  57. @Override
  58. public void eat(){
  59. throw new UnsupportedOperationException("It eats live fishes");
  60. }
  61.  
  62. @Override
  63. public void move(){
  64. throw new UnsupportedOperationException("It can walk in 4 legs");
  65. }
  66. }
Reputation Points: 17
Solved Threads: 0
Newbie Poster
ClimaxBeetle is offline Offline
15 posts
since Mar 2009
Apr 9th, 2009
0

Re: General Java Problem

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 10th, 2009
0

Re: General Java Problem

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.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
ClimaxBeetle is offline Offline
15 posts
since Mar 2009
Apr 10th, 2009
0

Re: General Java Problem

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
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JComboBox help
Next Thread in Java Forum Timeline: File Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC