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

90.2K

Verified Solution

Question

Programming

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

//this is the class I'm generating a random number every 10second 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 randomstorage = newArrayList();

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 thearraylist

public class getTheUpdate {

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

EverySecond everySecond = new EverySecond();

everySecond.getrand(0);

}

}

Answer & Explanation Solved by verified expert
4.1 Ratings (801 Votes)
EverySecondjava classimport javautilArrayListimport javautilRandomimport javautilconcurrentExecutorsimport javautilconcurrentScheduledExecutorServiceimport javautilconcurrentTimeUnitpublic class EverySecond declare the arraylist publicallystatic ArrayList randomstorage newArrayListpublic static int genrand Random number new Randomgenerate random numberint rand    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students