Java Full
Java Full
Java Full
Program
import java.util.Scanner;
class Test{
for(int i=0;i<len/2;i++){
if(str.charAt(i) != str.charAt(len-i-1)){
flag = 1;
break;
if(flag == 0){
System.out.println("Palindrome");
else{
System.out.println("Not Palindrome");
}
Output
Palindrome
Not Palindrome
FREQUENCY OF CHARACTER IN STRING
Program
import java.util.Scanner;
class Test{
char ch = sc.nextLine().charAt(0);
int count = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ch){
count++;
}
Output
Program
import java.util.Scanner;
class Test{
int m1 = sc.nextInt();
int n1 = sc.nextInt();
int m2 = sc.nextInt();
int n2 = sc.nextInt();
if(n1 != m2){
return;
for(int j=0;j<n1;j++){
System.out.print("A["+i+"]["+j+"]=");
A[i][j] = sc.nextInt();
for(int i=0;i<m2;i++){
for(int j=0;j<n2;j++){
System.out.print("B["+i+"]["+j+"]=");
B[i][j] = sc.nextInt();
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
C[i][j]=0;
for(int k=0;k<n1;k++){
System.out.println("Matrix A");
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
System.out.print(A[i][j]+"\t");
System.out.println();
System.out.println("Matrix B");
for(int i=0;i<m2;i++){
for(int j=0;j<n2;j++){
System.out.print(B[i][j]+"\t");
System.out.println();
System.out.println("Matrix C");
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
System.out.print(C[i][j]+"\t");
System.out.println();
}
Output
Enter the order - m1:2
Enter the order - n1:3
Enter the order - m2:3
Enter the order - n2:2
Read Matrix A
A[0][0]=2
A[0][1]=4
A[0][2]=6
A[1][0]=8
A[1][1]=10
A[1][2]=12
Read Matrix B
B[0][0]=1
B[0][1]=3
B[1][0]=5
B[1][1]=7
B[2][0]=9
B[2][1]=11
Matrix A
2 4 6
8 10 12
Matrix B
1 3
5 7
9 11
Matrix C
76 100
166 226
INHERITANCE
Program
import java.util.Scanner;
class Employee{
System.out.println("Salary="+ salary);
this.name = name;
this.age = age;
this.phone = phone;
this.address = address;
this.salary = salary;
System.out.println("Name = "+name);
System.out.println("Age = "+age);
System.out.println("Phone Number = "+phone);
System.out.println("Address = "+address);
System.out.println("Salary = "+salary);
super(name,age,phone,address,salary);
this.specialization = specialization;
this.department = department;
displayEmployee();
System.out.println("Specilization ="+specialization);
System.out.println("Department ="+department);
this.specialization = specialization;
this.department = department;
displayEmployee();
System.out.println("Specilization ="+specialization);
System.out.println("Department ="+department);
class Test{
System.out.print("Name:");
System.out.print("Age:");
System.out.print("Phone Number:");
System.out.print("Address:");
System.out.print("Salary:");
System.out.print("Specialization:");
String spec = sc.nextLine();
System.out.print("Department:");
m.displayManager();
System.out.print("Name:");
System.out.print("Age:");
System.out.print("Phone Number:");
System.out.print("Address:");
System.out.print("Salary:");
System.out.print("Specialization:");
System.out.print("Department:");
o.displayOfficer();
}
Output
Enter the officer's Detail
Name:Sangeeth
Address:Trivandrum
Specialization:Computer Science
Department:CSE
Age:32
Number:9633566474
Salary:10000
The officer Detail
Name:Sangeeth
Age:32
Number:9633566474
Address:9633566474
Salary:10000.0
Specialization:Computer Science
Department:CSE
Enter the manager's Detail
Name:Manu
Address:Kochi
Specialization:CSE
Department:Computer Science
Age:30
Number:9895881182
Salary:67000
The manager's Detail
Name:Manu
Age:30
Number:9895881182
Address:9895881182
Salary:67000.0
Specialization:CSE
Department:Computer Science
POLYMORPHISM
Program
abstract class Shape{
public abstract void numberOfSides();
}
class Rectangle extends Shape{
public void numberOfSides(){
System.out.println("Number of Sides = 4");
}
}
class Triangle extends Shape{
public void numberOfSides(){
System.out.println("Number of Sides = 3");
}
}
class Hexagon extends Shape{
public void numberOfSides(){
System.out.println("Number of Sides = 6");
}
}
class Test{
public static void main(String args[]){
Rectangle r = new Rectangle();
Triangle t = new Triangle();
Hexagon h = new Hexagon();
r.numberOfSides();
t.numberOfSides();
h.numberOfSides();
}
}
Output
Number of Sides = 4
Number of Sides = 3
Number of Sides = 6
FILE HANDLING – READER/WRITER
Program
import java.io.*;
class Test{
try{
int i;
while((i=fin_1.read()) != -1){
fout.write(i);
while((i=fin_2.read()) != -1){
fout.write(i);
fin_1.close(); fin_2.close();
fout.close();
catch(IOException e){
System.out.println(e.getMessage());
}
}
}
Output
file1.txt -> a b c d
file2.txt -> e f g h
file3.txt -> a b c de f g h
FILE HANDLING FUNCTION
Program
import java.io.*;
class Test{
public static void main(String args[]){ try{
FileReader fin = new FileReader("test.txt"); FileWriter
fout = new FileWriter("copy.txt");
int i;
while((i=fin.read()) != -1){
fout.write(i);
}
fin.close();
fout.close();
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
Output
Case1:
Case 2:
STRING TOKENIZER
Program
import java.util.Scanner;
import java.util.StringTokenizer;
public class StrTok{
public static void main(String[] args){
String S'
int sum=0,tmp=0;
System.out.print("Enter line of integers =");
Scanner sc = new Scanner(System.in);
s=sc.nextLine();
StringTokenizer str-new StringTokenizer(s),;
System.out.print("Digits in the line are = ");
while(str.hasMoreTokens()
{
tmp = Integer.parseInt(str.nextToken();
System.out.print(tmp+".t");
sum=sum+tmp;
}
System.out.print("rSum of digit= ");
System.out.print(sum);
}
}
Output
Sum of digit = 44
Program
import java.util.Scanner;
class Test{
if(b == 0){
throw new ArithmeticException("Divide by zero is not possible");
}
else{
System.out.println("Result = "+a/b);
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
} finally{
}
}
System.out.println("End of Program");
}
Output
Enter the value of x and y
120
End of Program
MULTI-THREADED PROGRAM
Program
import java.util.Random;
class RandonThread extends Thread{
public void run(){
Random r = new Random();
for(int i=0;i<20;i++){
int n = r.nextInt(100); // i will get a value between 0 and 100
if(n % 2 == 0){
new Even(n).start();
}
else{
new Odd(n).start();
}
class Test{
public static void main(String args[]){
RandonThread r = new
RandonThread(); r.start();
}
}
Output
Cube of 25 =15625
Square of 76 =5776
Cube of 51 =132651
Square of 14 =196
Cube of 23 =12167
Square of 18 =324
Cube of 91 =753571
Cube of 51 =132651
Square of 16 =256
Cube of 61 =226981
Cube of 3 =27
Cube of 99 =970299
Square of 76 =5776
Square of 30 =900
Cube of 65 =274625
Cube of 5 =125
Square of 32 =1024
Cube of 5 =125
Cube of 3 =27
Cube of 19 =6859
THREAD SYNCHRONIZATION
Program
class Display{
public synchronized void print(String msg){
System.out.print("["+msg);
try{
Thread.sleep(1000);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("]");
}
private Display d;
private String msg;
public SyncThread(Display d,String
msg){ this.d=d;
this.msg = msg;
}
class Test{
public static void main(String args[]){
Display d = new Display();
SyncThread t1 = new
SyncThread(d,"Hello"); SyncThread t2 =
new SyncThread(d,"World"); t1.start();
t2.start();
}
}
Output
[Hello]
[World]
SIMPLE CALCULATOR
Program
import javax.swing.*;
import java.awt.event.*;
class Calculator extends JFrame implements ActionListener{
private JTextField t1;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton b10;
private JButton b11;
private JButton b12;
private JButton b13;
private JButton b14;
private JButton b15;
private JButton b16;
private Integer res;
private String operation;
public Calculator(){
setLayout(null);
setSize(640,480);
t1 = new JTextField();
t1.setBounds(100,100,200,30);
b1 = new JButton("1");
b1.setBounds(100,140,50,30);
b2 = new JButton("2");
b2.setBounds(150,140,50,30);
b3 = new JButton("3");
b3.setBounds(200,140,50,30);
b4 = new JButton("+");
b4.setBounds(250,140,50,30);
// Third Row
b5 = new JButton("4");
b5.setBounds(100,170,50,30);
b6 = new JButton("5");
b6.setBounds(150,170,50,30);
b7 = new JButton("6");
b7.setBounds(200,170,50,30);
b8 = new JButton("-");
b8.setBounds(250,170,50,30);
// Fourth Row
b9 = new JButton("7");
b9.setBounds(100,200,50,30);
b10 = new JButton("8");
b10.setBounds(150,200,50,30);
b11 = new JButton("9");
b11.setBounds(200,200,50,30);
b12 = new JButton("*");
b12.setBounds(250,200,50,30);
// Fourth Row
b13 = new JButton("/");
b13.setBounds(100,230,50,30);
b14 = new JButton("%");
b14.setBounds(150,230,50,30);
b15 = new JButton("=");
b15.setBounds(200,230,50,30);
b16 = new JButton("C");
b16.setBounds(250,230,50,30);
add(t1);add(b1);add(b2);
add(b3);add(b4);add(b5);
add(b6);add(b7);add(b8);
add(b9);add(b10);add(b11);
add(b12);add(b13);add(b14);
add(b15);add(b16);
b1.addActionListener(this);b2.addActionListener(this);
b3.addActionListener(this);b4.addActionListener(this);
b5.addActionListener(this);b6.addActionListener(this);
b7.addActionListener(this);b8.addActionListener(this);
b9.addActionListener(this);b10.addActionListener(this);
b11.addActionListener(this);b12.addActionListener(this);
b13.addActionListener(this);b14.addActionListener(this);
b15.addActionListener(this);b16.addActionListener(this);
}
public void doAction(String op){
if(operation == null){
operation = op;
res = Integer.parseInt(t1.getText());
t1.setText("");
}
else{
switch(operation){
case "+": res = res + Integer.parseInt(t1.getText());
break;
case "-": res = res - Integer.parseInt(t1.getText());
break;
case "/": try{
if(t1.getText().equals("0"){
throw new ArithmeticException("Divide by Zero");
}
res = res / Integer.parseInt(t1.getText());
}
catch(ArithmeticException e){
t1.setText(e.getMessage());
operation = null;
res = 0;
}
break;
case "*": res = res * Integer.parseInt(t1.getText());
break;
case "%": res = res % Integer.parseInt(t1.getText());
break;
}
if(op.equals("=")){
t1.setText(res.toString());
res = 0;
operation = null;
}
else{
operation = op;
t1.setText("");
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()== b1)
t1.setText(t1.getText()+"1");
else if(e.getSource()== b2)
t1.setText(t1.getText()+"2");
else if(e.getSource()== b3)
t1.setText(t1.getText()+"3");
else if(e.getSource()== b5)
t1.setText(t1.getText()+"4");
else if(e.getSource()== b6)
t1.setText(t1.getText()+"5");
else if(e.getSource()== b7)
t1.setText(t1.getText()+"6");
else if(e.getSource()== b9)
t1.setText(t1.getText()+"7");
else if(e.getSource()== b10)
t1.setText(t1.getText()+"8");
else if(e.getSource()== b11)
t1.setText(t1.getText()+"9");
else if(e.getSource()== b16){
t1.setText("");
res =0;
operation = null;
}
else if(e.getSource()== b4){
doAction("+");
}
else if(e.getSource()== b8)
doAction("-");
else if(e.getSource()== b12)
doAction("*");
else if(e.getSource()== b13)
doAction("/");
else if(e.getSource()== b14)
doAction("%");
else if(e.getSource()== b15)
doAction("=");
}
public static void main(String args[]){
new Calculator().setVisible(true);
}
}
TRAFFIC LIGHT
Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TrafficLight extends JPanel implements
ActionListener{ private JRadioButton r1;
private
JRadioButton r2;
private
JRadioButton r3;
private Color
red_c; private
Color green_c;
private Color
orange_c; public
TrafficLight(){
setBounds(0,0,600,480);
r1 = new
JRadioButton("Red");
r2 = new
JRadioButton("Green")
;
r3 = new
JRadioButton("Orange");
ButtonGroup group = new
ButtonGroup();
r1.setSelected(true);
group.ad
d(r1);
group.ad
d(r2);
group.ad
d(r3);
add(r1);
add(r2);
add(r3);
red_c = Color.red;
green_c =
getBackground ();
orange_c =
getBackground();
r1.addActionListener(t
his);
r2.addActionListener(t
his);
r3.addActionListener(t
his);
red_c = Color.red;
green_c =
getBackground ();
orange_c =
getBackground();
}
else if(r2.isSelected() ==
true){ red_c =
getBackground ();
green_c =
Color.green;
orange_c = getBackground();
else if(r3.isSelected() ==
true){ red_c =
getBackground ();
green_c =
getBackground();
orange_c =
Color.orange;
}
repaint();
}
super.paintComponent(g); g.drawOval(50,50,50,50);
g.drawOval(50,110,50,50); g.drawOval(50,170,50,50);
g.setColor(red_c); g.fillOval(50,50,50,50); g.setColor(orange_c);
g.fillOval(50,110,50,50); g.setColor(green_c);
g.fillOval(50,170,50,50);
}
class Test{
TrafficLight t = new
TrafficLight(); f1.add(t);
}
}
DOUBLY LINKED LIST
QUICK SORT
Program
import java.util.Scanner;
class LinkedList{
private Node head;
class Node{
}
}
class Test{
public static void main(String [] args){
LinkedList list = new LinkedList();
Scanner sc = new Scanner(System.in);
String choice = "";
while(!choice.equals("4")){
System.out.print("1. Insert at End \n2. Delete From Front \n3. Display
\n4.Exit\n");
System.out.println("Enter the choice:");
choice = sc.nextLine();
switch(choice){
case "1": System.out.print("Enter the number to insert:");
int data = sc.nextInt();
sc.nextLine();
list.insert(data);
System.out.println("Data inserted Successfully");
break;
case "2": list.delete();
break;
case "3": list.display();
break;
case "4": break;
default: System.out.println("Invalid Choice");
}
}
}
Output
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
1
Enter the number to insert:1
Data inserted Successfully
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
1
Enter the number to insert:2
Data inserted Successfully
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
1
Enter the number to insert:3
Data inserted Successfully
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
2
Element 1 got deleted
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
3
2 3
1. Insert at End
2. Delete From Front
3. Display
4.Exit
Enter the choice:
4
QUICK SORT
Program
import java.util.Scanner; class Test{
public static void quickSort(String A[],int p,int r){
if(p<r){
int q =
partition(A,p,r);
quickSort(A,p,q-
1);
quickSort(A,q+1,
r);
}
quickSort(A,0,n-1);
System.out.println("After Quick Sort");
for(int i =0;i<n;i++)
System.out.println(A[i]);
}
}
Output
Olivia
Emma
Ava
Sophia
After Quick Sort
Ava
Emma
Olivia
Sophia
Zara