About
1st QUESTION
object operator { def combine(a: Double, b: Double, c: Double, operation: (Double, Double) => Double): Double = { operation(operation(a, b), c) }
def main(args: Array[String]): Unit = { val res0: Double = combine(1, 2, 3, _ + _) println(s"Result 0: $res0")
val res1: Double = combine(2, 4, 6, _ * _)
println(s"Result 1: $res1")
val res2: Double = combine(1, 2, 3, (x, y) => x min y)
println(s"Result 2: $res2")
} }
2ND QUESTION
applyall :: Num a => [a -> a] -> a -> [a] applyall fs x = map (\f -> f x) fs --appling function +3,*2 in 10 so that it print [13,20]
main :: IO () main = do let result = applyall [(+3), (*2)] 10 putStrLn ("Result: " ++ show result)
3RD QUESTION
import java.util.Random;
class NumberGenerator { private int number; private boolean isGenerated;
public synchronized void generateNumber() {
// Generate a random number
Random rand = new Random();
number = rand.nextInt(100) + 1; // Generate a random number between 1 and 100
isGenerated = true;
System.out.println("Generated Number: " + number);
// Notify waiting threads that the number is ready
notify();
}
public synchronized void processNumber() {
while (!isGenerated) {
try {
// Wait for the number to be generated
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (number % 7 == 0) {
// Double the number if it is divisible by seven
number *= 2;
System.out.println("Second Thread: Doubled Number: " + number);
} else {
// Triple the number if it is not divisible by seven
number *= 3;
System.out.println("Third Thread: Tripled Number: " + number);
}
}
}
public class Main { public static void main(String[] args) { NumberGenerator numberGenerator = new NumberGenerator();
// First thread to generate a random number
Thread generatorThread = new Thread(() -> {
numberGenerator.generateNumber();
});
// Second thread to process the generated number
Thread processorThread = new Thread(() -> {
numberGenerator.processNumber();
});
generatorThread.start();
processorThread.start();
}
}