A computer screen displaying Java code with the text 'Java Programming'" Caption: "Coding in Java
Coding in Java

Convert the QuartsToGallons class.Accept the value from the user as input.

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);
        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");



The program you described is a Java class that performs a conversion of quarts to gallons.

  1. 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.
  2. A variable is declared to represent the number of quarts needed for a painting job and is assigned a value of 18.
  3. 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.
  4. 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.
  5. 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.