| Author |
Polymorphic behavior provided by inheritance programming assignment
|
Joniela Geldenhuys
Greenhorn
Joined: Aug 24, 2011
Posts: 10
|
|
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
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4750
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
change the return type of the method bounce to String. insert a constructor that takes a String as argument in the Bounceable class.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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!
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Polymorphic behavior provided by inheritance programming assignment
|
|
|