program list:
WAP program to show constructor overloading using static member.
WAP to implement multilevel inheritance and method overriding.
WAP to implement interface class and show use of package.
WAP to implement multilevel exception handling and create your own exception.
WAP to implement 3 threads such that 1st sleeps for 200ms, 2nd for 400ms and 3rd for 600ms.
WAP to create applet of moving banner.
WAP to make a simple calculator.
Build a client server chat application.
2. public class javaprog1{
String str1,str2,str3;
static int count;
javaprog1(){
count++;
}
javaprog1(String s1){
str1 = s1;
count++;
}
javaprog1(String s2,String
s3){
str2 = s2;
str3 = s3;
count++;
}
WAP program to show constructor
overloading using static member
3. public static void main(String args[]){
javaprog1 obj1 = new javaprog1();
javaprog1 obj2 = new javaprog1("string 1");
javaprog1 obj3 = new javaprog1("string 2","string 3");
System.out.println("number of times static variable used :
"+count);
}
}.
4.
5. WAP to implement multilevel
inheritance and method overriding
class parent{
public void
function(){
System.out.println("In
parent class");
}
}
class child1 extends
parent{
public void function(){
System.out.println("In
child1 class");
}
}
6. class child2 extends parent{
public void function(){
System.out.println("In child2
class");
}
}
public class javaprog2{
public static void main(String
args[]){
parent obj = new parent();
obj.function();
child1 obj_c1 = new child1();
child2 obj_c2 = new child2();
obj = obj_c1;
obj.function();
obj = obj_c2;
obj.function();
}
}
7.
8. WAP to implement interface class
and show use of package
// JavaProg3 is a different
//package
package JavaProg3;
public interface javaprog3{
public void print(String
str_arg);
}
import JavaProg3.*;
public class javaprog4 implements
javaprog3{
public void print( String str_arg){
System.out.println(str_arg);
}
public static void main(String args[]){
javaprog4 obj = new javaprog4();
obj.print(args[0]);
}
}
9.
10. exception handling and create your
own exception
import JavaProg3.*;
public class javaprog4 implements
javaprog3{
public void print( String str_arg[]){
try{
for(int i=0;i<10;i++)
System.out.println(str_arg[i]+"n");
}catch(Exception e){
System.out.println("exception
caught and re-thrown");
throw(e);
}
}
public static void main(String args[]){
javaprog4 obj = new
javaprog4();
try{
obj.print(args);
} catch(Exception e){
System.out.println(e);
}
}
}
11.
12. WAP to implement 3 threads such that 1st
sleeps for 200ms, 2nd for 400ms and 3rd for
600ms
class NewThread implements
Runnable {
Thread t;int time;
NewThread(String str,int time1) {
time = time1;
t = new Thread(this, str);
System.out.println(t);
t.start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(t);
Thread.sleep(time);
}
} catch (InterruptedException e) {
System.out.println("Child
interrupted.");
}
System.out.println("Exiting"+t);
}
}
class ThreadDemo {
public static void main(String args[]) {
13. try {
NewThread t1 =
new NewThread("thread1",200);
NewThread t2 = new
NewThread("thread2",400);
NewThread t3 = new
NewThread("thread3",600);
}catch (Exception e) {
System.out.println("
thread interrupted."+e);
}
}
}
14.
15. WAP to create applet of moving
banner
import java.awt.*;
import java.applet.*;
public class ParamBanner
extends Applet implements
Runnable {
String msg=" Hello Java......
";
Thread t = null;
int state;
boolean stopFlag;
public void start() {
setBackground(Color.blue);
setForeground(Color.green);
Font currentFont = new
Font("TimesRoman", Font.PLAIN,
40);
setFont(currentFont);
t = new Thread(this);
stopFlag = false;
t.start();
}
public void run() {
char ch;