Question

Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change.

I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here

package threadExamples;
import java.util.Scanner;

public class InteractiveCounting {

        static InteractiveCounting ic;
        static InteractiveCounting.ReadInput ri;
        static InteractiveCounting.Count c;

        public static void main( String args[] ){

                ic = new InteractiveCounting();

                c = ic.new Count();
                c.start();

                ri = ic.new ReadInput();
                ri.start();

        }

        class ReadInput extends Thread{

                Scanner scanner = new Scanner(System.in);
                int numberGap;

                void read(){
                        while(true){
                                System.out.print( "\nThis is thread:" + getName());
                                System.out.print( " => Enter number: \n" );
                                InteractiveCounting.c.numberGap = scanner.nextInt();
                        }
                }

                public void run(){
                        read();
                }

        }

        class Count extends Thread{

                int total = 0;
                int numberGap = 1;

                void count(){
                        while(true){
                                total = total + numberGap;
                                System.out.println();
                                System.out.print( "This is thread:" + getName() );
                                System.out.println( " => " + total );
                                try {
                                        Thread.sleep( 3000 );
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }

                public void run(){
                        count();
                }
        }

}

threadtype2 program

package threadExamples;

public class ThreadType2 implements Runnable {

    public void run() {
        System.out.println("Hello from a thread implemented through the Runnable interface!");
    }

    public static void main(String args[]) {
        (new Thread(new ThreadType2())).start();
    }

}
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • In each of the following questions, first use NetBeans IDE to run the code (if there...

    In each of the following questions, first use NetBeans IDE to run the code (if there is an error fix it) then take a screenshot of the output and paste it under (A). In (B), justify the output you obtained in (A). - 9. Given the following: public class WorkingSheet {     public static void main(String[] args) {        B myB = new B();        A myA = new B();        System.out.print(myB instanceof A);        System.out.print(myB instanceof C);        System.out.print(myA...

  • Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program:...

    Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingDemo extends Thread public void runot try{ System.out.println (--Multithread.countDown): catch (Exception e) w public class Multithread{ public static int countDown = 10; public static void main(String[] args) { for (int i = 0; i < 3; i++) { MultithreadingDemo object = new MultithreadingDemol); object.start(); } میه

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • Here is the finsihed java program, it just needs to be converted to MIPs assembly language:...

    Here is the finsihed java program, it just needs to be converted to MIPs assembly language: import java.util.*; public class LoveCS { public static void main(String[] args) { int timesMessagePrinted; int count = 0; int sum = 0; Scanner input = new Scanner(System.in); System.out.print("How many times should the message be printed?: "); timesMessagePrinted = input.nextInt(); for(int x = 1; x <= timesMessagePrinted; x++) { System.out.println(x +" I love Computer Science!!"); count++; sum += x; } System.out.print("Printed this message " +count...

  • Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...

    Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • Write a program that will define a runnable frame to have a ball move across the...

    Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The...

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