Convert the QuartsToGallons class to an interactive application. Instead of
assigning a value to the number of quarts, accept the value from the user as input.
Save the revised class as QuartsToGallonsInteractive.java.
public class QuartsToGallonInteractive {
public static void main(String[] args) {
int req_quarts;
System.out.println("Enter the number of required quarts:");
Scanner input=new Scanner(System.in);
req_quarts=input.nextInt();
int num_of_gallons=req_quarts/4;
int num_of_rem_quarts=req_quarts%4;
System.out.println("A job that needs "+req_quarts+ " quarts required "+num_of_gallons+" gallons plus "+num_of_rem_quarts+" quarts");
}
}
Explanation
The program you described is a Java class that performs a conversion of quarts to gallons.
- A named constant is declared to hold the number of quarts in a gallon, which is 4. This constant is used to perform the conversion.
- A variable is declared to represent the number of quarts needed for a painting job and is assigned a value of 18.
- The program then computes the number of gallons needed for the job by dividing the number of quarts by the number of quarts in a gallon.
- The number of gallons and quarts needed for the job is displayed with an explanatory text. For example, if 18 quarts are needed for a job, the program would display that 4 gallons plus 2 quarts are required.
- The class is saved as “QuartsToGallons.java”.
The program demonstrates how to perform a conversion of quarts to gallons and display the result in a user-friendly manner.
Leave a Reply