• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Polymorphic behavior provided by inheritance programming assignment

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys can someone please help me!

this is what the assignment want from me

1. Create an abstract base class, Ball. the single constructor requires a String to indicate the type of ball, which then is stored in an instance variable. The instance method getType(), returns a new String containing the type of ball. the class declares an abstract method play() which returns void
2. Create an abstract class Bounceable which inherits from Ball. Bouncealbe has a single abstract method, bounce() which returns a String there are no instance varibles
3. create 2 concrete classes Baseball and Bowlingball which inherit from Ball. No instance variables or additional methods are required only implementation of any inherited abstract methods
4. create 2 concrete classes Tennisball and Basketball which inherit from Bounceable. No instance variables or additonal methods are required only implementation of any inherited abstract methods

The class PolyTest source code they gave me
Here is all my classes
















These are the errors

PolyTest.java: void type not allowed here
system.out.println("Ball #" +(i+1)+ "is a "+ball[i].getType());

Basketball.java: Basketball is not an abstract and does not override abstract method bounce() in Bounceable
public class Basketball extends Bounceable

Basketball.java: bounce() in Basketball cannot override bounce() in Bounceable; attempting to use incompatible return type
found: java.lang.string
required: void
public String bounce()

Basketball.java: Cannot find symbol
symbol: Constructor Bounceable(java.lang.string)
location: class Bounceable
super("Basketball");

Tennisball.java: Tennisball is not an abstract and does not override abstract method bounce() in Bounceable
public class Tennisball extends Bounceable

Tennisball.java: bounce() in Tennisball cannot override bounce() in Bounceable; attepmting to use incompatible return type
found: java.lang.string
required: void
public String bounce()

Tennisball.java: Cannot find symbol
Symbol: Constructor Bounceable(java.lang.string)
Location: class Bounceable
super("Tennisball");

Please tell me what I'm doing wrong

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joniela Geldenhuys wrote:Please tell me what I'm doing wrong


When you get a compiler error you need to read it very carefully and then look at your code. The messages are usually quite specific. For example:
Basketball.java: bounce() in Basketball cannot override bounce() in Bounceable; attempting to use incompatible return type
found: java.lang.string
required: void
public String bounce()

is telling you that there is an incompatibility between the method bounce() in Basketball, and the one in Bounceable. Have a look at both methods carefully. Do you see the problem now? If not, come back; otherwise work through each message with the same care.

Nobody ever said programming was easy .

Winston
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change the return type of the method bounce to String. insert a constructor that takes a String as argument in the Bounceable class.


 
Sheriff
Posts: 22857
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joniela Geldenhuys wrote:PolyTest.java: void type not allowed here
system.out.println("Ball #" +(i+1)+ "is a "+ball[i].getType());


I don't get that error.

Basketball.java: Basketball is not an abstract and does not override abstract method bounce() in Bounceable
public class Basketball extends Bounceable

Basketball.java: bounce() in Basketball cannot override bounce() in Bounceable; attempting to use incompatible return type
found: java.lang.string
required: void
public String bounce()


These two are related. Bounceable specifies method public void bounce(), yet Basketball has method public String bounce(). The return types must match. Just change the void into String in Bounceable.

Basketball.java: Cannot find symbol
symbol: Constructor Bounceable(java.lang.string)
location: class Bounceable
super("Basketball");


Class Bounceable has no constructor that takes a String. Add one:

Tennisball.java: Tennisball is not an abstract and does not override abstract method bounce() in Bounceable
public class Tennisball extends Bounceable

Tennisball.java: bounce() in Tennisball cannot override bounce() in Bounceable; attepmting to use incompatible return type
found: java.lang.string
required: void
public String bounce()

Tennisball.java: Cannot find symbol
Symbol: Constructor Bounceable(java.lang.string)
Location: class Bounceable
super("Tennisball");


The same issues as with Basketball.

After you've solved these errors there is a bit of advice I would like to give. I think that Bounceable doesn't belong into this hierarchy. It seems better to make it an interface:
Basketball and Tennisball then only need minimal change:
The rest of your classes can remain the same. And as a bonus, you can create a class BouncingCastle that also implements Bounceable without having to be a Ball!
reply
    Bookmark Topic Watch Topic
  • New Topic