Professional Documents
Culture Documents
1. Write a program to input name and salary of employee and throw user defined exception if
entered salary is negative.
import java.util.*;
import java.io.*;
class NegativeSalaryException extends Exception
{
public NegativeSalaryException (String str)
{
super(str);
}
}
public class S1
{
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Name of employee:");
String name=sc.nextLine();
System.out.print("Enter Salary of employee:");
int salary=sc.nextInt();
try
{
if(salary<0)
throw new NegativeSalaryException("Enter Salary amount is negative");
System.out.println("Name is: "+name);
System.out.println("Salary is: "+salary);
}
catch (NegativeSalaryException a)
{
System.out.println(a);
}
}
}
2. Write a program to create two threads. One thread will display the numbers from 1 to 50
(ascending order) and other thread will display numbers from 50 to 1 (descending order).
class Ascending extends Thread
{
public void run()
{
for(int i=1; i<=50;i++)
{
System.out.println("Ascending Thread : " + i);
}
}
}
class Descending extends Thread
{
public void run()
{
for(int i=50; i>0;i--) {
System.out.println("Descending Thread : " + i);
}
}
}
public class AscendingDescendingThread
{
public static void main(String[] args)
{
Ascending a=new Ascending();
a.start();
Descending d=new Descending();
d.start();
}
}
3. Write a program to define two threads and display default priority and user defined priority for the defined
threads and main thread
import java.lang.*;
class ThreadDemo extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
public static void main(String[]args)
{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
System.out.println("t1 thread priority : " + t1.getPriority()); // Default 5
System.out.println("t2 thread priority : " + t2.getPriority()); // Default 5
t1.setPriority(2);
t2.setPriority(5);
System.out.println("t1 thread priority : " + t1.getPriority()); //2
System.out.println("t2 thread priority : " + t2.getPriority()); //5
// Main thread
System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());
// Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : " + Thread.currentThread().getPriority());
}
}
4. Design an applet which accepts username as a parameter for html page and display number of
characters from it.
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
String str="";
public void init()
{
str=getParameter("username");
}
public void paint(Graphics g)
{
int n= str.length();
String s="Number of chars = "+Integer.toString(n);
g.drawString(s,100,100);
}
}
/*<applet code=myapplet height=200 width=200>
<param name="username" value="student1">
</applet>*/
5. Write a program to create an applet for displaying four circle one below the other and filled the
alternate circle with red color.
import java.awt.*;
import java.applet.*;
public class Exp28XIII3 extends Applet
{
public void paint(Graphics g)
{
for(int i=1;i<=4;i++)
{
if(i%2==0)
{
g.fillOval(90,i*50+10,50,50);
g.setColor(Color.black);
}
else
{
g.drawOval(90,i*50+10,50,50);
g.setColor(Color.red);
}
}
}
}
/*
<applet code="Exp28XIII3" width=300 height=300></applet>
*/
6. Write an applet to accept username in the form of parameter and print “Hello <username>”.
import java.awt.*;
import java.applet.*;
public class hellouser extends Applet
{
String str;
public void init()
{
str = getParameter("username");
str = "Hello "+ str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
/*<Applet code = hellouser.class width = 400 height = 400>
<PARAM NAME = "username" VALUE = abc>
</Applet>*/
7.Design an Applet to pass username and password as parameters and check if password
contains more than 8 characters.
import java.awt.*;
import java.applet.*;
public class MyApplet1 extends Applet
{
String u;
String p;
public void init()
{
u = getParameter("username");
p = getParameter("password");
}
public void paint(Graphics g)
{
if(p.length()>8)
{
g.drawString("Password has more than 8 characters.",20,20);
}
else
{
g.drawString("Password has less than 8 characters.",20,40);
}
}
}
/*
<applet code="MyApplet1" height="300" width="500">
<param name="username" value="Admin"/>
<param name="password" value="Admin"/>
</applet>
*/