Question

In: Computer Science

I'm generating a random number every 10 second; In arraylist I'm Storing and Updating the number...

I'm generating a random number every 10 second; In arraylist I'm Storing and Updating the number every 10 second when I generate new number. However, my goal to call the update number from arraylist in another class main method. I would apperciate any help.

//this is the class I'm generating a random number every 10 second and Storing and Updating the number in arraylist.

package com.singltondesignpattern;

import java.util.ArrayList;

import java.util.Random;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

public class EverySecond {

public static int genrand() {

Random number =new Random();

int rand=number.nextInt(1000);

return rand;

}

public static void getrand(int rand) {   

ArrayList <Integer> randomstorage = new ArrayList<Integer>();

randomstorage.add(rand);

System.out.println("Array value "+randomstorage);

}

public static void main(String[] args) throws Exception {

Runnable helloRunnable = new Runnable() {

public void run() {

int CurrentNum=genrand(); //create a random number

System.out.println("generate number ==== "+CurrentNum);

getrand(CurrentNum); //update it in array list

}

};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);

   }

}

//this is the class I want to get the update number from the arraylist

public class getTheUpdate {

public static void main(String[] args) throws Exception {

EverySecond everySecond = new EverySecond();

everySecond.getrand(0);

}

}

Solutions

Expert Solution

//EverySecond.java class

import java.util.ArrayList;

import java.util.Random;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

public class EverySecond {
//declare the arraylist publically
static ArrayList<Integer> randomstorage = new ArrayList<Integer>();
public static int genrand() {

Random number = new Random();
//generate random number
int rand = number.nextInt(1000);
//return generated random number
return rand;

}

public static void getrand(int rand) {
//initialise the arraylist
randomstorage = new ArrayList<Integer>();
//add new generated random number in arraylist
randomstorage.add(rand);

System.out.println("Array value " + randomstorage);

}

public static void main(String[] args) throws Exception {

Runnable helloRunnable = new Runnable() {

public void run() {

int CurrentNum = genrand(); //create a random number

System.out.println("generate number ==== " + CurrentNum);

getrand(CurrentNum); //update it in array list

}

};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);

}

}

//getTheUpdate.java Where you can acess the EverySecond.java

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class getTheUpdate {

public static void main(String[] args) throws Exception {
//create object of EverySecond class
EverySecond everySecond = new EverySecond();

//or you can create another method to generate random number every 10 second and pass the number to update in arraylist
Runnable helloRunnable = new Runnable() {

public void run() {

int CurrentNum = everySecond.genrand(); //create a random number

// System.out.println("generate number ==== " + CurrentNum);
everySecond.getrand(CurrentNum); //update it in array list
System.out.println("This is updated number from ArrayList: " + everySecond.randomstorage.get(0));
}

};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);
}
}


Related Solutions

Question: A robot is given 1 random instruction every second for 10 seconds. Each instruction will...
Question: A robot is given 1 random instruction every second for 10 seconds. Each instruction will be either a command to move 1 inch to the left, or 1 inch to the right. The likelihoods of the instruction being a command to move to the left and right are 'p' and '1-p', respectively. Find: a) What is the expectation of the robot's final position with respect to its starting position. b) What is the variance we might expect for observations...
A number is selected at random from the set {1,2,...,20}. Then a second number is selected...
A number is selected at random from the set {1,2,...,20}. Then a second number is selected randomly between 1 and the first number selected. What is the probability that the second number is 5?
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message. Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball
Every day there is a random number of pencils in the mail. Jerry places eraser caps...
Every day there is a random number of pencils in the mail. Jerry places eraser caps on each one that arrives. Jerry has enough eraser caps to cap 600 pencils. Each day, Jerry gets at least two pencils, but not more than a dozen, and he always receives an even number of pencils. Among the possible numbers of pencils Jerry might receive, all are equally likely. Moreover, different days are independent. What is the approximate probability that Jerry's amount of...
Consider a one-dimensional random walker (using numpy) that can move every second. With probability pl =...
Consider a one-dimensional random walker (using numpy) that can move every second. With probability pl = 1/3 it moves to the left, with probability pr = 1/3 it moves to right, and with probability pr = 1/3 it rests and does not move. Assuming at time t = 0, the random walker is at x = 0, plot (using matplotlib) the probability density function and the cumulative probability function for t = 10, t = 100, and t = 1000...
A number between 1 and 10 is chosen at random . What is the probability of...
A number between 1 and 10 is chosen at random . What is the probability of getting a multiple of 5 or an even number?
<< Using R code >> Set seed number as "12345" every time you generate random numbers....
<< Using R code >> Set seed number as "12345" every time you generate random numbers. For each answer, use # to explain if necessary. 2) Generate a data.frame "D" with 3 variables. The 1st variable "v1" has 50 number of N(5,3^2) (normal with mean 5, standard deviation 3) The 2nd variable "v2" has 50 number of exp(5) (exponential with parameter 5) The 3rd variable "v3" has 50 random characters from lower case alphabets. 2-1) Rename the variable from "v1",...
10. Write a for loop which will print every number evenly divisible by 13 between 1...
10. Write a for loop which will print every number evenly divisible by 13 between 1 and 100, inclusive. Your loop will only increment by 1 each loop so you need an if test to see if each number should be printed. Put each number output on the same line, 1 space apart. 11. Write a while loop which will prompt the user for a number and accept their input and then display this message (assuming number input is 3)...
A certain gym selected a random sample of 10 customers and monitored the number of times...
A certain gym selected a random sample of 10 customers and monitored the number of times each customer used the workout facility in a​ one-month period. The data are shown in the table below. The​ gym's managers are considering a promotion in which they reward frequent users with a small gift. They have decided that they will only give gifts to those customers whose number of visits in a​ one-month period is 1 standard deviation above the mean. Find the...
Compute ?(?) for the following random variable ?: ?=Number of tosses until all 10 numbers are...
Compute ?(?) for the following random variable ?: ?=Number of tosses until all 10 numbers are seen (including the last toss) by tossing a fair 10-sided die. To answer this, we will use induction and follow the steps below: Let ?(?) be the expected number of additional tosses until all 10 numbers are seen (including the last toss) given ? distinct numbers have already been seen. 1. Find ?(10). ?(10)=? 2. Write down a relation between ?(?) and ?(?+1). Answer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT