So my teacher this semester is the worst I ever had, I can not get a hold of him at all for help on these labs I've been working on them and trying to get his help on them for the past 3 weeks but since it's an online class, the teacher and fellow classmates are very hard to get a hold of. We have a total of 6 labs in this chapter but I got 1 done and 1 partially done.

My first question is I've got this mostly done but there are a few methods that I just don't know what to do with to get the output the books shows. It has a base class called Ball, 2 concrete sub-classes called Bowlingball and Baseball, a abstract sub-class called Bounceable, and that has 2 concrete sub-classes called Tennisball and Basketball. Now I have this mostly working except for two methods. All the methods an instance variables that are needed or wanted are there, the book specifically states that no others are needed anywhere that I didn't include them.

//my base class
public abstract class Ball
{
	private String type;
			
	public String getType()
	{
		return type;
	}
	
	public abstract void play();
}
//Bowlingball
public class Bowlingball extends Ball
{
	public void play()
	{
	}
}
//Baseball
public class Baseball extends Ball
{
	public void play()
	{
	}
}
//Bounceable
public abstract class Bounceable extends Ball
{
	public abstract String bounce();
}
//Tennisball
public class Tennisball extends Bounceable
{
	public String bounce()
	{
		return "boing!";
	}
	
	public void play()
	{
		
	}
}
//Basketball
public class Basketball extends Bounceable
{
	public String bounce()
	{
		return "dribble...";
	}
	
	public void play()
	{
	}
}
//Test class
public class PolyTest
{
	public static void main(String[] args)
	{
		Ball[] ball = {new Baseball(),new Basketball(),new Bowlingball(), new Tennisball()};
		
		
		System.out.println("For each type of ball...\n");
		
		for(int i=0; i<ball.length;++i)
		{
			System.out.println("Ball #"+(i+1)+" is a " +ball[i].getType());
			
			System.out.print("Playing: ");
			ball[i].play();
			
			if(ball[i] instanceof Bounceable)
				
				System.out.println("Bouncing "+((Bounceable)ball[i]).bounce());
			else
				System.out.println("You can't bounce this ball!");
			
			System.out.println("\n");
		}
		
		System.out.println("\nEnd of Program.");
	}
}

Now my first problem is with method play(), as you can see I don't have play doing anything because I don't know how I should code it, also I don't know why my getType() is not working. I need the output to look like this:
"For each type of ball..

Ball #1 is a Baseball
Playing: The American pasttime - batter up!
You can't bounce this ball!

Ball #2 is a Basketball
Playing: Basketball begins with a tipoff
Bouncing dribble...

Ball #3 is a Bowling ball
Playing: Set up the pins. Do you know how to score?
You can't bounce this ball!

Ball #4 is a Tennis ball
Playing: All set for a match? I love this game!
Bouncing boing!


End of program"

I have it outputing everything except the ball type and the playing line, and normally I'd set up play() to return a string and return what the book shows but, the problem states that it needs to return type void. If somebody could help me out with this I'd really appreciate it.


My second question was if somebody could direct me to where I could learn more about "Using Final Classes and Methods" that also would be greatly appreciated. Thank you to whoever could help me out.

Also how do I rate up somebody who helped me out last time?

Recommended Answers

All 2 Replies

This problem is teaching you about method overriding and how it is an important part of Inheritance. For example, your baseball's play() method should print "Playing America's past time" to the user, the Basketball class's play() method could print out "Playing Basketball", etc. The same goes for the bounce() method - certain types of balls cannot bounce, so they would just implement the bounce() method so that it says "I don't bounce()!" ...

Ok, so then how do I override it with a void type method? Because I did the overriding part correctly with bounce() didn't I?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.