Question

d. Assumecalories is already declared as an integer.  Code a fall-through switchstatement by dividing calories by 100...

d. Assumecalories is already declared as an integer.  Code a fall-through switchstatement by dividing calories by 100 in the controlling expression of the switch header, so the following messages print:

When caloriesare 1200 through 1800:  “Diet is on target.”

When caloriesare 2000 through 2550:  “Calorie intake ok if active.”

When caloriesare any other value:  “Calorie intake is either insufficient or too much!”

e. Re-code 1d using double-selection ifs.  You’ll use a conditional logical operator to join the sets of relational conditions (choose the right one).  

f. Code a forloop with the switch structure from 1d and allow 3 attempts.  Prompt the user to enter calories.  When the number entered is in the correct range exit the loop (not the program) after one of the messages in 1d is printed.  Print the message “No more attempts left!” when it is the last attempt.

g. Re-code the following as a do-whileloop with a switchstatement.  Assume cruiseis declared and has been set to true, so it can enter the loop.  Assume choice and destinationare already declare

while(cruise)

{

            System.out.printf(“%nChoose a number from 1 through 4 to find out “

+ “which cruise you have won:  “);

            choice = input.nextInt();

            if(choice == 1)

            {

                        destination = “Bahamas”;                                                    

            }

            else

            {          if(choice == 2)

                        {

                                    destination = “British Isles”;                                                                        

                        }

                        else

                        {          if(choice == 3)

                                    {

                                                destination = “Far East”;                  

}

else

                        {          if(choice == 4)

{

                                                destination = “Amazon River”;

}

else

{          

System.out.printf(“%nInvalid choice!  Enter “

+ “5 to continue or 0 to exit:  ”);

                                                                        choice = input.nextInt();

                                    

                                                }//END if choice = 4 else NOT = 4

                                    }//END if choice = 3 else NOT = 3

                        }//END if choice = 2 else NOT = 2

            }//END if choice = 1 else NOT = 1  

            if(choice >= 0 && choice < 5)

            {

                        cruise = false;

            }//END if choice from 1-4

}//END while cruise is true

System.out.printf(“%nYou have won a cruise to the %s!”, destination);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW PLEASE REPOST REMAINING QUESTIONS

ANSWER:

A)

#include<iostream>
#include <cmath>
using namespace std;
int main(){
   int calories;
   //requesting calories from user
   cout<<"Enter calories: ";
   cin>>calories;
  
   //as the switch statement only accepts integer
   //make the values from 2551 to 2599 more than 2600
   if(calories>2550 && calories<2600)
       calories+=50;
  
   //switch statement with calaries/100 as header
   switch(calories/100){
       case 12 ... 18: cout<<"Diet is on target\n";
                       break;
                      
       case 20 ... 25: cout<<"Calorie intake ok if active\n";
                       break;
                      
       default: cout<<"Calorie intake is either insufficient or too much\n";
                       break;  
   }
   return 0;
}

PGM END

B)

//############################ PGM START ##########################################

#include<iostream>
using namespace std;

int main(){
   int calories;
   //requesting calories from user
   cout<<"Enter calories: ";
   cin>>calories;
   //using if else statement to compare and print results
   if(calories>=1200 && calories<=1800){
       cout<<"Diet is on target\n";
   }else{
       if(calories>=2000 && calories<=2550){
           cout<<"Calorie intake ok if active\n";
       }else{
           cout<<"Calorie intake is either insufficient or too much\n";
       }
   }
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
d. Assumecalories is already declared as an integer.  Code a fall-through switchstatement by dividing calories by 100...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • d. Assumecalories is already declared as an integer. Code a fall-through switchstatement by dividing calories by...

    d. Assumecalories is already declared as an integer. Code a fall-through switchstatement by dividing calories by 100 in the controlling expression of the switch header, so the following messages print: When caloriesare 1200 through 1800: “Diet is on target.” When caloriesare 2000 through 2550: “Calorie intake ok if active.” When caloriesare any other value: “Calorie intake is either insufficient or too much!” e. Re-code 1d using double-selection ifs. You’ll use a conditional logical operator to join the sets of relational...

  • Need some guidance with the following problems as I am new to Java programming. Any assistance...

    Need some guidance with the following problems as I am new to Java programming. Any assistance is greatly appreciated. Thanks Using printf and specifiers for all print instructions: 1.            Code the following: a.            10% is stored in a variable called discount when the customer is a student; otherwise, it stores 0%. Code this if … else control structure using the conditional operator (ternary operator). The variables discount and student have already been declared. Assume student is a boolean variable. b.           ...

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT