Problem Statement:
Write a java program to check the given number is Palindrome or not
Sourcecode:
import [Link].*;
class Palindrome
public static void main(String args[])
int n=4664,k,r,sum=0;
k=n;
while(n>0)
n=n/10;
r=n%10;
sum=sum*10+r;
if(sum==k)
[Link]("the given number is palindrome");
else
[Link]("the given number is not a palindrome");
1
OUTPUT:
C:\sukanya>javac [Link]
C:\sukanya>java Palindrome
the given number is a palindrome
2
Problem Statement:
Write a java program to generate Pascal’s triangle
Sourcecode:
public class Pattern
{
public static void main(String[] args)
{
int rows = 6, coef = 1;
for(int i = 0; i < rows; i++)
{
for(int space = 1; space < rows - i; ++space)
{
[Link](" ");
}
for(int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
[Link]("%4d", coef);
}
[Link]();
}
}
}
3
OUTPUT:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
4
Problem Statement:
Write a program to read data from the keyboard at runtime
Sourcecode:
import [Link].*;
class Input
public static void main(String args[])throws IOException
int n;
float x;
char ch;
String s;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("enter an integer values:");
n=[Link]([Link]());
[Link]("enter float values:");
x=[Link]([Link]());
[Link]("entr a character:");
ch=(char)[Link]();
[Link]("enter a string:");
s=[Link]();
[Link]("integer value is:"+n);
[Link]("floting value is:"+x);
[Link]("character value is:"+ch);
5
[Link]("string value is:"+s);
OUTPUT:
C:\sukanya>javac [Link]
C:\sukanya>java Input
enter an integer values:
100
enter float values:
45.23
entr a character:
ch
enter a string: star
integer value is:100
floting value is:45.23
character value is:ch
string value is:star
6
Problem Statement:
Write a java program to read the details of a student and find the result.
Sourcecode:
import [Link].*;
class Student
public static void main(String args[])throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("enter student Id:");
int sno=[Link]([Link]());
[Link]("enter student name:");
String sname=[Link]();
[Link](" enter subjects:");
String sub=[Link]();
[Link]("enter seven subjects marks:");
int m1=[Link]([Link]());
int m2=[Link]([Link]());
int m3=[Link]([Link]());
int m4=[Link]([Link]());
int m5=[Link]([Link]());
int m6=[Link]([Link]());
int m7=[Link]([Link]());
int total=m1+m2+m3+m4+m5+m6+m7;
7
float avg=total/7;
[Link]("student number:"+ sno);
[Link]("student name:"+ sname);
[Link]. println("subjects;"+ sub);
[Link]("english:"+ m1);
[Link]("telugu:"+ m2);
[Link]("maths:"+ m3);
[Link]("statistics:"+ m4);
[Link]("computers:"+ m5);
[Link]("ict:"+ m6);
[Link]("css:"+ m7);
[Link]("total marks:"+ total);
if(avg>70)
[Link]("distinction");
else if((avg<70)&&(avg>60))
[Link]("first class");
else if((avg<60)&&(avg>50))
[Link]("second class");
else if((avg<50)&&(avg>35))
[Link]("third class");
else
[Link]("fail");
8
OUTPUT:
C:\VANITHA>javac [Link]
C:\VANITHA>java Student
enter student Id:100
enter student name:vanitha
enter subjects:7
enter seven subjects marks:
89
90
91
78
76
57
78
student number:100
student name:vanitha
subjects;7
english:89
telugu:90
maths:91
statistics:78
computers:76
ict:57
css:78
9
total marks:559
distinction
10
Problem Statement:
Write a java program to find the sum of multiple numbers using Method
Overloading.
Sourcecode:
import [Link].*
public class Methodoverloading
{
int add(int num1, int num2)
{
return num1+num2;
}
int add(int num1, int num2, int num3)
{
return num1+num2+num3;
}
int add(int num1, int num2, int num3, int num4)
{
return num1+num2+num3+num4;
}
public static void main(String[] args)
Methodoverloading obj = new Methodoverloading ();
[Link]("Sum of two numbers: "+[Link](10, 20));
[Link]("Sum of three numbers: "+[Link](10, 20, 30));
[Link]("Sum of four numbers: "+[Link](1, 2, 3, 4));
11
}
OUTPUT:
Sum of two numbers: 30
Sum of three numbers: 60
Sum of four numbers: 10
12
Problem Statement:
Write a java program for constructor overloading
Sourcecode:
import [Link].*;
class Employee
int empno;
String ename;
Employee()
empno=100;
ename="Radha";
Employee(int no,String name)
empno=no;
ename=name;
void display()
[Link]("The Employee Number is :"+empno);
[Link](" Employee Name is:"+ename);
13
class Overloading
public static void main(String args[])
Employee e1= new Employee(100,"Sita");
Employee e2= new Employee();
[Link]();
[Link]();
OUTPUT:
C:\VANITHA>javac [Link]
C:\VANITHA>java Overloading
The Employee Number is :100
Employee Name is:Sita
The Employee Number is :100
Employee Name is:Radha
14
Program Statement:
Program to convert the given matrix into its transpose matrix
Source Code:
import [Link].*;
class MatrixTranspose
public static void main(String args[])throws IOException
int m, n, c, d;
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter the number of rows and columns of matrix");
m = [Link]([Link]());
n = [Link]([Link]());
int matrix[][] = new int[m][n];
[Link]("Enter the elements of matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
matrix[c][d] = [Link]([Link]());
[Link]("The original Matrix:");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
[Link](matrix[c][d]+"\t");
[Link]("\n");
15
}
int transpose[][] = new int[n][m];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];
[Link]("\nTranspose of the matrix:");
for (c = 0; c < n; c++)
for (d = 0; d < m; d++)
[Link](transpose[c][d]+"\t");
[Link]("\n");
Output:
C:\ramya>javac [Link]
C:\ramya>java MatrixTranspose
Enter the number of rows and columns of matrix
16
Enter the elements of matrix
The original Matrix:
2 3 4 5
6 7 8 9
0 1 2 3
Transpose of the matrix:
2 6 0
3 7 1
4 8 2
5 9 3
17
Problem Statement:
Write a java program to demonstrate string functions
Sourcecode:
import [Link].*;
import [Link].*;
class Strdemo
public static void main(String args[])throws IOException
String s1="A book on java";
String s2=new String("I like it");
char arr[]={'d','r','e','a','m','d','o','c','t','o','r','i','a','s'};
String s3=new String(arr);
[Link](s1);
[Link](s2);
[Link](s3);
[Link]("length s1="+[Link]());
[Link]("s1 and s2 joined is"+[Link](s2));
[Link](s1+"from"+s3);
boolean x=[Link]("A");
if(x)
[Link](s1+"start with A");
else
[Link](s1+"does not starts with A");
18
[Link](s1+"in upper case is:"+[Link]());
[Link](s2+"in lower case is:"+[Link]());
OUTPUT:
C:\VANITHA>javac [Link]
C:\VANITHA>java Strdemo
A book on java
I like it
dreamdoctorias
length s1=15
s1 and s2 joined isA book on javaI like it
A book on javafromdreamdoctorias
A book on javastart with A
A book on javain upper case is:A BOOK ON JAVA
I like itin lower case is:i like it
19
Problem Statement:
Write a java program to implement Inheritance
Sourcecode:
import [Link].*
class Teacher
String designation = "Teacher";
String collegeName = "Beginnersbook";
void does()
[Link]("Teaching");
public class PhysicsTeacher extends Teacher
String mainSubject = "Physics";
public static void main(String args[])
PhysicsTeacher obj = new PhysicsTeacher();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]();
20
}
OUTPUT:
Beginnersbook
Teacher
Physics
Teaching
21
Problem Statement:
Write a java program to demonstrate Interfaces
Sourcecode:
import [Link].*;
import [Link].*;
interface Father
double Ht=5.9;
void height();
interface Mother
double Ht=5.5;
void height();
class Child implements Father,Mother
public void height()
double ht=([Link]+[Link])/2;
[Link]("child height is:"+ht);
class Demointerface
22
{
public static void main(String args[])throws IOException
Child c=new Child();
[Link]();
OUTPUT:
C:\VANITHA>javac [Link]
C:\VANITHA>java Demointerface
child height is:5.7
23
Problem Statement:
Write a java program to illustrate the concept of try, catch and finally blocks.
Sourcecode:
import [Link].*
class exception
public static void main(String args[])
try
String s1 = args[0];
String s2 = args[1];
int n1 = [Link](s1);
int n2 = [Link](s2);
int n3 = n1/n2;
[Link]("division = " +n3);
catch(ArithmeticException ae)
[Link]("donot enter zero for denominator....");
catch(NumberFormatException nt)
[Link]("pass only numerical values....");
24
}
catch(ArrayIndexOutOfBoundsException ab)
[Link]("pass two values from cmd prompt....");
finally
[Link]("i am from finally");
OUTPUT:
D:\Programs\core>javac [Link]
D:\Programs\core>java exception 2 0
donot enter zero for denominator....
i am from finally
25
Problem Statement:
Write a java program to implement Threads
Sourcecode:
import [Link].*;
class A extendsThread
public void run()
for(int i=1;i<=5;i++)
try
[Link]("\t from thread A:i="+i);
[Link](1000);
catch(Exception e)
{}
[Link]("Exit from A");
class B extends Thread
public void run()
26
{
for(int j=1;j<5;j++)
try
[Link]("\t from thread B:j="+j);
[Link](1000);
catch(Exception e)
[Link]("exit from B");
class C extends Thread
public void run()
for(int k=1;k<=5;k++)
try
[Link]("\t from thread C:k="+k);
27
[Link](1000);
catch(Exception e)
[Link]("exit from C");
class ThreadTest
public static void main(String args[])
new A().start();
new B().start();
new C().start();
OUTPUT:
Threadtest
From Thread A:i=1
From Thread B:j=1
From Thread C:K=1
28
From Thread A:i=2
From Thread B:j=2
From Thread C:K=2
From Thread A:i=3
From Thread B:j=3
From Thread C:K=3
From Thread A:i=4
From Thread B:j=4
From Thread C:K=4
From Thread A:i=5
From Thread B:j=5
From Thread C:K=5
exit from C
exit from A
exit from B
29
Problem Statement:
Program to arrange the given strings in ascending order
Source code:
import [Link].*;
public class AlphaString
public static void main(String[] args) throws IOException
String temp;
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter number of strings you would like to enter:");
int count = [Link]([Link]());
String str[] = new String[count];
[Link]("Enter the Strings one by one:");
for(int i = 0; i < count; i++)
str[i] = [Link]();
for (int i = 0; i < count; i++)
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
temp = str[i];
30
str[i] = str[j];
str[j] = temp;
[Link]("Strings in Sorted Order:");
for (int i = 0; i <= count - 1; i++)
[Link]("\n"+str[i] );
Output:
C:\ramya>javac [Link]
C:\ramya>java AlphaString
Enter number of strings you would like to enter:5
Enter the Strings one by one:
zebra
cattle
morel
butterfly
monkey
31
Strings in Sorted Order:
butterfly
cattle
monkey
morel
zebra
32
Problem Statement:
Create a package it must contain a class with suitable methods to (calculate)
display multiplication table of a given number.
Sourcecode:
Import [Link].*
package mp;
public class mul
int n;
public mul(int n)
this.n = n;
public void table()
for (int i=1;i<=10;i++)
[Link](n + "*" + i + "=" + (n*i));
//[Link]
//import [Link];
class packdemo
33
public static void main(String args[])
int n = [Link](args[0]);
[Link] mo = new [Link](n);// mul mo = new mul(n);
[Link]();
OUTPUT:
D:\Programs\core>javac -d . [Link]
D:\Programs\core>javac [Link]
D:\Programs\core>set classpath = %classpath%;.;
D:\Programs\core>java packdemo 4
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
34
35