Question

In: Computer Science

Java question: A rumor spreads in the following way: a person picks at random another person...

Java question: A rumor spreads in the following way: a person picks at random another person to inform of the rumor. If that person hasn't already been informed of the rumor, that person starts spreading the rumor as well. If that person had already been informed of the rumor, neither person spreads this rumor any further. Starting with 999 people who don't know the rumor and one who does and starts spreading it (1000 people in total), write a class in Java named RumorSpreading that simulates this situation and then prints the final percentage of the population that ends up knowing the rumor. Assume that we do NOT consider the case where more than one person spreads the rumor simultaneously.

Solutions

Expert Solution

Here's the solution to the given problem. I have added comments so that it's easier for you to understand. Keep learning :)

RumorSpreader.java:


import java.util.Random;

public class RumorSpreader {
  
   static Random rd = new Random();
  
   public static int generateRandomOtherThanSelf(int x) {
       int random = rd.nextInt(1000);
       while(random==x)
           random = rd.nextInt(1000);
       return random;
   }

   public static void main(String[] args) {
       int arr[] = new int[1000];
       arr[0] = 1;
       int next,i;
       while(true) {
           int flag = 0;
           // Iterate through all the people
           // State 0 denotes that the person doesn't know the rumor.
           // State 1 denotes that the person knows the rumor and can spread it.
           // State 2 denotes that the person knows the rumor and can't spread it.
           for (i=0; i<1000; i++) {
               // If anyone knows the rumor, spread it to a random person
               if(arr[i] == 1) {
                   // flag 1 denotes that we found a person who can spread the rumor.
                   flag = 1;
                   next = generateRandomOtherThanSelf(arr[i]);
                   // If the next person didnt know the rumor already, he'll spread further
                   if(arr[next] == 0) {
                       arr[next] = 1;
                   }
                   // If the next person knew the rumor already,
                   // both next nd current will stop participating in spreading the rumour futher
                   else if(arr[next] == 1) {
                       arr[next] = 2;
                       arr[i] = 2;
                   }
                   break;
               }
           }
           // If we didnt find any person, who can spread the rumor, break
           if(flag==0)
               break;
       }
      
       // Finally, count the number of people who know the rumor
       int count=0;
       for (i=0; i<1000; i++) {
           if(arr[i] != 0)
               count++;
       }
       System.out.println("Percentage of people knowing the rumor: " + ((double)count/1000) * 100);
   }

}

Output:


Related Solutions

Question 1. Any act by which one person gives to or receives from another person information...
Question 1. Any act by which one person gives to or receives from another person information about that person's needs, desires, perceptions, knowledge, or affective states is called Communication Information Feedback Knowledge Question 2. The voluntary commitment of businesses to include in their corporate practices the economic, social, and environmental criteria/actions, which are above and beyond legislative requirements is known as Corporate Image Corporate Structure Corporate Social Responsibility Corporate Policy Question 3. Individuals or group of individuals who are interested...
Or to put the question another way - what is the result of a proton-positron collision,...
Or to put the question another way - what is the result of a proton-positron collision, or an up quark-charm antiquark collision, etc.? As far as I know, annihilation happens only between particles of opposite charge and same mass, but perhaps I am wrong? And if the types of annihilation mentioned above cannot happend, what are the reasons? Thank you.
To the nearest tenth, how many days will it be before the rumor spreads to half the carrying capacity? For the following exercises, use this scenario: The equation N(t) = 1200/1 + 199e−0.625t models..
For the following exercises, use this scenario: The equation N(t) = 1200/1 + 199e−0.625t models the number of people in a school who have heard a rumor after t days.To the nearest tenth, how many days will it be before the rumor spreads to half the carrying capacity?
B. Which of the following statements if made by one person to another is an offer...
B. Which of the following statements if made by one person to another is an offer and which are not? Be sure to state reasons for your response. Statement #1 – “I am thinking about selling my laptop computer and if I do, I probably would want at least $250 dollars for this lovely piece of equipment.” Statement #2 – “I am selling my laptop computer and the asking price is $250 dollars.” Statement #3 – “ Are you interested...
Which of the following is not a key way in which business organizations compete with one-another?
Which of the following is not a key way in which business organizations compete with one-another?
Consider the following experiment. Each participant interacted for an hour with another person who was actually...
Consider the following experiment. Each participant interacted for an hour with another person who was actually a research confederate (an actor working for the researcher). The confederate either presented herself as similar or dissimilar to the participant. After this interaction, both persons agreed to return 1 week later for another session with each other. When the real participants returned, they were falsely informed that the person they had met the week before had died. The researchers then compared how long...
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed:...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object with the current time as its seed. Constructs a Random object with a specified seed. Returns a random int value. Returns a random int value between 0 and n (exclusive). Returns a random long value. Returns a random double value between 0.0 and 1.0 (exclusive). Returns...
Scenario B: Consider the following experiment. Each participant interacted for an hour with another person who...
Scenario B: Consider the following experiment. Each participant interacted for an hour with another person who was actually a research confederate (an actor working for the researcher). The confederate either presented herself as similar or dissimilar to the participant. After this interaction, both persons agreed to return 1 week later for another session with each other. When the real participants returned, they were falsely informed that the person they had met the week before had died. The researchers then compared...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT