Question

In: Computer Science

Code the following: When an employee has worked for Anderson & Beckett for 30 years or...

  1. Code the following:
  1. When an employee has worked for Anderson & Beckett for 30 years or more, 100 shares of preferred stock are awarded; otherwise no shares are awarded. Code this if … else control structure using the conditional operator (ternary operator). The variables yearsEmployed and preferredShares are already declared.

  1. Code a do-while loop that keeps printing the message “GOTTA FIND MY CAR KEYS!” as long as the keys haven’t been found. Assume foundKeys and input (for the Scanner class) are already declared. The variable foundKeys has been initialized to ‘n’ and it’s the loop-control variable.

  1. Re-code 1b using a while loop.

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

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

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

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

  1. 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).

  1. Code a for loop 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.

  1. Re-code the following as a do-while loop with a switch statement. Assume cruise is declared and has been set to true, so it can enter the loop. Assume choice and destination are already declared.

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);

Solutions

Expert Solution

please check out the solution... please do a comment for any doubt... thank you...

1.a> Shares will be awarded if the employee works for >=30 years...

using ternary operator, it can be written as ....

preferredShares = (yearsEmployed>=30)? 100: 0;
 

1.b>

do{
    System.out.println("GOTTA FIND MY CAR KEYS!");   //printing the given massage
    foundKeys=scnr.nex().charAt(0);    //taking input for foundKeys, here scnr is the object of Scanner class
    }while(foundKeys!='n');   //loop will continue until foundKeys not-equals to n

1.c>

while(foundKeys != 'n'){   //i.e. until key is found
    System.out.println("GOTTA FIND MY CAR KEYS!");   //printing the given massage
    foundKeys=scnr.next().charAt(0);    //taking input for foundKeys, here scnr is the object of Scanner class
    }  //end of while

1.d>

in this case, there is a problem... for switch case calories is divided by 100. so the number of digits in calorie decreases by 2 digits. now if the input is 2560 calories... it should come under "Calorie intake is either insufficient or too much!" case... but after divided by 100, new calories becomes 25.... in switch case it comes under “Calorie intake ok if active.” case... this condition happens because switch cannot use float value...

switch(calories/100){   // calories is divided by 100
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17: 
    case 18:
        System.out.println("Diet is on target.");
        break;
    case 20:
    case 21:
    case 22:
    case 23:
    case 24: 
    case 25:
        System.out.println("Calorie intake ok if active.");
        break;
    default:
        System.out.println("Calorie intake is either insufficient or too much!");
    }//end of switch

1.e>

if(calories>=1200 && calories<==1800)    //When calories are 1200 through 1800
    System.out.println("Diet is on target.");
else if(calories>=2000 && calories<==2550)  //When calories are 2000 through 2550
    System.out.println("Calorie intake ok if active.");
else  //When calories are any other value
    System.out.println("Calorie intake is either insufficient or too much!"); 

1.f>

for(int i=1;i<=3;i++){  //loop will continue for 3 times
    if(i==3)  //if it is the last attempt
        System.out.print("No more attempts left!");
    calories=scnr.nextInt();  // taking input... scnr is the object of scanner class
    switch(calories/100){   // calories is divided by 100
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17: 
    case 18:
        System.out.println("Diet is on target.");
        break;
    case 20:
    case 21:
    case 22:
    case 23:
    case 24: 
    case 25:
        System.out.println("Calorie intake ok if active.");
        break;
    default:
        System.out.println("Calorie intake is either insufficient or too much!");
    }//end of switch
   }//end of for

1.g> updated the given code to do while

Note: in the print statement, there was "%n", it will be "\n" i.e newline... which is been updated...

do    //updated to do while loop

{

               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

}while(cruise);    //END do-while if cruise is true

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

Related Solutions

Years ago in Seattle I worked for an insurance company with just one Jewish employee, who...
Years ago in Seattle I worked for an insurance company with just one Jewish employee, who was a good friend of mine. He invented Jewish holidays, taking days off several times a year. As the only other employee at all familiar with Judaism, I could have tattled on him or kept silent and been disloyal to my employer. I kept silent. Was that the ethically correct choice?
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
Daryl Kearns saved $240,000 during the 30 years that he worked for a major corporation. Now...
Daryl Kearns saved $240,000 during the 30 years that he worked for a major corporation. Now he has retired at the age of 60 and has begun to draw a comfortable pension check every month. He wants to ensure the financial security of his retirement by investing his savings wisely and is currently considering two investment opportunities. Both investments require an initial payment of $160,000. The following table presents the estimated cash inflows for the two alternatives: Year 1 Year...
Analyze the following situation: Martha has worked for John for two years. About 6 months ago,...
Analyze the following situation: Martha has worked for John for two years. About 6 months ago, John asked Martha out to dinner. They had a good time together and agreed that they had some real interests in common outside of work. The pair dated for two months. Martha initially liked John, but he was beginning to get annoying. John called her all the time, was very pushy about her seeing him, and wanted to control all aspects of her life,...
Rossi Industries has payroll processes as described in the following paragraphs: When a new employee is...
Rossi Industries has payroll processes as described in the following paragraphs: When a new employee is hired, the human resources department completes a personnel action form and forwards it to the payroll department. The form contains information such as pay rate, number of exemptions for tax purposes, and the type and amount of payroll deductions. When an employee is terminated or voluntarily separates from Rossi, the human resources department completes a personnel action form to indicate separation and forwards it...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
Leon has worked for a small tool and die company for several years. The owner is...
Leon has worked for a small tool and die company for several years. The owner is retiring and Leon has the opportunity to purchase the business. However, he plans to retire in 10 years, and he knows that neither of his children has any desire to work in the business. What appears to be Leon’s key consideration in choosing an ownership structure? a. Business control and transfer of ownership b. Ease of start-up and administration c. Management structure d. Legal...
A study compared the number of years a person has worked for the same company (X)...
A study compared the number of years a person has worked for the same company (X) with the person’s salary in thousands of dollars per year (Y). The data for nine employees appear in the following table. Use the data to answer the questions. Years Annual Salary 5 24 8 40 3 20 6 30 4 50 9 40 7 35 10 50 Answer these questions based on the research scenario and the data in this table. What is the...
Camila is 45 years old and has worked as a Food Supplement Consultant at Healthy &...
Camila is 45 years old and has worked as a Food Supplement Consultant at Healthy & Fit Inc. (the "Company") for the last 9.5 years. The Company specializes in the sale of food supplement and vitamin products in Ontario and has an annual payroll of $3 millions. The Company hires consultants like Camila throughout the province and assigns them a specific and exclusive geographical area (e.g. North York, Downtown Toronto, Barrie, etc.). Camila and the other consultants sell the Company’s...
John Brenner had worked for the same domestic appliance retailer for over twenty years when he...
John Brenner had worked for the same domestic appliance retailer for over twenty years when he saw an advertisement from an East European manufacturer which wanted to start selling its brand of appliances in the UK. John answered the advertisement and spent a year preparing and negotiating with the manufacturer. The manufacturer was not keen to have all its distribution done by a new and untried company. In the end, John agreed to set up a company called Brenner Refrigeration...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT