A friend of mine likes to eat peanut M&M’s. Each morning he randomly selects a number of Peanut M&M’s between 1 and 12 to eat during the day from a bag which originally contained 100 Peanut M&M’s.(Hint: Looks like a randi(12) might be used here!). The question is on what day will my friend have to open a new bag of M&M’s for his daily habit. That means that if on day t the last M&Ms are eaten, then on day t+1 he has to start a new bag. By the way, on the last day if the random integer is equal to or greater than the number of M&M’s left, then he eats all that are left.
Notes:
(1) For our previous problems, we have ONLY used for/end loops. That was because we knew EXACTLY how many times to loop through the code. In this problem, you are to find the number of days (aka: loops), so a while-end is used.
(2) REMEMBER that for a for-end loop, the loop index variable is incremented for you. In a while loop, YOU must increment the loop index variable.
(3) So MOST days you will have the number of Peanut M&M’s left at the beginning of the day as P(t)and at the end of the day as: P(t+1)=P(t)–randi(12)
(4) The way to deal with the last day is that when P(t+1)<=0 set P(t+1)=0.
(5) Don’t forget to initialize your “t”.
Hi,
The problem has been solved. While loop has been used to solve the problem. Everyday a random function "rand(12)" will give the value from 0-12 days. A screenshot has been attached for better understanding.
Screenshot:
Code:
-------------------
import java.util.*;
class Peanut{
public static void main(String[] args){
int total=100;
Random random = new
Random();
int t=1;
while(total>0){
int rand=
random.nextInt(12);
total=
total-rand;
t++;
//System.out.println(total+" -> "+t);
}
System.out.println(t+" day will my friend have to open a new bag of M&M’s for his daily habit");
}
}
-------------------
The problem has been resolved, I hope you will like the solution. If you have any question or query regarding this problem or other, please comment below and give the positive rating.
Thank You!! Happy Learning!! Keep Chegging!!
A friend of mine likes to eat peanut M&M’s. Each morning he randomly selects a number...
CHECK YOUR UNDERSTANDING 4. You are helping a person to eat when he begins to choke. He is making a high- pitched wheezing sound, and it is difficult for him to answer you when you ask If he Questions for Review 1. What should you do if a fi re alarm sounds while you are at work? needs help. What should you do? a. Perform cardiopulmonary resuscitation (CPR). b. Call for help immediately, and then begin administering back blows and...
the mean number of sick days an employee takes per year is believed to be about 10. Members of a personnel department do not believe this figure. They randomly survey B employees. The number of sick days that the mean number is about 107 Conduct a hypothesis test at the 5% level. they took for the past year are as follows: 12; 6; 14 5 11; 6;10. Lt X the number of sick days they took for the past year....
Required information
[The following information applies to the questions
displayed below.]
Consider the following narrative describing the process of going
to class:
Larry awoke to his alarm clock buzz. He got up and dressed for
the day. Then, he ate a hearty breakfast of oatmeal, toast, orange
juice, and coffee. He grabbed his books and prepared to leave for
school. Before he left home, he checked the weather. If it looked
like rain, he put on a jacket and took...
ppsMDb Youlube M Inbox- navee Question 1 6 marks he points A(a,b) and C(c,d) are the opposite vertices of square ABCD in the Cartesian plane What are the coordinates of the other two vertices? Be sure to cover all cases of where A and C could bel (a) 4 marks (b) What is the area of the square ABCD? 2 marks P A(ab) (Hint: you may find the following construction useful. Calculate both p+q and p-q) C(c,d) g Minh Pham...
INSTRUCTIONS 1. Prompt the user for the number of participating units and expected peak demand hours 2. Create two arrays: O A String array called unitstatus, with an array element count equal to the number of participating units o An int array called unitCredit, with an array element count equal to the number of participating units O HINT: make sure you understand the difference between array size (element count) and element index (position) 3. Initialize the arrays, setting each element...
no need to show any work, just the letters from each
queastion. thanks in advance
Question 32 10 pts You can concatenate stric ts by using the concat method of the String and Question 33 You can use the thod to replace an mat aspect location a l Question 34 10 pts You can use this area D Question 35 10 pts You use this method to determine the number Question 36 Most of the stwing comparison methods are case...
For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...
Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...
10. Uneven cash flows A Aa E A series of cash flows may concept of the time valu s necessarily be an annuity. Cash flows can also be uneven and variable in amount, but the Il continue to apply Consider the following case: The Purple Lion Beverage Company expects the following cash flows from its manufacturing plant in Palau over the next six years: Year 1 Year 2 Annual Cash Flows Year 4 $180,000 $450,000 Year 3 Year 6 $375,000...
help
CASE STUDY IN CARE OF DIVERSE CLINIS HEART FAILURE SCENARIO You are the nurse working in an internal medicine office or a thing to pl ay yow patient is 70-yearold JM, a man who has been coming to the cline for several years for management or coronary artery disease and hypertension. A cardiac catheterination des a year po showed 50% tesis of the circumflex commary artery. He has had episodes of dames for the months ldema, decreased exercise tolerance,...