Slip7
Q1. 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.
firstlast.html
<html>
<body>
<form
action="firstlast.jsp" method="get">
Enter
No: <input type="text" name="fno" />
<br>
<input
type="submit" />
</form>
</body>
<html>
firstlast.jsp
<%@
page language="java" %>
<html>
<body>
<%!int
i,n,sum=0;%>
<%int
n=Integer.parseInt(request.getParameter("fno")); %>
<%
if(n>10)
{
sum
+= n % 10;
}
while(n>10)
{
n
= n / 10;
}
sum
+= n;
out.println("\nSum
of first and last digit = "+ sum);
%>
<font
size=18 color='red'>
Sum
= <%=sum%>
</font>
<br>
</body>
</html>
Q2. Write a
Multithreading program in java for Racing Car. (Use
AWT)
import
java.awt.Color;
import
java.awt.Graphics;
import
javax.swing.*;
public
class car extends JApplet {
public
static void main(String[] args) {
JFrame
frame = new JFrame();
frame.getContentPane().add(new
car());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024,
739);
frame.setVisible(true);
}
public
void paint(Graphics g) {
try
{
int
j = 0;
while
(j < 500)
{
g.setColor(Color.white);
g.fillRect(0,
0, 5000, 5000);
g.setColor(Color.red);
g.fillRect(250
+ j, 250, 100, 50);
g.setColor(Color.black);
g.fillOval(260
+ j, 300, 20, 20);
g.fillOval(320
+ j, 300, 20, 20);
j++;
Thread.sleep(2);
}
}
catch (InterruptedException e) {
}
}
}
<APPLET
CODE="car.class" WIDTH=400 HEIGHT=300> </APPLET>