Question: Polymorphism Sally runs a bike store but has a problem with her suppliers. She does not know what type of bike she will receive until

Polymorphism

Sally runs a bike store but has a problem with her suppliers. She does not know what type of bike she will receive until it arrives. She only knows she will receive 10 bikes every day. She wants to be able to create a list of bikes as they are received and display this on her computer so she will know what types of bikes arrived. Fred agreed to help her by writing a Java program that would allow this to be automated. Unfortunately, a new bike arrived and Fred has been off riding and has not been seen since. Sally sells three types of bikes. A regular bicycle with a max speed of 20 mph. It is powered by pedaling. A motorcycle with a max speed of 100 mph that is powered by gasoline. Finally, a scooter with a max speed of 30 mph that is powered by electricity. Fred has created a base class, Bike.java. Sally does not want us to modify this since Fred may return from his ride and she doesnt want to upset him Your job is to create three classes that inherit from Bike and a BikeTester class. The three classes are Bicycle.java, Motorcycle.java and Scooter.java. Each of these classes will need to complete the implementation began in Bike.java. Since Sally doesnt want us to change Freds code, you will need to create instance variables in each of these classes to hold the max speed and the power type. Notice Fred has plans for a custom exception to be thrown from Bike.java. We will need to create this exception. Finally, create a class that will randomly generate the bikes Sally receives in a day. This class should create the appropriate number of bikes of random types. Once these are created, you should use the toString() to print the list of bikes. The normal ratio of bikes received is 3 Motorcycles, 3 Scooters and 4 Bicycles. You should build your program to use a random distribution based on this normal distribution. Submit files Bicycle.java, Motorcycle.java, Scooter.java, BikeTester.java and UnkownPowerTypeException.java. Sample output:

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Bicycle with a max speed of 20

It is powered by pedal.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

Your bike is a Motorcycle with a max speed of 100

It is powered by gas.

Your bike is a Scooter with a max speed of 30

It is powered by electric.

bike.java below

public abstract class Bike

{

private String[] powerTypes = {"pedal", "gas", "electric"};

private String type;

public abstract int getMaxSpeed();

public abstract String getPowerType();

public Bike(String type)

{

this.type = type;

}

public String getPowerTypes(int num)

{

return powerTypes[num];

}

public void validatePowerType(String type) throws UnknownPowerTypeException

{

boolean found = false;

for (int index = 0; index < powerTypes.length && !found; index++)

{

if (type.equalsIgnoreCase(powerTypes[index]))

{

found = true;

}

}

if (!found)

{

throw new UnknownPowerTypeException("Invalid power type " + type + " encountered.");

}

}

public String getType()

{

return type;

}

public String toString()

{

return "Your bike is a " + type + " with a max speed of " + getMaxSpeed()

+ " \t It is powered by " + getPowerType() + ".";

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!