All the input from the user should be from the command line (use the Scanner obj
ID: 3732350 • Letter: A
Question
All the input from the user should be from the command line (use the Scanner object). You only need to create one Scanner object to handle all the input.
Do not use JOptionPane.
Methods are a way to break apart a complex program into smaller pieces. In this assignment, you will modify your previous Gas Price Assignment 2 to enclose the different parts of your program inside methods.
Your main method will call five methods: displayPrices(), getGasType(), getGallons(), calculatePrice(), and displayTotal(). There should be nothing else in the main method besides calls to these methods and a few variable declarations.
The method displayPrices() will display to the user the prices for the gas.
The method getGasType() will return the value the user enters. If the value is not one of the valid values, tell the user to re-enter a value. Use a while loop that will continue to loop if the user does NOT enter a valid value. Since you have put the price list into the method displayPrices(), you can call that method from this method if the user did not enter a valid value. This method should return the int or char – your choice – that indicates the user’s selection.
The method getGallons() will ask the user how much they have pumped and return the value. Similar to the getGasType() method, this method should not accept bad data. The number should be between 1 and 100 inclusive (meaning 1 and 100 are valid values). You can accept floating point numbers if you wish. This method will return an int or double – your choice.
The method calculatePrice() will define two parameters, gas and gallons. The parameter gas will be either a char or an int depending on what was returned from getGasType(). The parameter gallons will be a double or an int depending on what was returned from getGallons(). This method will calculate the price of gas and return it as a double. It is in this method that you will put the switch statement from the previous assignment. This method will return the price of gas as a double.
The method displayTotal() will define one parameter, a double, and display the total price. The value sent to this method will be the value that was returned from calculatePrice().
The method headers:
public static void displayPrices()
public static char getGasType()
or public static int getGasType()
public static double getGallons()
or public static int getGallons()
public static double calculatePrice(char, double)
or public static double calculatePrice(int, int)
or public static double calculatePrice(char, int)
or public static double calculatePrice(int, double)
public static void displayTotal(double)
Notes:
Format the dollar amounts properly using printf. The dollar amounts should have two numbers after the decimal point.
All methods need to start with public static
To simplify the program, declare the Scanner object inside the class but outside the methods as shown below. It should be the first statement in the class, before the main method. All the methods will have access to it.
static Scanner keyboard = new Scanner(System.in);
Here is Part 2
import java.util.Scanner;
public class GasPrice2
{
public static final double REGULAR = 2.19, MIDGRADE = 2.49, PREMIUM = 2.71;
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String tGas;
int tGas2;
System.out.println(" What type of gas did you use? "
+ " Enter Number Option: (1)Regular at $2.19,"
+ " (2)Mid-grade at $2.49, or (3)Premium at $2.71 ");
tGas = input.nextLine();
tGas2 = Integer.parseInt(tGas);
String gasAmount;
double aGas;
System.out.println("How many gallons of gas did you pump?");
gasAmount = input.nextLine();
aGas = Double.parseDouble(gasAmount);
String tGas2String;
switch (tGas2)
{
case 1: tGas2String = "Your total price of gas is $" + (REGULAR * aGas);
break;
case 2: tGas2String = "Your total price of gas is $" + (MIDGRADE * aGas);
break;
case 3: tGas2String = "Your total price of gas is $" + (PREMIUM * aGas);
break;
default: tGas2String = "Invalid Gas Input";
break;
}
System.out.println(tGas2String);
}
}
Explanation / Answer
/**
* The java program that prompts user to enter user
* choice and then check if input is valid and then
* prompt for the number of gallons and then
* find the total cost of gallons.
* */
//GasPrice3.java
import java.util.Scanner;
public class GasPrice3
{
//set constants
public static final double REGULAR = 2.19;
public static final double MIDGRADE = 2.4;
public static final double PREMIUM = 2.71;
private static Scanner keyboard = new Scanner(System.in);
public static void main(String args[])
{
//calling displayPrices method
displayPrices();
//calling getGasType method
char gastype=getGasType();
//calling getGallons
double gallons=getGallons();
//calling calculatePrice method
double total_price=calculatePrice(gastype, gallons);
System.out.printf("You owe: $%5.2f ",total_price);
}
/*The method calculatePrice that takes gastype and number of gallons
* and returns the total price.*/
public static double calculatePrice(char gasType, double numGallons){
double totalPrice=0;
if(gasType=='r')
totalPrice=REGULAR*numGallons;
else if(gasType=='m')
totalPrice=MIDGRADE*numGallons;
else if(gasType=='p')
totalPrice=PREMIUM*numGallons;
return totalPrice;
}
/*The method getGallons that takes number of gallons
* and returns the total price.*/
public static double getGallons(){
String gasAmount;
double gallons;
do
{
System.out.println("How many gallons?");
gasAmount = keyboard.nextLine();
gallons = Double.parseDouble(gasAmount);
}while(gallons<=0);
return gallons;
}
/*The method getGasType that takes gastype and returns
* type of chars.*/
public static char getGasType(){
String tGas="";
char gasType;
do
{
tGas = keyboard.nextLine();
gasType=tGas.toLowerCase().charAt(0);
if(gasType!='r' && gasType!='m' && gasType!='p')
System.out.println("Invalid selection");
}while(gasType!='r' && gasType!='m' &&gasType!='p');
return gasType;
}
/*The method displayPrices that display type of gases.*/
public static void displayPrices(){
System.out.println("What type of gas?");
System.out.println("(R)egular: $12.19");
System.out.println("(M)idgrade: $2.49");
System.out.println("(P)remium: $2.71");
}
}//end of the class GasPrice3
-------------------------------------------------------------------------
Sample Output:
What type of gas?
(R)egular: $12.19
(M)idgrade: $2.49
(P)remium: $2.71
e
Invalid selection
R
How many gallons?
10
You owe: $21.90