-6

First 300 meters is 20 fee, succeeding 200 meters is 2 fee. I want to compute distance in meters.

input: 300 output: 20

input: 500 output: 22

input: 700 output: 24

public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    System.out.println("Input the distance in Meters: ");
    int getInput = sc.nextInt();

    double first = 20.00; //first 300 meters
    double second = 2.00; //every 200 meters

    if(getInput <= 300){

        System.out.println("Your trip fee is: " + first);

    }
    else if(getInput >= 300){
        double fee = getInput / 300;
        double tFee = fee * first;

        double remainder = getInput % 300;
        double output = remainder / 200;
        double fees = output * second;

        double totalFee = tFee + fees;

        System.out.println("Your trip fee is: " + totalFee );

    }else{


        System.out.println("i du nu!");

    }
}

i need algorithm for this, my mind is stuck. i already used if else statements.

  • 6
    You should take a look at [ask] – pvg Jun 29 '17 at 09:36
  • This is not coding service . What is the effort from ur side? – soorapadman Jun 29 '17 at 09:37
  • If you *tried* somethings, just post it along with your question. Use the [edit](https://stackoverflow.com/posts/44821387/edit) button below your post. Also, make sure you ask a clear question. What is your expected output? – MC Emperor Jun 29 '17 at 09:37
  • 1
    So first once 300&20, the remaining every 200&2. So input - 300 is interesting. – Joop Eggen Jun 29 '17 at 09:39
  • Your question is not clear enough and also you must show us what you have tried so far...we can't code for you from scratch – UkFLSUI Jun 29 '17 at 09:42
  • my expected output is 24, I just explain the inputs so that you can understand me. I'll post my code. wait for a moment – Kenneth Israel Jun 29 '17 at 09:44
  • The description of the problem is still pretty bad :) What should the fee be for intermediate values, for example 550? Is it 22, 24, or 22.25? Also, what does "I want to compute distance in meters." mean? Aren't you trying to computer the fee? – bluemind Jun 29 '17 at 11:32

2 Answers2

0

We should not code for you, but here is the algorithm may help you. But try to understand then implement:

if(input <= 300)
{
    output = 20;
}
else if(input > 300)
{
    extra = input - 300;

    if(extra%200 == 0)
    {
        output =  20 + (2*(extra/200));
    }
    else
    {
        output = 20 + (2*(extra/200)) + 2;
    }
}
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
0

Depending on what you want, I think there's three options. A sample program with all three:

    public static void main(String [] args){
//        Scanner sc = new Scanner(System.in);
//        System.out.println("Input the distance in Meters: ");
//        compute(sc.nextInt());

        System.out.println("input: 300");
        compute(300);
        System.out.println("input: 500");
        compute(500);
        System.out.println("input: 550");
        compute(550);
        System.out.println("input: 700");
        compute(700);
    }

    private static void compute(int input) {

        int output = 20 +  ((input - 300) / 200) * 2;
        System.out.println("Result A: " + output);

        double outputB = 20.0 +  ((input - 300.0) / 200.0) * 2.0;
        System.out.println("Result B: " + outputB);

        int outputC = 20 + (int)Math.ceil((input - 300.0) / 200.0) * 2;
        System.out.println("Result C: " + outputC);
    }

With these results:

input: 300
Result A: 20
Result B: 20.0
Result C: 20
input: 500
Result A: 22
Result B: 22.0
Result C: 22
input: 550
Result A: 22
Result B: 22.5
Result C: 24
input: 700
Result A: 24
Result B: 24.0
Result C: 24
bluemind
  • 1,591
  • 11
  • 16