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 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
System.out.println("Start Loop : ");
executor.scheduleAtFixedRate(new sampleCallableExample(), 0, 10, TimeUnit.SECONDS);
System.out.println("array size:" + globals.Update.size());
for (int x = 0; x < globals.Update.size(); x++) {
System.out.println("Current Num = " + globals.Update.get(x));
}
System.out.println("Close Loop : ");
}
}
package com.Test;
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) {
}
}
}
package com.Test;
import java.util.ArrayList;
public class globals {
public static ArrayList Update = new ArrayList();
}
The Current output is:
Start Loop :
generate number ==== 992
Number added
array size:1
Current Num = 992
Close Loop :
generate number ==== 407
Number added
generate number ==== 32
Number added
However, It should be:
Start Loop :
generate number ==== 992
Number added
array size:1
Current Num = 992
Close Loop :
Start Loop :
generate number ==== 407
Number added
array size:1
Current Num = 407
Close Loop :
Start Loop :
generate number ==== 32
Number added
array size:1
Current Num = 32
Close Loop :
//there are some changes in Main class and sampleCallableExample class
//Main.java
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.scheduleAtFixedRate(new sampleCallableExample(), 0, 10, TimeUnit.SECONDS);
// System.out.println("array size:" +
globals.Update.size());
//
// for (int x = 0; x < globals.Update.size(); x++) {
//
// System.out.println("Current Num = " +
globals.Update.get(x));
//
// }
//
// System.out.println("Close Loop : ");
}
}
//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 void call() throws Exception {
//there is change in code
//we have write all the statements, which needs to be print
globals.Update.clear();
System.out.println("Start Loop : ");
// ArrayList Update = new ArrayList();
int CurrentNum = genrand();
System.out.println("generate number ==== " + CurrentNum);
globals.Update.add(CurrentNum);
System.out.println("Number added");
System.out.println("array size:" + globals.Update.size());
for (int x = 0; x < globals.Update.size(); x++) {
System.out.println("Current Num = " + globals.Update.get(x));
}
System.out.println("Close Loop : ");
// return globals.Update;
//we don't required any object in return
}
// As seen run wraps call object
public void run() {
try {
// Object o = call();
//just call method and make its return datatype void
call();
// System.out.println("Returned " + o);
} catch (Exception e) {
e.printStackTrace();
}
}
//there is no use of constructor that call method one time
extra
// public sampleCallableExample() {
//
// try {
//
// run();
//
// } catch (Exception e) {
//
// }
//
// }
}
//globals.java
import java.util.ArrayList;
public class globals {
public static ArrayList Update = new ArrayList();
}
//output
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 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...
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...
. 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...
Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("How many numbers do you want to input?"); int num = input.nextInt(); ArrayList<Double> d = new ArrayList<Double>(num); for(int i = 0 ; i < num; i++){ System.out.print("Enter...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
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 =...
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,...
Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...
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...