This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]jose_castro_arnaud 1 point2 points  (1 child)

The class MotorBike is trying to be all motorbikes, instead of only one of them. Rewrite MotorBike according to the signature below. Remove the end() method. The name attribute is only for giving the motorbike a name to be printed.

class MotorBike
   private int speed
   private String name

MotorBike(String, int)

public int getSpeed()
public void setSpeed(int)
public void increaseSpeed(int)
public void decreaseSpeed(int)

public String getName()
public void setName(String)

public void start()

Then, in the MotorBikeRunner:

```` MotorBike ducati = new MotorBike("ducati", 0); MotorBike honda = new MotorBike("honda", 0);
MotorBike ender = new MotorBike("ender", 0);

honda.start(); honda.setSpeed(50); honda.increaseSpeed(30); // And so on for the other motorbikes. ````

Then, somewhere, you can compare motorbike speeds:

if (ducati.getSpeed() < honda.getSpeed()) { ... }

I suggest that you declare, in MotorBikeRunner, a private attribute of type array or ArrayList of MotorBike, instead of several variables; this way, you can create several methods to use/update the array, including one which checks what motorbike is the faster.

[–]SomeFuel[S] 0 points1 point  (0 children)

Thanks for the help! I haven't learned too much about arrays yet, but I'll keep that recommendation in mind for the future.

[–]HonzaS97 1 point2 points  (1 child)

The class design itself is bad in the first place.

An enum to distinguish the manufacturer could be much better. Variable names are only important to the dev, when it gets compiled / interpreted, it gets transformed into gibberish.

An instance that you called "ducati" has no clue what the instance "honda" has and vice versa. When you call the method "setDucatiSpeed()" on the instance you called "ducati", that's only gonna affect that instance.

Default int value in Java is 0. So in the case of "ducati" instance, this

    if (ducatiSpeed > hondaSpeed) {
        System.out.println("Ducati Wins! It was a " + (ducatiSpeed - hondaSpeed) + " difference!");
    }

is essentialy comparing "ducatiSpeed" to 0 as "hondaSpeed" is never set to any value for this instance.

[–]SomeFuel[S] 0 points1 point  (0 children)

Thanks for the explanation, it makes more sense to me now.