Basic Java Problem | Calculate if your Trip is Completed

Basic Java Problem: Calculate if your Trip is Completed

Basic Java Problem | Problem Statement

You can finally go on a road trip with your friends. The only problem is that your car has limited gasoline. You know that on average, your car gives you x kilometers per litre of petrol.

The nearest gasoline pump is y kilometers away from your current location. Given that you have p litres of petrol left in your car, return true if you can make it to the petrol station in time, else return false.

Hint: Understand the relation between speed, distance, and time and implement it.

Input format

The input consists of 3 double values denoting – petrol left, average kilometers per litre of petrol and distance from petrol pump, in that order.

Output format

Return true if you can make it to the petrol pump in time, else return false.

Sample Input 1

3 50 100

Sample Output 1

true

Explanation

The number of kilometers that can be covered = 3*50 = 150 km. This is more than the 100 km distance from the petrol pump. So, the answer is true.

[quads id=2]

Java Basic Problem | 6-Step Strategy to solve problems

Use the 6-step strategy to solve any problem

  1. Understand the problem
  2. Design test data/test cases (i.e. input and expected output)
  3. Derive the solution (write pseudocode for the solution)
  4. Test the solution (do a dry run of the pseudo-code for the test data and confirm it works)
  5. Write the program/code (using Java here)
  6. Test the code (debug any syntax errors, runtime errors, and logical errors).

Java Basic Problem: Solve using the 6-step strategy applied to the given problem statement for C, C++, Java, JavaScript, and Python.

Step 1: Understand the Problem


The problem is determining whether a car with a certain amount of petrol left can reach the nearest petrol pump given its average fuel efficiency and the distance to the pump.

Step 2: Design Test Data or Test Cases


Let’s design some test cases:

Input: 3 50 100
Expected Output: true

Input: 1 25 50
Expected Output: false

Step 3: Derive the Solution


We need to calculate the maximum distance the car can travel with the available petrol and then compare it with the distance to the petrol pump.

Pseudo Code:

1. Read petrol_left, avg_kmpl, distance
2. max_distance = petrol_left * avg_kmpl
3. if max_distance >= distance
4.     return true
5. else
6.     return false

Step 4: Test the Solution


Let’s dry-run the pseudo code with the provided test cases.

For Test Case 1:

petrol_left = 3, avg_kmpl = 50, distance = 100

max_distance = 3 * 50 = 150

max_distance >= distance, so return true

For Test Case 2:

petrol_left = 1, avg_kmpl = 25, distance = 50

max_distance = 1 * 25 = 25

max_distance < distance, so return false

Step 5: Write the Code

C
#include <stdio.h>

int main() {
    double petrol_left, avg_kmpl, distance;
    scanf("%lf %lf %lf", &petrol_left, &avg_kmpl, &distance);

    double max_distance = petrol_left * avg_kmpl;

    if (max_distance >= distance)
        printf("true\n");
    else
        printf("false\n");

    return 0;
}
C

C++
#include <iostream>
using namespace std;

int main() {
    double petrol_left, avg_kmpl, distance;
    cin >> petrol_left >> avg_kmpl >> distance;

    double max_distance = petrol_left * avg_kmpl;

    if (max_distance >= distance)
        cout << "true" << endl;
    else
        cout << "false" << endl;

    return 0;
}
C++

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double petrol_left = scanner.nextDouble();
        double avg_kmpl = scanner.nextDouble();
        double distance = scanner.nextDouble();

        double max_distance = petrol_left * avg_kmpl;

        if (max_distance >= distance)
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Java

JavaScript
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('', input => {
    const [petrol_left, avg_kmpl, distance] = input.split(' ').map(Number);

    const max_distance = petrol_left * avg_kmpl;

    if (max_distance >= distance)
        console.log("true");
    else
        console.log("false");

    rl.close();
});
JavaScript

Python
def main():
    petrol_left, avg_kmpl, distance = map(float, input().split())

    max_distance = petrol_left * avg_kmpl

    if max_distance >= distance:
        print("true")
    else:
        print("false")

if __name__ == "__main__":
    main()
Python

Step 6: Test the Code
Compile and run the code with the test cases you designed to make sure it works as expected. If any issues arise, debug and correct them.

Leave a Comment