Question
Hello, I have a problem with this code I can't figure out. If you keep running the code it will run fine at certain points
Hello, I have a problem with this code I can't figure out. If you keep running the code it will run fine at certain points (printing out each functions end time) but then it will give me this error:
Sorry for the mess but Chegg wouldn't let me post the code if I didn't condense it.
Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0 at java.base/java.util.ArrayList.add(ArrayList.java:455) at java.base/java.util.ArrayList.add(ArrayList.java:467) at Q1_PLV2.function5(Q1_PLV2.java:207) at Q1_PLV2$5.run(Q1_PLV2.java:68) at java.base/java.lang.Thread.run(Thread.java:832)
be it in one function or another. Can you help me fix this?
Thank you!
Java Code:
random import and arraylist import
public class Q1_PLV2 { public static Thread thr1; public static Thread thr2; public static Thread thr3; public static Thread thr4; public static Thread thr5; static int sum = 0; static float sumGPA = 0, sumAg = 0; static int countGPA = 0, countAGE = 0; static long uniqueID = 1111;
public static void main(String[] args) { ArrayList list = new ArrayList(); thr1 = new Thread(new Runnable() { public void run( ) { try { function1(list); } catch (InterruptedException e) { e.printStackTrace(); } } }); thr2 = new Thread(new Runnable() { public void run() { try { function2(list);} catch(InterruptedException e) { e.printStackTrace(); } } }); thr3 = new Thread(new Runnable() { public void run() { try { function3(list);} catch(InterruptedException e) { e.printStackTrace(); } } }); thr4 = new Thread(new Runnable() { public void run() { try { function4(list); } catch(InterruptedException e) { e.printStackTrace(); } } }); thr5 = new Thread(new Runnable() { public void run() { try { function5(list); } catch(InterruptedException e) { e.printStackTrace(); } } }); thr1.start(); thr2.start(); thr3.start(); thr4.start(); thr5.start(); try { thr1.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { thr2.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { thr3.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { thr4.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { thr5.join(); } catch (InterruptedException e1) { e1.printStackTrace(); }} public static void function1(ArrayList list) throws InterruptedException { long startTime = System.nanoTime(); for (long i = 0; i < 10000; i++) { Student s = new Student(rngGPA(), rngAGE(), rngID()); list.add(s); sumGPA = sumGPA + s.getGpa(); countGPA++; sumAg = sumAg+ s.getAge(); countAGE++; } long endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.; System.out.println("Function 1 Time: " + time + " seconds"); } public static void function2(ArrayList list) throws InterruptedException { long startTime = System.nanoTime(); for (long i = 0; i < 10000; i++) { Student s = new Student(rngGPA(), rngAGE(), rngID()); list.add(s); sumGPA = sumGPA + s.getGpa(); countGPA++; sumAg = sumAg+ s.getAge(); countAGE++; } long endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.; System.out.println("Function 2 Time: " + time + " seconds"); } public static void function3(ArrayList list) throws InterruptedException { long startTime = System.nanoTime(); for (long i = 0; i < 10000; i++) { Student s = new Student(rngGPA(), rngAGE(), rngID()); list.add(s); sumGPA = sumGPA + s.getGpa(); countGPA++; sumAg = sumAg+ s.getAge(); countAGE++; } long endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.; System.out.println("Function 3 Time: " + time + " seconds"); } public static void function4(ArrayList list) throws InterruptedException { long startTime = System.nanoTime(); for (long i = 0; i < 10000; i++) { Student s = new Student(rngGPA(), rngAGE(), rngID()); list.add(s); sumGPA = sumGPA + s.getGpa(); countGPA++; sumAg = sumAg+ s.getAge(); countAGE++; } long endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.; System.out.println("Function 4 Time: " + time + " seconds"); } public static void function5(ArrayList list) throws InterruptedException { long startTime = System.nanoTime(); for (long i = 0; i < 10000; i++) { Student s = new Student(rngGPA(), rngAGE(), rngID()); list.add(s); sumGPA = sumGPA + s.getGpa(); countGPA++; sumAg = sumAg+ s.getAge(); countAGE++; } long endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.; System.out.println("Function 5 Time: " + time + " seconds"); } public static float rngGPA() { Random r = new Random(); return (r.nextFloat()*3)+2; } public static int rngAGE() { Random r = new Random(); return r.nextInt((30-18)+1)+18; } public static long rngID() { return uniqueID++; }}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started