-1

My assignment is to create a program that will give the MPH of a bicycle given the gear (1 - 3) and cadence (1-100).

  • 1st gear MPH = cadence/12,
  • 2nd gear MPH = cadence/6,
  • 3rd gear MPH = cadence/4.

We have set a class and need to run a test program. Here is what I have so far, I am just confused as to where I run the looping structure with user input and where my return statements should be:

public class Bicycle
{
    private int bike;
    private int gear = 1;
    private int cadence = 1;
    private int speed = 1;

    public void changeGear(int getGear)
    {

        gear = getGear;
    }

    public void changeCadence(int getCadence)
    {
        cadence = getCadence;
    }

    public void MPH(int getMPH)
    {
        speed = getMPH;
    }
}

Here is my test:

import java.util.Scanner;

public class BicycleTest
{
    public static void main(String[] args)
    {
        Bicycle bike;
        bike = new Bicycle();
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70

1 Answers1

0

First of all, to set and get the attributs of an instance, use the naming conventions (getStuff and setStuff). Then speed is not a property of your bike, it changes and depends of other attributs, also when setting new values, check out if it's in you requried range.

To get the speed, just check gear and return the good result :

public class Bicycle {

    private int bike;
    private int gear;
    private int cadence;

    public int getGear() {
        return gear;
    }    
    public void setGear(int gear) {
        if (gear >= 1 && gear <= 3) {
            this.gear = gear;
        }
    }

    public int getCadence() {
        return cadence;
    }
    public void setCadence(int cadence) {
        if (cadence >= 1 && cadence <= 100) {
            this.cadence = cadence;
        }
    }

    public double getSpeed() {
        if (gear == 1) {
            return cadence / 12.0;
        } else if (gear == 2) {
            return cadence / 6.0;
        } else if(gear == 3{
            return cadence / 4.0;
        }else{
            return 0;
        }
    }
}

To use this : instanciate a Bike, ask the user for the gear and cadence and you're done :

public static void main(String[] args) {
    Bicycle bike = new Bicycle();
    Scanner sc = new Scanner(System.in);

    System.out.println("Give the gear :");
    bike.setGear(Integer.parseInt(sc.nextLine()));

    System.out.println("Give the cadence :");
    bike.setCadence(Integer.parseInt(sc.nextLine()));

    double speed = bike.getSpeed();
    System.out.println(speed);
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thank you for explaining this further. I do have a follow up question...what is "Integer.parseInt(sc.nextLine)? I haven't seen that yet. – soconnell80 Oct 30 '18 at 17:48
  • @soconnell80 I personnaly always use `Integer.parseInt(sc.nextLine())` rather than `sc.nextInt()` to be sure to avoid problem with return line char, it just read the lines typed and convert it to `int` – azro Oct 30 '18 at 17:49
  • Okay, is this connected then to the user input? Again, I apologize for not understanding. Meaning, another way of using import java.util.Scanner? – soconnell80 Oct 30 '18 at 17:52
  • @soconnell80 you should read about asking a user input, System.in is the keyboard, and Scanner is used to listen to that input : https://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java – azro Oct 30 '18 at 17:53
  • thank you! I appreciate all of your help! This makes sense to me! – soconnell80 Oct 30 '18 at 18:03
  • @soconnell80 np ;) I let you upvote/accept the answer so ;) – azro Oct 30 '18 at 18:15