Write a Java class that declares a named constant to hold the number of quarts i
ID: 3837083 • Letter: W
Question
Write a Java class that declares a named constant to hold the number of quarts in a gallon (4). Also declare a variable to represent the number of quarts needed for a painting job, and assign an appropriate value-for example, 18. Compute and display the number of gallons and quarts needed for the job. Display explanatory text with the values-for example, A job that needs 18 quarts requires 4 gal Ions plus 2 quarts. Save the class as QuartsToGallons.java. 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.Explanation / Answer
a)
public class QuartsToGallons {
public static void main(String[] args) {
//Declaring a named Constant
final int no_of_Quarts_in_gallon=4;
//Declaring a integer variable
int no_of_quarts=18;
//This statement will convert total quarts into gallons and quarts.
System.out.println("A job that needs "+no_of_quarts+" quarts requires "+no_of_quarts/no_of_Quarts_in_gallon+" gallons plus "+no_of_quarts%no_of_Quarts_in_gallon+" quarts.");
}
}
___________________
Output:
A job that needs 18 quarts requires 4 gallons plus 2 quarts.
___________________
b)
package org.students;
import java.util.Scanner;
public class QuartsToGallons {
public static void main(String[] args) {
//Declaring a named Constant
final int no_of_Quarts_in_gallon=4;
//Declaring a integer variable
int no_of_quarts = 0;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the no of quarts entered by the user
System.out.print("Enter no of quarts :");
no_of_quarts=sc.nextInt();
//This statement will convert total quarts into gallons and quarts.
System.out.println("A job that needs "+no_of_quarts+" quarts requires "+no_of_quarts/no_of_Quarts_in_gallon+" gallons plus "+no_of_quarts%no_of_Quarts_in_gallon+" quarts.");
}
}
____________________
output:
Enter no of quarts :35
A job that needs 35 quarts requires 8 gallons plus 3 quarts.
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.