Computer Science, asked by Anonymous, 1 year ago

Convert the QuartsToGallons program to an interactive application. Instead of assigning a value to the number of quarts, accept the value from the user as input:


class QuartsToGallonsInteractive
{
public static void main(String[] args)
{
// Modify the code below
final int QUARTS_IN_GALLON = 4;
int quartsNeeded = 18;
int gallonsNeeded;
int extraQuartsNeeded;
gallonsNeeded = quartsNeeded / QUARTS_IN_GALLON;
extraQuartsNeeded = quartsNeeded % QUARTS_IN_GALLON;
System.out.println("A job that needs " + quartsNeeded +
" quarts requires " + gallonsNeeded + " gallons plus " +
extraQuartsNeeded + " quarts.");
}
}

Answers

Answered by Anonymous
3

Answer:

As mentioned in the  problem we need to take inputs from the user so that we have created the instance of scanner class.

Now we can take inputs like QUARTS_IN_GALLON  and quartsNeeded

class QuartsToGallonsInteractive

{

public static void main(String[] args)

{

Scanner s = new Scanner(System.in)

int QUARTS_IN_GALLON = s.nextInt(); // here we are taking inputs from the user

int quartsNeeded = s.nextInt();

int gallonsNeeded ;

int extraQuartsNeeded;

gallonsNeeded = quartsNeeded / QUARTS_IN_GALLON;

extraQuartsNeeded = quartsNeeded % QUARTS_IN_GALLON;

System.out.println("A job that needs " + quartsNeeded +

" quarts requires " + gallonsNeeded + " gallons plus " +

extraQuartsNeeded + " quarts.");

}

}

Similar questions