Access over 20 million homework & study documents

8 Cookie Calories A bag of cookies holds 40 cookies The calorie

Content type
User Generated
Rating
Showing Page:
1/3
8. Cookie Calories
A bag of cookies holds 40 cookies. The calorie information
on the bag claims that there are 10 servings in the bag and
that a serving equals 300 calories. Write a JAVA program
that lets the user enter the number of cookies he or she
actually ate and then reports the number of total calories
consumed.
SAMPLE RUN #6: java CookieCalorieCounter
Enter·number·of·cookies·eaten:3
Solution
import java.util.Scanner;
/**
* @author
*
*/
public class CookieCalorieCounter {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
scanner = new Scanner(System.in);
// prompt read the number of cookies eaten
System.out.print(\"Enter number of cookies
eaten:\");
int cookieCount = scanner.nextInt();
int servingSize = 40 / 10;
int caloriesPerCookie = 300 / servingSize;
int totalCalories = cookieCount *
caloriesPerCookie;
// print the result console
System.out.println(\"Your calorie intake was: \" +
totalCalories
+ \" calories\");
} catch (Exception e) {
// TODO: handle exception
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}
OUTPUT:
Enter number of cookies eaten:3
Your calorie intake was: 225 calories

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
8. Cookie Calories A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a serving equals 300 calories. Write a JAVA program that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed. SAMPLE RUN #6: java CookieCalorieCounter Enter·number·of·cookies·eaten:3 Solution import java.util.Scanner; /** * @author * */ public class CookieCalorieCounter { /** * @param args */ public static void main(String[] args) { Scanner scanner = null; try { scanner = new Scanner(System.in); // prompt read the number of cookies eaten System.out.print(\"Enter number of cookies eaten:\"); int cookieCount = scanner.nextInt(); int servingSize = 40 / 10; int caloriesPerCookie = 300 / servingSize; int totalCalories = cookieCount * caloriesPerCookie; // print the result conso ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Nice! Really impressed with the quality.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4