Question: JAVA: Run the program and observe the output (but I get errors and not sure why) BusinessLogic.java import java.util.Scanner; public class BusinessLogic { int num1,

JAVA: Run the program and observe the output (but I get errors and not sure why)

BusinessLogic.java

import java.util.Scanner; public class BusinessLogic { int num1, num2 = 1; int total = 0; prod = 1; public void reader(){ // We want reader to run on it's own thread Scanner scanner = new Scanner(System.in); // read from the keyboard while (true){ // infinite loop to keep reading num1 = scanner.nextInt(); num2 = num1; } }// reader Public void total(){ while (true){ // infinite loop to keep reading try { Thread.currentThread().Sleep (3 * 1000); // to do it every 3 seconds } catch(InterruptedException ie) {} total += num1; // now we can access num, so we can use it here num1 = 0; System.out.println("Total so far: " + total): } }// total Public void product(){ while (true){ // infinite loop to keep reading try { Thread.currentThread().Sleep (5 * 1000); // to do it every 5 seconds } catch(InterruptedException ie) {} prod *= num2; // now we can access num, so we can use it here num2 = 1; System.out.println("Product so far: " + prod): } }// product - we need to call this function from the new thread }// class BusinessLogic

ReaderThread.java

import java.util.Scanner; import java.util.concurrent.*; public class ReaderThread implement Runnable { int num; // made num a globle variable BusinessLogic bl; public ReaderThread(BusinessLogic bl){ this.bl = bl; } public void run(){ bl.reader(); } public static void main (string[] args) { // we need to actually create two threads // need to create BusinessLogic first then pass it to the thread BusinessLogic bl = new BusinessLogic(){ Thread readerThread = new Thread(new ReaderThread(bl)); Thread totalThread = new Thread(new TotalThread(bl)); Thread prodThread = new Thread(new ProductThread(bl)); totalThread.start(); prodThread.start(); readerThread.start(); } }

TotalThread.java

// TotalThread // Not reading so don't need the following 2 imports // import java.util.Scanner; // import java.util.concurrent; public class TotalThread implement Runnable { int total =0; // total to keep track. Initialize BusinessLogic bl; public TotalThread(BusinessLogic bl){ this.bl = bl; } public void run(){ bl.total(); } } // problem? the total and the num from user are in different classes // solution - call the function inside or run. Create another - BusinessLogic 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!