Answer:
Certainly! Here's a simple Java program named GasPrice.java that follows your requirements:
java
Copy code
import java.util.Scanner;
public class GasPrice {
// Named constants for gas prices
private static final double REGULAR_GAS_PRICE = 3.00; // Replace with the actual price for regular gas
private static final double PREMIUM_GAS_PRICE = 3.50; // Replace with the actual price for premium gas
public static void main(String[] args) {
// Prompt user to input how much gas they need
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount of gas needed (in gallons): ");
// Read user input for gas amount
double gasAmount = scanner.nextDouble();
// Calculate the cost for regular and premium gas
double regularGasCost = REGULAR_GAS_PRICE * gasAmount;
double premiumGasCost = PREMIUM_GAS_PRICE * gasAmount;
// Output the results
System.out.println("Cost of Regular Gas: $" + regularGasCost);
System.out.println("Cost of Premium Gas: $" + premiumGasCost);
// Close the scanner
scanner.close();
}
}
Make sure to replace the placeholder values for REGULAR_GAS_PRICE and PREMIUM_GAS_PRICE with the actual prices for regular and premium gas.
You can test the program by compiling and running it. It will prompt you to enter the amount of gas needed and then display the cost for both regular and premium gas
Explanation: