OneCompiler

ja20

107
  1. Create a JSP page to accept a number from a user and display it in words: Example:
    123 – One Two Three. The output should be in red color. [15 M]

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Number to Words</title> <style> .red { color: red; } </style> </head> <body> <% String numberStr = request.getParameter("number"); try { int number = Integer.parseInt(numberStr); String[] words = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; out.print("<span class='red'>"); for (char digit : numberStr.toCharArray()) { int index = Character.getNumericValue(digit); out.print(words[index] + " "); } out.print("</span>"); } catch (NumberFormatException e) { out.print("Invalid input. Please enter a valid number."); } %> </body> </html>
  1. Write a java program to blink image on the JFrame continuously

import javax.swing.;
import java.awt.
;
import java.awt.event.*;

public class BlinkingImage extends JFrame {
private ImageIcon icon;
private JLabel label;

public BlinkingImage() {
    super("Blinking Image");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 300);
    
    // Load the image
    icon = new ImageIcon("image.jpg");
    label = new JLabel(icon);
    add(label, BorderLayout.CENTER);

    // Timer to toggle visibility
    Timer timer = new Timer(500, new ActionListener() {
        boolean visible = true;

        public void actionPerformed(ActionEvent e) {
            label.setVisible(visible);
            visible = !visible;
        }
    });
    timer.start();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new BlinkingImage().setVisible(true);
        }
    });
}

}