Q.1.
Advanced Java:
A)
Write a JSP program to calculate sum of first and last digit of a given number.
Display
sum in Red Color with font size 18.
Answer :
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Sum of First and Last Digits</title>
</head>
<body>
<h2>Enter a
Number:</h2>
<form action="sum.jsp" method="post">
<input
type="number" name="num" required>
<input
type="submit" value="Calculate">
</form>
<%
if(request.getParameter("num")!=null) {
int num = Integer.parseInt(request.getParameter("num"));
int lastDigit = num % 10;
int firstDigit = num;
while(firstDigit>=10) {
firstDigit = firstDigit / 10;
}
int sum =
firstDigit + lastDigit;
%>
<h2
style="color:red; font-size:18px;">Sum of First and Last Digits: <%=sum%></h2>
<%
}
%>
</body>
</html>
Output :
B)
Write a java program in multithreading using applet for Traffic signal.
Answer :
import java.awt.*;
public class Slip5B extends Frame {
int f = 0;
public Slip5B() {
Signal s = new Signal();
s.start();
setSize(500, 500);
setVisible(true);
}
public void paint(Graphics g) {
switch (f) {
case 0:
g.setColor(Color.red);
g.fillOval(60, 60, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 120, 50, 50);
g.fillOval(60, 180, 50, 50);
break;
case 1:
g.setColor(Color.yellow);
g.fillOval(60, 120, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 60, 50, 50);
g.fillOval(60, 180, 50, 50);
break;
case 2:
g.setColor(Color.green);
g.fillOval(60, 180, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 120, 50, 50);
g.fillOval(60, 60, 50, 50);
break;
}
}
class Signal extends Thread {
public void run() {
while (true) {
f = (f + 1) % 3;
repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
public static void main(String args[]) {
new Slip5B();
}
}
No comments:
Post a Comment