Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To become familiar with writing a thread by extending the Thread class. 1. Write

ID: 3640510 • Letter: T

Question

To become familiar with writing a thread by extending the Thread class.

1. Write a class named RaceCar that extends the Thread class and contains the run() method.

2. Add an int field named finish and a String field called name. Add a
constructor that initializes both fields.

3. Within run(), add a for loop that executes finish number of times.
Within the for loop, use System.out.println() to print out the name
field and also the current iteration through the loop. For example,
the third time through the loop should output “Mario: 3” for a car
named Mario. Then, have the thread sleep for a random amount of
time between 0 and 5 seconds.

4. At the end of the for loop, print out a message stating that the race
car has finished the race, and print out the name field as well. For
example, “Mario finished!”

5. Save and compile the RaceCar class.

6. Write a class named Race that contains main().

7. Within main(), declare and create an array large enough to hold five
Thread references.

8. Write a for loop that fills the array with five RaceCar objects. The
names should be retrieved from five command-line arguments, and
the int should be the same for each RaceCar. This value will represent how long the race will last, and it should also be input from the
command line.

9. Write a second for loop that invokes start() on each Thread in the
array.

10. Save, compile, and run the Race program.

The Race program will look like a car race that slowly progresses as
you watch it. The winner will be whichever RaceCar thread reaches the
finish line first.

Explanation / Answer


public class RaceCar extends Thread {

    private int finish;
    private String name;

    RaceCar(int finish, String name) {
        this.finish = finish;
        this.name = name;
    }

    public void run() {
        java.util.Random randomGenerator = new java.util.Random();
        for (int i = 0; i < this.finish; i++) {
            
            System.out.println (this.name + ": " + i);            
            try {
                // Random numbers from 0-5 secs
                Thread.sleep((randomGenerator.nextInt(5000) + 0));
                
            } catch (InterruptedException iex) {
                iex.printStackTrace();
            } // END TRY CATCH SLEEP
            
        } // END LOOP       
        System.out.println(this.name + " race finished!!");        
    } // END RUN

}

class DriverRaceCar {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);

// 5 RaceCar thread references
        Thread[] threadRefs = new Thread[5];

        System.out.print("Enter Length Of the Race : ");
        int length = Integer.parseInt(input.nextLine());

        System.out.println (" Enter 5 car names for the race ");
        System.out.println ("===============================");
        for (int loop = 0; loop < threadRefs.length; loop++) {
            System.out.print("Enter Car Name [" + (loop + 1) + "] : ");
            threadRefs[loop] = new RaceCar(length, input.nextLine());
        }
        
        // Start the race;
        for (int loop = 0; loop < threadRefs.length; loop++) {
            RaceCar car = (RaceCar)threadRefs[loop];
            car.start();
        }
        
    }
}


public class RaceCar extends Thread {

    private int finish;
    private String name;

    RaceCar(int finish, String name) {
        this.finish = finish;
        this.name = name;
    }

    public void run() {
        java.util.Random randomGenerator = new java.util.Random();
        for (int i = 0; i < this.finish; i++) {
            
            System.out.println (this.name + ": " + i);            
            try {
                // Random numbers from 0-5 secs
                Thread.sleep((randomGenerator.nextInt(5000) + 0));
                
            } catch (InterruptedException iex) {
                iex.printStackTrace();
            } // END TRY CATCH SLEEP
            
        } // END LOOP       
        System.out.println(this.name + " race finished!!");        
    } // END RUN

}

class DriverRaceCar {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);

// 5 RaceCar thread references
        Thread[] threadRefs = new Thread[5];

        System.out.print("Enter Length Of the Race : ");
        int length = Integer.parseInt(input.nextLine());

        System.out.println (" Enter 5 car names for the race ");
        System.out.println ("===============================");
        for (int loop = 0; loop < threadRefs.length; loop++) {
            System.out.print("Enter Car Name [" + (loop + 1) + "] : ");
            threadRefs[loop] = new RaceCar(length, input.nextLine());
        }
        
        // Start the race;
        for (int loop = 0; loop < threadRefs.length; loop++) {
            RaceCar car = (RaceCar)threadRefs[loop];
            car.start();
        }
        
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote