Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started