Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the logic to check if n is a Vampire Number. The following is the code I need assistance with: public class

I need help with the logic to check if n is a Vampire Number. The following is the code I need assistance with:

public class VampireNumber {

private static int TOTAL = 0; private static Object lock = new Object();

public static void main(String[] args) { // Create and start the two worker threads Thread worker1 = new Thread(new EvenWorker()); worker1.start(); Thread worker2 = new Thread(new OddWorker()); worker2.start(); // Join the two worker threads try { worker1.join(); worker2.join(); } catch (InterruptedException e) { e.printStackTrace(); } // Compute and display the TOTAL number of Vampire numbers System.out.println("The TOTAL number of Vampire numbers found is: " + TOTAL); }

public static class EvenWorker implements Runnable { @Override public void run() { int count = 0; for (int i = 100000; i <= 999999; i += 2) { if (isVampireNumber(i)) { System.out.println("First worker found: " + i); count++; } } synchronized (lock) { TOTAL += count; } System.out.println("First worker found " + count + " Vampire numbers"); } }

public static class OddWorker implements Runnable { @Override public void run() { int count = 0; for (int i = 100001; i <= 999999; i += 2) { if (isVampireNumber(i)) { System.out.println("Second worker found: " + i); count++; } } synchronized (lock) { TOTAL += count; } System.out.println("Second worker found " + count + " Vampire numbers"); } }

public static boolean isVampireNumber(int n) { // Logic to check if n is a Vampire number } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions