Wen Cy Wen Cy - 1 month ago 18
Java Question

Java - How to make a simple examination?

How to make a program that will ask 5 questions(multiple choice a to d)?

And each question have 30 seconds coundown using thread sleep(return countdown)
and when the countdown goes to 0 it will proceed to the next question

and how to add a score using a formula?

import java.util.Scanner;
import java.io.IOException;

class timer{
public static void main(String[]args) throws IOException{
Scanner scan = new Scanner(System.in);
Thread thread1 = Thread.currentThread();
Thread thread2 = new Thread(() -> {
try{
for(int seconds = 30; seconds >= 0; seconds--){
System.out.println("\rYou have "+seconds+"second's left!");
Thread.sleep(1000);
}
}catch(InterruptedException e){}
});
System.out.println("What is 1+1? ");
System.out.println("a. 1\t b.2\t c.3\t d.4");
thread2.start();
String answer = scan.next();
thread2.stop();
}
}

Answer

I would personally synchronize my threads using await and signal/signalAll, and rely on the method Condition#await(long time, TimeUnit unit) to know if the answer was given or not while awaiting, so my code would be something like that:

Scanner scan = new Scanner(System.in);
// The lock used to used to synchronize my threads
Lock lock = new ReentrantLock();
// The target condition
Condition answered = lock.newCondition();
Thread thread2 = new Thread(() -> {
    try {
        // Indicates whether the question has been answered
        boolean hasAnswered = false;
        lock.lock();
        try {
            // Loop until the question has been answered or 10 seconds
            for (int seconds = 10; !hasAnswered && seconds >= 0; seconds--){
                System.out.printf("You have %d sec's left!%n", seconds);
                // Wait 1 second and if await returns true it means that
                // I had an answer in time otherwise it means that we
                // reached the timeout without getting answer
                hasAnswered = answered.await(1L, TimeUnit.SECONDS);
            }
        } finally {
            lock.unlock();
        }
        // Print the message indication whether we get the answer in time or not
        if (hasAnswered) {
            System.out.println("Good Job !!");
        } else {
            System.out.println("Too late !!");
        }
    } catch(InterruptedException e){
        Thread.currentThread().interrupt();
    }
});
System.out.println("What is 1+1? ");
System.out.println("a. 1\t b.2\t c.3\t d.4");
thread2.start();
String answer = scan.next();
lock.lock();
try {
    // Notify the other thread that we have an answer
    answered.signalAll(); // could be answered.signal() as we have only 
                          // thread waiting to be notified but it is a 
                          // better practice to use signalAll
} finally {
    lock.unlock();
}

Output in case of a success :

What is 1+1? 
a. 1     b.2     c.3     d.4
You have 10 sec's left!
You have 9 sec's left!
You have 8 sec's left!
You have 7 sec's left!
2
Good Job !!

Output in case it is too late :

What is 1+1? 
a. 1     b.2     c.3     d.4
You have 10 sec's left!
You have 9 sec's left!
You have 8 sec's left!
You have 7 sec's left!
You have 6 sec's left!
You have 5 sec's left!
You have 4 sec's left!
You have 3 sec's left!
You have 2 sec's left!
You have 1 sec's left!
You have 0 sec's left!
Too late !!