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;
    private double radius;

    public Wheel()
    {
        position = 0.0;
        radius = 50.0;
    }

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

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

    public double getPosition()
    {
        return position;
    }
    
    public double getRadius()
    {
        return radius;
    }
    
    public double getCircumference()
    {
        double circumference;
        circumference = 2*radius*Math.PI;
        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;
    }
}

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. :)

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.