OneCompiler

ja8

239
  1. Write a java program to define a thread for printing text on output screen for ‘n’
    number of times. Create 3 threads and run them. Pass the text ‘n’ parameters to the
    thread constructor.
    Example:
    i. First thread prints “COVID19” 10 times.
    ii. Second thread prints “LOCKDOWN2020” 20 times
    iii. Third thread prints “VACCINATED2021” 30 times [15 M]
    public class TextPrinterThread extends Thread {
    private String text;
    private int times;

    public TextPrinterThread(String text, int times) {
    this.text = text;
    this.times = times;
    }

    @Override
    public void run() {
    for (int i = 0; i < times; i++) {
    System.out.println(text);
    }
    }

    public static void main(String[] args) {
    TextPrinterThread thread1 = new TextPrinterThread("COVID19", 10);
    TextPrinterThread thread2 = new TextPrinterThread("LOCKDOWN2020", 20);
    TextPrinterThread thread3 = new TextPrinterThread("VACCINATED2021", 30);

     thread1.start();
     thread2.start();
     thread3.start();
    

    }
    }

2.Write a JSP program to check whether a given number is prime or not. Display the result
in red color.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Prime Number Checker</title> </head> <body> <h1>Prime Number Checker</h1> <form method="post"> Enter a number: <input type="number" name="number"><br> <input type="submit" value="Check"> </form> <% if (request.getMethod().equals("POST")) { int number = Integer.parseInt(request.getParameter("number")); boolean isPrime = true; if (number <= 1) { isPrime = false; } else { for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { isPrime = false; break; } } } if (isPrime) { out.println("<p style='color:red'>" + number + " is a prime number</p>"); } else { out.println("<p style='color:red'>" + number + " is not a prime number</p>"); } } %> </body> </html>