General Java Problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2009
Posts: 6
Reputation: ClimaxBeetle is an unknown quantity at this point 
Solved Threads: 0
ClimaxBeetle ClimaxBeetle is offline Offline
Newbie Poster

General Java Problem

 
1
  #1
Mar 21st, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: General Java Problem

 
0
  #2
Mar 21st, 2009
@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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: ClimaxBeetle is an unknown quantity at this point 
Solved Threads: 0
ClimaxBeetle ClimaxBeetle is offline Offline
Newbie Poster

Re: General Java Problem

 
0
  #3
Mar 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: General Java Problem

 
0
  #4
Mar 21st, 2009
You need to put the two abstract methods eat and move in the Animal class like :
  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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: ClimaxBeetle is an unknown quantity at this point 
Solved Threads: 0
ClimaxBeetle ClimaxBeetle is offline Offline
Newbie Poster

Re: General Java Problem

 
0
  #5
Apr 9th, 2009
Now here's my 2nd attempt:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: General Java Problem

 
0
  #6
Apr 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: ClimaxBeetle is an unknown quantity at this point 
Solved Threads: 0
ClimaxBeetle ClimaxBeetle is offline Offline
Newbie Poster

Re: General Java Problem

 
0
  #7
Apr 10th, 2009
Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: General Java Problem

 
0
  #8
Apr 10th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 428 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC