186k views
0 votes
// QuartsToGallons.java public class QuartsToGallons { public static final int QUARTS_IN_GALLON = 4; public static void main(String[] args) { int quartsNeeded = 18; // finding required quarts System.out.println("A job that needs " + quartsNeeded + " quarts requires " + (quartsNeeded / QUARTS_IN_GALLON) + "gallons plus " + (quartsNeeded % QUARTS_IN_GALLON) + "quarts"); } }

1 Answer

5 votes

Program rearranged

// QuartsToGallons.java

public class QuartsToGallons

{

public static final int QUARTS_IN_GALLON = 4;

public static void main(String[] args)

{

int quartsNeeded = 18;

// finding required quarts

System.out.println("A job that needs " + quartsNeeded + " quarts requires " + (quartsNeeded / QUARTS_IN_GALLON) + "gallons plus " + (quartsNeeded % QUARTS_IN_GALLON) + "quarts");

}

}

Answer:

The output of the program is

A job that needs 18 quarts requires 4 gallons plus 2 quarts

Step-by-step explanation:

From the rearranged program above

At line 3, a global variable named QUARTS_IN_GALLON is declared

The Variable is declared as an integer

And the Variable is initialised to 4

At line 5, a local Variable is declared

The Variable is declared as an integer

And the Variable is initialised to 18

At Line 7, some arithmetic operations are done and the result of the Calculation is printed alongside some strings

The first operation is quartsNeeded/QUARTS_IN_GALLON

This is an integer division and the result will be in integer.

So, 18/4 = 4

The second operation is quartsNeeded%QUARTS_IN_GALLON

This means the remainder of the integer division

So 18%4 = 2

Then, the final step is to execute the print operation which outputs the following

A job that needs 18 quarts requires 4 gallons plus 2 quarts

User Racs
by
3.3k points