FuelUp.java

1 minute read

This program was created using Java’s GUI called JOptionPane, which is completely outdated but it was a requirement for programming.

It is intended to calculate the amount of fuel a vehicle needs. There are restrictions as in that it needs 10 times the amount of fuel than the distance it travels and that it always carries a minimum of 100 fuel before setting off.

Therefore, we would have conditionals where if the distance is less than or equal to 10, it would remain at 100 due to the restriction.

The rest should be straightforward where you multiply by 100.

The code has been modified slightly and now uploaded to the site.

<— Return Home

JOptionPane Code Snippet

//Link to Challenge: https://edabit.com/challenge/4PGHC3nqN9gH2svby
import javax.swing.JOptionPane;
public class FuelUp {
    public static void main(String[] args){
        boolean looper = true;
        JOptionPane.showMessageDialog(null, "Welcome!\nPress enter to continue...");
        while(looper == true){
        String input = JOptionPane.showInputDialog(null, "Your vehicle needs 10 times the amount of fuel than the distance it travels.\nPlease type how much fuel you need.\nType -1 to exit!");
        double get = Double.parseDouble(input);
        if (get == -1){
            System.exit(0);
        }
        double result = calculateFuel(get);
        if (result % 1 == 0){
            int conversion = (int)(result);
            JOptionPane.showMessageDialog(null, "Your car needs " + conversion + " times the amount of fuel!");
        }
        else
            JOptionPane.showMessageDialog(null, "Your car needs " + result + " times the amount of fuel!");
    }
    }

    public static double calculateFuel(double n){
        if (n < 100)
            return 100;
        else{
            double total = n * 10;
            return total;
        }
    }
}

<— Return Home