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...
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 =...
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...
. 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...
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...
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,...
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...
The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter; groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream"); System.out.println("Number of items...
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....