[ INDEX
S No Name of the program
1 Write a java program to find the factorial of a number
2 Write a java program to display Fibonacci series
3
Write a java program to count number of words in a given text
4
Write a java program to check the given string is palindrome or not
5 Write a java program to print the prime numbers upto nth number Program:
6
Write a java program to display the employee details using Scanner class.
7
Write a java program to represent Abstract class with example.
8
Write a java program to implement Interface using extends keyword.
9
Write a Java program that implements a multi-thread application that has three threads.
10
Write a java program to display File class properties.
11
Write an applet program that displays a simple message.
12
Write a Java program compute factorial value using Applet.
13
Write a program for passing parameters using Applet.
14
Write a java program that connects to a database using JDBC
15
Write a java program to connect to database using JDBC & insert values into table
16 Write a java program to connect to a database using JDBC and delete values from table
1. Write a java program to find the factorial of the number
class Fact
{
public static void main(String[] args)
{
int number = 5;
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
[Link]("Factorial of a number is:" + factorial);
}
}
Output:
Factorial of a number is:120
2. Write a java program to display Fibonacci series
import [Link];
class Fib
{
public static void main(String args[ ])
{
Scanner input=new Scanner([Link]);
int i,a=0,b=1,c=0,t;
[Link]("Enter value of t:");
t=[Link]();
[Link](a);
[Link](" "+b);
for(i=0;i<t-2;i++)
{
c=a+b;
a=b; b=c;
[Link](" "+c);
}
[Link]();
[Link](t+"th value of the series is: "+c);
}
}
Output –
Enter value of t: 8
0 1 1 2 3 5 8 13
3. Write a java program to count number of words in a given text
import [Link].*;
class CountWords
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string : ");
String str=[Link](); int count=0; if([Link]()==0)
{
[Link]("[Link] words in given text:" + count);
}
else
{
for(int i=0;i<[Link]();i++)
{
if([Link](i)==' ')
{
count++;
}
}
[Link]("[Link] words in given text:" + (count+1));
}
}
}
Output:
Enter a string: hi this is siva
No. of words in given text: 4
4. Write a java program to check the given string is palindrome or not.
import [Link].*;
public class Palindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter String:");
String str=[Link]();
char ch[]=new char[[Link]()];
for(int i=[Link]()-1,j=0;i>=0;i--,j+
+)
ch[j]=[Link](i);
String restr=new String(ch);
[Link]("Reverse of String "+str+" is "+restr);
if([Link](restr)==0)
[Link](str+" "+"is a Palindrome");
else
[Link](str+" "+"is not a Palindrome");
}
}
Output:
Enter String: madam
Reverse of String madam is madam madam is a Palindrome
5. Write a java program to print the prime numbers upto nth number
import [Link].*;
class Primenos
{
public static void main(String args[])
{
int i,t,flag,n;
Scanner sc = new Scanner([Link]);
[Link]("ENTER n VALUE:");
n=[Link]([Link]());
[Link]("PRIME NUMBERS UP TO"+" "+n+":");
for(i=2;i<=n;i++)
{
flag=1; for(t=2;t<i;t+
+)
{
if(i%t==0)
{
flag=0;
break;
}
}
if(flag==1)
[Link](i);
}
}
}
Output:
ENTER n VALUE:
5
PRIME NUMBERS UP TO 5:
2
3
5
6. Write a java program to display the employee details using Scanner class.
import [Link];
class Employee
{
int Id;
String Name; int Age;
long Salary;
void GetData()
{
Scanner sc = new Scanner([Link]);
[Link]("\n\tEnter Employee Id : ");
Id = [Link]([Link]());
[Link]("\n\tEnter Employee Name : ");
Name = [Link]();
[Link]("\n\tEnter Employee Age : ");
Age = [Link]([Link]());
[Link]("\n\tEnter Employee Salary : ");
Salary = [Link]([Link]());
}
void PutData()// Defining PutData()
{
[Link]("\n\t" + Id + "\t" +Name + "\t" +Age + "\t" +Salary);
}
public static void main(String args[])
{
Employee[] Emp = new Employee[3]; int i;
for(i=0;i<3;i++)
Emp[i] = new Employee(); // Allocating memory to each object
for(i=0;i<3;i++)
{
[Link]("\nEnter details of "+ (i+1) +" Employee\n");
Emp[i].GetData();
}
[Link]("\nDetails of Employees\n");
for(i=0;i<3;i++)
Emp[i].PutData();
}
}
7 Write a java program to represent Abstract class with example.
abstract class Shape
{
abstract void numberOfSides();
}
class Trapezoid extends Shape
{
void numberOfSides()
{
[Link]("The no. of side's in trapezoidal are6");
}
}
class Triangle extends Shape
{
void numberOfSides()
{
[Link]("The no. of side's in triangle are:3 ");
}
}
class Hexagon extends Shape
{
void numberOfSides()
{
[Link]("The no. of side's in hexagon are:6 ");
}
}
class ShapeDemo
{
public static void main(String args[])
{
Trapezoid obj1 = new Trapezoid();
Triangle obj2 = new Triangle();
Hexagon obj3 = new Hexagon();
[Link]();
[Link]();
[Link]();
}
}
8. Write a java program to implement Interface using extends keyword.
interface Inf1
{
public void method1();
}
interface Inf2 extends Inf1
{
public void method2();
}
public class Demo implements Inf2
{
public void method1()
{
[Link]("method1");
}
public void method2()
{
[Link]("method2");
}
public static void main(String args[])
{
Inf2 obj = new Demo();
obj.method2();
}
}
9. Write a Java program that implements a multi-thread application that has three threads.
/* The first thread displays "Good Morning" for every one second, the second thread
displays "Hello" for every two seconds and third thread displays "Welcome" for every three
seconds */
class GoodMorning extends Thread
{
synchronized public void run()
{
try
{
int i=0;
while (i<5)
{
sleep(1000); [Link]("Good morning "); i++;
}
} catch (Exception e) {}
}
}
class Hello extends Thread
{
synchronized public void run()
{
try {
int i=0;
while (i<5)
{
sleep(2000);
[Link]("hello"); i++;
}
} catch (Exception e) {}
}
}
class Welcome extends Thread
{
synchronized public void run()
{
try {
int i=0;
while (i<5)
{
sleep(3000);
[Link]("welcome"); i++;
}
} catch (Exception e) {}
}
}
class MultithreadDemo
{
public static void main(String args[])
{
GoodMorning t1 = new GoodMorning();
Hello t2 = new Hello();
}
Welcome t3 = new Welcome();
[Link]();
[Link]();
[Link]();
}
}
10. Write a java program to display File class properties.
import [Link];
property class fileProperty
{
public static void main(String[] args)
{
//accept file name or directory name through command line args
String fname =args[0];
//pass the filename or directory name to File
object File f = new File(fname);
//apply File class methods on File object
[Link]("File name :"+[Link]());
[Link]("Path: "+[Link]());
[Link]("Absolute path:" +[Link]());
[Link]("Parent:"+[Link]());
[Link]("Exists :"+[Link]());
if([Link]())
{
[Link]("Is writeable:"+[Link]());
[Link]("Is readable"+[Link]());
[Link]("Is a directory:"+[Link]());
[Link]("File Size in bytes "+[Link]());
}
}
}
11. Write an applet program that displays a simple message
[Link]:
// Import the packages to access the classes and methods in awt and applet
classes. import [Link].*;
import [Link].*;
public class Applet1 extends Applet
{
// Paint method to display the message.
public void paint(Graphics g)
{
[Link]("HELLO WORLD",20,20);
}
[Link]:
/* <applet code="Applet1" width=200 height=300>
</applet>*/
12. Write a Java program to compute factorial value using Applet
. import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Factorial" width=500 height=500></applet> */
public class FactorialApplet extends Applet implements ActionListener
{
Label l1,l2;
TextField
t1,t2; Button
b1,b2;
public void init()
{
l1=new Label("Enter a value:
"); l2=new Label("Result:");
t1=new TextField(10);
t2=new TextField(10);
b1=new
Button("Calculate");
b2=new Button("Clear");
add(l1);
add(t1);
add(b1);
add(b2);
add(l2);
add(t2);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
int
n=[Link]([Link]());
int fact=1;
if([Link]()==b1)
{
if(n==0||n==1)
{
fact=1;
[Link]([Link](fact))
;
}
else
{for(int i=1;i<=n;i++) fact=fact*i; }
[Link]([Link](fact));
}
else if([Link]()==b2)
{
[Link]("");
[Link]("");
}
}
}
13. Write a program for passing parameters using Applet.
import [Link].*;
import [Link].*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
[Link]("Name is: " + n, 20, 20);
[Link]("Age is: " + a, 20, 40);
}
}
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
14. Write a java program that connects to a database using JDBC program:
import [Link];
import [Link];
public class PostgreSQLJDBC
{
public static void main(String args[])
{
Connection c = null;
try
[Link]("[Link]");
c = [Link]("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e)
{ [Link]();
[Link]([Link]().getName()+": "+[Link]());
[Link](0);
[Link]("Opened database successfully");
}
}
15. Write a java program to connect to database using JDBC & insert values
into table
import [Link].*;
public class insert1
{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String fullname = "MRCET";
String email = "MRCET@[Link]";
try
{
[Link]("[Link]");
Connection con = [Link]("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = [Link]();
// Inserting data in database
String q1 = "insert into userid values('" +id+ "', '" +pwd+
"', '" +fullname+ "', '" +email+ "')";
int x = [Link](q1);
if (x > 0)
[Link]("Successfully Inserted");
else
[Link]("Insert Failed");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
16. Write a java program to connect to a database using JDBC and delete values from table.
import [Link].*;
public class delete
{
public static void main(String args[])
{
String id = "id2";
String pwd = "pwd2";
try
{
[Link]("[Link]");
Connection con = [Link]("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = [Link]();
// Deleting from database
String q1 = "DELETE from userid WHERE id = '" +
id + "' AND pwd = '" + pwd + "'";
int x = [Link](q1);
if (x > 0)
[Link]("One User Successfully Deleted");
else
[Link]("ERROR OCCURED :(");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}