question archive A bag of cookies holds 40 cookies

A bag of cookies holds 40 cookies

Subject:Computer SciencePrice:4.89 Bought3

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. Create a VB application that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed. If the user fails to enter a numeric value, display an appropriate error message and do not attempt to perform calculations.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Program code screen shot:

// CalorieAte. java

import java.util.*;

public class CalorieAte

{

{public static void main(String args(])

{

// create object to the Scanner class to read input

// from the user

Scanner input = new Scanner(System.in);

// Declare the required variables

Int cookiesAte = 0;

// assign the values to the declare variables

Int totalCookies_Bag = 40;

Int total_servings = 10;

Int caloriesperserving = 300;

// calculation of cookies present in each serve

int cookiesPer_serving = totalCookies_Bag / total_servings;

// calculation of calories present in each cookie

Int caloryInEachCookie = caloriesperserving / cookiesPer_serving;

// prompt the user to enter the number of cookies eaten

System.out.println("Welcome to calories ate calculator!");

System. out.print("Enter number of cookies you eaten: ");

cookiesAte = input.nextInt();

// calculate the calories eaten by total cookies eaten

double caloriesConsumed = cookiesAte * caloryInEachCookie;

// display the values.

System.out.println("Total cookies present in each serve: " + cookiesPer_serving);

System.out.println("Total calories present in each cookie: " + caloryInEachCookie);

System.out.println("Therefore, calories you eaten is " + caloriesConsumed);

}

}

Sample output:

Welcome to calories ate calculator!

Enter number of cookies you eaten: 12

Total cookies present in each serve: 4

Total calories present in each cookie: 75

Therefore, calories you eaten is 900.0

Program code to copy:

// CalorieAte.java

import java.util.*;

public class CalorieAte

{

     public static void main(String args[])

     {

          // create object to the Scanner class to read input

          // from the user

          Scanner input = new Scanner(System.in);

          // Declare the required variables

          int cookiesAte = 0;

          // assign the values to the declare variables

          int totalCookies_Bag = 40;

          int total_servings = 10;

          int caloriesperserving = 300;

          // calculation of cookies present in each serve

          int cookiesPer_serving = totalCookies_Bag / total_servings;

          // calculation of calories present in each cookie

          int caloryInEachCookie = caloriesperserving / cookiesPer_serving;

          // prompt the user to enter the number of cookies eaten

          System.out.println("Welcome to calories ate calculator!");

          System.out.print("Enter number of cookies you eaten: ");

          cookiesAte = input.nextInt();

          // calculate the calories eaten by total cookies eaten

          double caloriesConsumed = cookiesAte * caloryInEachCookie;

          // display the values.

          System.out.println("Total cookies present in each serve: " + cookiesPer_serving);

          System.out.println("Total calories present in each cookie: " + caloryInEachCookie);

          System.out.println("Therefore, calories you eaten is " + caloriesConsumed);

     }

}

Related Questions