hi, im trying to make a bike move by using the pedal() method, the method will move the bike forward a distance of half the circumference of a wheel. ive tried creating the pedal() method however when the method is invoke the position of the bike is still the same. please help me. thanks.

``````import java.lang.Math;

public class Wheel
{
private double position;

public Wheel()
{
position = 0.0;
}

{
}

public void setPosition(double newPosition)
{
position = newPosition;
}

public void rollForward(double distance)
{
position = position + distance;
}

public double getPosition()
{
return position;
}

{
}

public double getCircumference()
{
double circumference;
return circumference;
}

}``````
``````public class Bike
{
private Wheel frontWheel;
private Wheel backWheel;

public Bike()
{

frontWheel = new Wheel();
backWheel = new Wheel();

frontWheel.setPosition(50);
backWheel.setPosition(-50);
}

public double getPosition()
{
double position;
position = (frontWheel.getPosition() + backWheel.getPosition())/2;
return position;
}
public double pedal() // here's my problem
{

double position;
position = getPosition() + frontWheel.getCircumference()/2;
return position;
}
}``````
2
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by thekashyap

Seems like when you do call the pedal(), it would return you the new position (say P1).
But the next time you call pedal again, it would still give you P1.
You should also call `rollForward()` of both wheels inside pedal() to update required members inside wheel objects.

--edit--
The way you've written the wheel interface you can call setPosition() as well. But if you ask me you should take the position as a param in c'tor and remove setPosition() method from Wheel. So that once a cycle is created the wheels can only be rolled, not moved. :)

Edited by thekashyap: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.