views:

42

answers:

1

Here is the question:

The driving distance between Perth and Adelaide is 1996 miles.

On the average, the fuel consumption of a 2.0 litre 4 cylinder car is 8 litres per 100 kilometres.

The fuel tank capacity of such a car is 60 litres.

Design and implement a JAVA program that prompts for the fuel consumption and fuel tank capacity of the aforementioned car.

The program then displays the minimum number of times the car’s fuel tank has to be filled up to drive from Perth to Adelaide. Note that 62 miles is equal to 100 kilometres.

What data will you use to test that your algorithm works correctly?

Here is what I've done so far:

import java.util.Scanner;//

public class Ex4{

  public static void main( String args[] ){

        Scanner input = new Scanner( System.in );
        double distance, consumption, capacity, time;
        distance = (1996/62*100);
        consumption = (8/100);
        capacity = 60;
        time = (distance*consumption/capacity);
        System.out.println("The car's fuel tank need to be filled up:"
                             + time + "times");
  }
}

I can compile it but the problem is that the result is always 0.0, can anyone help me what's wrong with it ?

+2  A: 

You are doing integer division (even though the result is a double), so consumption is zero. Try, 8.0d / 100.0d, and a similar adjustment for distance.

Ramashalanka
thanks a lot, it's correct now :)
Ivan
How about What data will you use to test that your algorithm works correctly? I don't understand which data +_+
Ivan
Well, for a start, to answer the question you've been given, you need to change `consumption` and `capacity` to user inputs with prompts, not fixed numbers. Specify the units with the prompt. The consumption should probably be as litres per 100 km rather than litres per km. Then enter the inputs from the question when prompted and check that you get the correct result (e.g. using a calculator to double check). Then, you'd try a few other inputs and check those. Perhaps check that you handle an input of zero for capacity correctly.
Ramashalanka