0% found this document useful (0 votes)
5 views4 pages

Even-Odd Number Printer in Java

The document contains Java code for a multi-threaded program that prints even and odd numbers up to a specified maximum. It uses synchronization to ensure that even and odd numbers are printed alternately by two threads. There is also a test case indicating a failure in the expected output versus the actual output when running the program.

Uploaded by

B NAGALAKSHMI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Even-Odd Number Printer in Java

The document contains Java code for a multi-threaded program that prints even and odd numbers up to a specified maximum. It uses synchronization to ensure that even and odd numbers are printed alternately by two threads. There is also a test case indicating a failure in the expected output versus the actual output when running the program.

Uploaded by

B NAGALAKSHMI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Scanner;

class NumberPrinter implements Runnable {

// Write code here...

private final int start; // The starting number (0 for even, 1 for odd)

private final int maxNumber; // The maximum number to print

private static int currentNumber = 1; // Track the current number being printed

private static final Object lock = new Object(); // Lock object for synchronization

public NumberPrinter(int start, int maxNumber) {

[Link] = start;

[Link] = maxNumber;

@Override

public void run() {

for (int i = start; i <= maxNumber; i += 2) {

synchronized (lock) {

// Print the number

[Link]([Link]().getName() + ":" + i);

// Increment the current number (to track which number is being printed)

currentNumber++;

// Notify the other thread to print next

[Link]();

// If there are still numbers to print, wait for the other thread

if (currentNumber <= maxNumber) {

try {
[Link](); // Wait for the other thread to print

} catch (InterruptedException e) {

[Link]().interrupt();

// Driver code is given below for your reference

public class EvenOddNumberPrinter {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int maxNumber = 0;

boolean validInput = false;

while (!validInput) {

[Link]("Enter the maximum number to print:");

String input = [Link]();

try {

maxNumber = [Link](input);

if (maxNumber >= 1) {

validInput = true;

} else {

[Link]("Please enter a number greater than or equal to 1.");

} catch (NumberFormatException e) {

[Link]("Invalid input. Please enter a valid number.");


}

Runnable evenPrinter = new NumberPrinter(0, maxNumber);

Runnable oddPrinter = new NumberPrinter(1, maxNumber);

Thread evenThread = new Thread(evenPrinter, "EvenThread");

Thread oddThread = new Thread(oddPrinter, "OddThread");

[Link]();

[Link]();

[Link]();

Test case 1

failed

Expected output Actual output

Enter·the·maximum·number·to·print:⏎ Enter·the·maximum·number·to·print

5 5

OddThread:·1⏎ Empty⏎

EvenThread:·2⏎ Empty⏎

OddThread:·3⏎ Empty⏎

EvenThread:·4⏎ Empty⏎

OddThread:·5⏎ Empty⏎

EvenThread:0

OddThread:1
Expected output Actual output

EvenThread:2

OddThread:3

EvenThread:4

OddThread:5

You might also like