Spaces:
Sleeping
Sleeping
| import com.sun.net.httpserver.HttpServer; | |
| import com.sun.net.httpserver.HttpHandler; | |
| import com.sun.net.httpserver.HttpExchange; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.net.InetSocketAddress; | |
| public class OnlineExam { | |
| public static void main(String[] args) throws Exception { | |
| HttpServer server = HttpServer.create(new InetSocketAddress(7860), 0); | |
| server.createContext("/", new MyHandler()); | |
| server.setExecutor(null); | |
| server.start(); | |
| System.out.println("Server started at http://localhost:7860"); | |
| } | |
| static class MyHandler implements HttpHandler { | |
| public void handle(HttpExchange t) throws IOException { | |
| String response = | |
| "<html>" + | |
| "<body>" + | |
| "<h1>Online Exam System</h1>" + | |
| "<form>" + | |
| "<h3>Q1. What is the capital of India?</h3>" + | |
| "<input type='radio' name='q1'> Mumbai<br>" + | |
| "<input type='radio' name='q1'> Delhi<br>" + | |
| "<input type='radio' name='q1'> Kolkata<br><br>" + | |
| "<h3>Q2. 2 + 2 = ?</h3>" + | |
| "<input type='radio' name='q2'> 3<br>" + | |
| "<input type='radio' name='q2'> 4<br>" + | |
| "<input type='radio' name='q2'> 5<br><br>" + | |
| "<h3>Q3. Which language is used for AI?</h3>" + | |
| "<input type='radio' name='q3'> Java<br>" + | |
| "<input type='radio' name='q3'> Python<br>" + | |
| "<input type='radio' name='q3'> C<br><br>" + | |
| "<h3>Q4. What is the national animal of India?</h3>" + | |
| "<input type='radio' name='q4'> Lion<br>" + | |
| "<input type='radio' name='q4'> Tiger<br>" + | |
| "<input type='radio' name='q4'> Elephant<br><br>" + | |
| "<h3>Q5. Which is the largest planet?</h3>" + | |
| "<input type='radio' name='q5'> Earth<br>" + | |
| "<input type='radio' name='q5'> Jupiter<br>" + | |
| "<input type='radio' name='q5'> Mars<br><br>" + | |
| "<button type='submit'>Submit Exam</button>" + | |
| "</form>" + | |
| "<p>Result: Demo Page (Logic not added yet)</p>" + | |
| "</body>" + | |
| "</html>"; | |
| t.sendResponseHeaders(200, response.getBytes().length); | |
| OutputStream os = t.getResponseBody(); | |
| os.write(response.getBytes()); | |
| os.close(); | |
| } | |
| } | |
| } | |
| public static void main(String[] args) { | |
| int score = 0; | |
| System.out.println("===== ONLINE EXAM SYSTEM ====="); | |
| // Q1 | |
| System.out.println("Q1. Capital of India?"); | |
| int a1 = 2; // predefined answer (Delhi) | |
| if (a1 == 2) score++; | |
| // Q2 | |
| System.out.println("Q2. National animal of India?"); | |
| int a2 = 2; // Tiger | |
| if (a2 == 2) score++; | |
| // Q3 | |
| System.out.println("Q3. 2 + 2 = ?"); | |
| int a3 = 2; // 4 | |
| if (a3 == 2) score++; | |
| System.out.println("\nRESULT"); | |
| System.out.println("Score: " + score + "/3"); | |
| if (score == 3) System.out.println("Excellent"); | |
| else System.out.println("Try Again"); | |
| } | |
| } |