0% found this document useful (0 votes)
1 views6 pages

Java Lab Program 13-20

The document contains multiple Java programs demonstrating various concepts such as multiple inheritance using interfaces, runtime polymorphism, calculating factorials from command line arguments, handling null pointer exceptions, displaying prime numbers, importing user-defined packages, applet lifecycle, and creating threads using the Runnable interface. Each program includes class definitions, method implementations, and example outputs. The code snippets illustrate fundamental Java programming techniques and error handling.

Uploaded by

Poorvika. M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Java Lab Program 13-20

The document contains multiple Java programs demonstrating various concepts such as multiple inheritance using interfaces, runtime polymorphism, calculating factorials from command line arguments, handling null pointer exceptions, displaying prime numbers, importing user-defined packages, applet lifecycle, and creating threads using the Runnable interface. Each program includes class definitions, method implementations, and example outputs. The code snippets illustrate fundamental Java programming techniques and error handling.

Uploaded by

Poorvika. M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

13.

JAVA PROGRAM TO IMPLEMENT MULTIPLE


INHERITANCE USING INTERFACE.
interface animal
{
void eat();
}
interface bird{
void fly();
}
class bat implements animal,bird
{
public void eat()
{
[Link]("bat eats insets");
}
public void fly()
{
[Link]("bat fly at night ");
}
}
public class interfaceeg
{
public static void main(String[] args)
{
bat b1=new bat();
[Link]();
[Link]();
}
[Link] PROGRAM TO DEMONSTRATE RUNTIME
POLYMORPHISM.
class person
{
void role()
{
[Link] ("Im a a person")
}
}
class father extends preson
{
void role()
{
[Link]("Im am a father");
}
}
class mather extends person
{
void role ()
{
[Link]("i am a mother");
}
}
public class runtimeexample {
public static void main (String[] args)
{
preson p;
p=new father();
[Link]();
p=new mother ();
[Link]();
}
}
15 JAVA PROGRAM TO FIND THE FACTORIAL OF LIST
NYMBER READING INPUT AS COMMAND
LINE ARGUMENTS.
class factorial
{
public static void main(String[] args)
{
int[] num=new int[5];
if([Link]==0)
{
[Link](" no command line arguments passed");
return;
}
for(int i=0;i<[Link];i++)
num[i]=[Link](args[i]);
for(int i=0;i<[Link];i++)
{
int fact=1;
for(int j=1;j<=num[i];j++)
fact*=j;
[Link]("the factorial of "+args[i]+" is: "+fact);
}
}
}
16 JAVA PROGRAM TO HANDLE NULL POINTER
EXCEPTION AND USE THE “FINALLY” METHOD TO
DISPLAY A MESSAGE TO THE USER.

import [Link];
public class nullpointerexceptiondemo
{
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
[Link]("enter a string(or press enter to leave it empty):");
String str=[Link]();
if([Link]())
{
str = null;
}
try
{
[Link]("length of the string: "+[Link]());
}
catch(NullPointerException e)
{
[Link]("error: entered string is empty or NULL.");
}
finally
{
[Link]("Execution complete. thank you!");
}
[Link]();
}
}
[Link] PROGRAM TO DISPLAY ALL PRIME NUMBERS
BETWEEN TWO LIMITS.
public class prime
{
public static void main(String[] args)
{
int low=10,high=50;
while(low<high)
{
boolean flag=false;
for(int i=2;i<=low/2;i++)
{
if(low%i==0)
{
flag=true;
break;
}
}
if(!flag &&low!=0&& low !=1)
[Link](low+" ");
low++;
}
}
}

[Link] PROGRAM TO IMPORT USER DEFINED PACKAGES.


package bcastudent;
public class student1
{
int id;
String name;
public void getdata(int i,String n)
{
id=i;
name=n;
}
public void display()
{
[Link]("Student ID="+id);
[Link]("Student NAME="+name);
}
}
import bcastudent.student1;
class ptest
{
public static void main(String[] args)
{
student1 s=new student1();
[Link](1234,"anil");
[Link]();
}
}
[Link] PROGRAM TO IMPLEMENT THE LIFE CYCLE OF
THE APPLET.
import [Link].*;
import [Link].*;
public class appletexample extends Applet
{
public void paint(Graphics g)
{
[Link]("Hello Applet",20,20);
}
}

<HTML>
<HEAD>
<TITLE> this is my first Applet</TITLE>
<BODY>
<Applet code=[Link] width=400 height=500></Applet>
</BODY>
</HEAD>
</HTML>

20. JAVA PROGRAM TO CRATE A THREAD USING


RUNNABLE INTERFACE.

class mythread implements Runnable


{
public void run()
{
[Link]("run() started");
for(int i=1;i<=10;i++)
{
[Link](“the value is:"+i);
}
[Link]("run() completed”);
}
}
public class threadrunnable{
public static void main(String[] args)
{
[Link](“main thread started”);
mythread obj=new mythread();
Thread t1=new Thread(obj,”thread one”);
[Link]();
[Link]("main thread completed");

}
}

You might also like