A) Write a java class that declares a named constant to hold the number of quart
ID: 671293 • Letter: A
Question
A) 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 gallons plus two quarts. Save the class as QuartsToGallons.java
B) convert QuartsToGallons class to an interactive application. Instead of assigning a value to the number of quarts, accept value from the user as input. Save the revised class as QuartsToGallonsInteractive.java
Explanation / Answer
public class Problem2 {
public static void main(String[] args) {
final int q=4;
int nquarts=18;//number of quarts needed
double gallons=(double)nquarts/(double)q;
System.out.println("Number of quarts needed to paint: "+nquarts);
System.out.println("Number of gallons needed to paint: "+gallons);
}
}
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args) {
final int q=4;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of quarts needed: ");
int nquarts=scan.nextInt();//number of quarts needed
double gallons=(double)nquarts/(double)q;
System.out.println("Number of quarts needed to paint: "+nquarts);
System.out.println("Number of gallons needed to paint: "+gallons);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.