Question

my goal is to generate a random number every 10 seconds and store in arraylist. when...

my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number***

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Main
{
public static void main(String[] args) throws Exception
{
  
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
  
//schedule thread for evry 10 seconds
  
//executor.schedule(new sampleCallableExample(), 1, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(new sampleCallableExample(), 0, 10, TimeUnit.SECONDS);
System.out.println("array size:"+globals.Update.size());
for(int x=0;x {
  
System.out.println("Current Num = " + globals.Update.get(x));
  
}
  
}
}

import java.util.ArrayList;
public class globals
{
public static ArrayList Update = new ArrayList();
  
}

sampleCallableExample.java :

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.Random;
class sampleCallableExample implements Runnable
{
public static int genrand()
{
Random number = new Random();
//generate random number
int rand = number.nextInt(1000);
//return generated random number
return rand;
}
public Object call() throws Exception
{
//ArrayList Update = new ArrayList();
int CurrentNum = genrand();
System.out.println("generate number ==== " + CurrentNum);
globals.Update.clear();
globals.Update.add(CurrentNum);
System.out.println("Number added");
return globals.Update;
}

// As seen run wraps call object
public void run()
{
try
{
Object o = call();
//System.out.println("Returned " + o);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public sampleCallableExample()
{
try
{
run();
  
  
}
catch(Exception e)
{
}
  
}
  
}   

Current Output:

generate number ==== 809

Number added

array size:1

Current Num = 809

generate number ==== 6

Number added

generate number ==== 107

Number added

It should be :

generate number ==== 809

Number added

array size:1

Current Num = 809

generate number ==== 6

Number added

array size:1

Current Num = 6

generate number ==== 107

Number added

array size:1

Current Num = 107

NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number***I would apperciate any help.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the update code according to the sample output:

================================================

Main.java

================================================

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

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

       ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

//schedule thread for evry 10 seconds

//executor.schedule(new sampleCallableExample(), 1, TimeUnit.MILLISECONDS);
       executor.scheduleAtFixedRate(new sampleCallableExample(), 0, 10, TimeUnit.SECONDS);
   }
}

class globals {
   public static ArrayList Update = new ArrayList();

}

==============================================

sampleCallableExample.java

==============================================

import java.util.Random;

public class sampleCallableExample implements Runnable {
   public static int genrand() {
       Random number = new Random();
//generate random number
       int rand = number.nextInt(1000);
//return generated random number
       return rand;
   }

   public Object call() throws Exception {
//ArrayList Update = new ArrayList();
       int CurrentNum = genrand();
       System.out.println("generate number ==== " + CurrentNum);
       globals.Update.clear();
       globals.Update.add(CurrentNum);
       System.out.println("Number added");
       System.out.println("array size:" + globals.Update.size());
       System.out.println("Current Num = " + globals.Update.get(0));
       return globals.Update;
   }

// As seen run wraps call object
   public void run() {
       try {
           Object o = call();
//System.out.println("Returned " + o);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   public sampleCallableExample() {
       try {
           run();

       } catch (Exception e) {
       }

   }

}

Sample output:

generate number ==== 478 Number added array size:1 Current Num = 478 generate number ==== 933 Number added array size:1 Curre

I hope this helps you!!

Thanks

Add a comment
Know the answer?
Add Answer to:
my goal is to generate a random number every 10 seconds and store in arraylist. when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...

    package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...

  • How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and...

    How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph? I have these methods: //This one receives one size(n) parameter    public static ArrayList<Integer> RandomArray(int n){               ArrayList<Integer> randomArray = new ArrayList<Integer>(n);               Random rand = new Random(); //--- random number        rand.setSeed(System.currentTimeMillis());               for(int i = 0; i < n; i++ ) {            //loop for creating...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT