0% found this document useful (0 votes)
21 views

Java Full

Fs

Uploaded by

Shaliq
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)
21 views

Java Full

Fs

Uploaded by

Shaliq
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
You are on page 1/ 49

PALINDROME CHECK

Program
import java.util.Scanner;

class Test{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.print("Enter the String:");

String str = sc.nextLine();

int flag = 0; int len = str.length();

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

Enter the String: malayalam

Palindrome

Enter the String: English

Not Palindrome
FREQUENCY OF CHARACTER IN STRING

Program
import java.util.Scanner;

class Test{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.print("Enter the String:");

String str = sc.nextLine();

System.out.print("Enter the character:");

char ch = sc.nextLine().charAt(0);

int count = 0;

for(int i=0;i<str.length();i++){

if(str.charAt(i) == ch){

count++;

System.out.println("Count of occurence of "+ ch +"="+count);

}
Output

Enter the String: malayalam

Enter the character: m

Count of occurence of m=2


MATRIX MULTIPLICATION

Program
import java.util.Scanner;

class Test{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.print("Enter the order - m1:");

int m1 = sc.nextInt();

System.out.print("Enter the order - n1:");

int n1 = sc.nextInt();

System.out.print("Enter the order - m2:");

int m2 = sc.nextInt();

System.out.print("Enter the order - n2:");

int n2 = sc.nextInt();

if(n1 != m2){

System.out.println("Matrix Multiplication not Possible");

return;

int A[][] = new int[m1][n1];

int B[][] = new int[m2][n2];

int C[][] = new int[m1][n2];

System.out.println("Read Matrix A");


for(int i=0;i<m1;i++){

for(int j=0;j<n1;j++){

System.out.print("A["+i+"]["+j+"]=");

A[i][j] = sc.nextInt();

System.out.println("Read Matrix B");

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++){

C[i][j] += A[i][k] * B[k][j];

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{

private String name;

private int age;

private String phone;

private String address;

private double salary;

public void printSalary(){

System.out.println("Salary="+ salary);

public Employee(String name,int age,String phone,String address,double salary){

this.name = name;

this.age = age;

this.phone = phone;

this.address = address;

this.salary = salary;

public void displayEmployee(){

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);

class Manager extends Employee{

private String specialization;

private String department;

public Manager(String name,int age,String phone,String address,double salary,

String specialization,String department){

super(name,age,phone,address,salary);

this.specialization = specialization;

this.department = department;

public void displayManager(){

displayEmployee();

System.out.println("Specilization ="+specialization);

System.out.println("Department ="+department);

class Officer extends Employee{

private String specialization;

private String department;

public Officer(String name,int age,String phone,String address,double salary,

String specialization,String department){


super(name,age,phone,address,salary);

this.specialization = specialization;

this.department = department;

public void displayOfficer(){

displayEmployee();

System.out.println("Specilization ="+specialization);

System.out.println("Department ="+department);

class Test{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.println("Enter Manager Details");

System.out.print("Name:");

String name = sc.nextLine();

System.out.print("Age:");

int age = sc.nextInt();sc.nextLine();

System.out.print("Phone Number:");

String phone = sc.nextLine();

System.out.print("Address:");

String addr = sc.nextLine();

System.out.print("Salary:");

double salary = sc.nextDouble();sc.nextLine();

System.out.print("Specialization:");
String spec = sc.nextLine();

System.out.print("Department:");

String dept = sc.nextLine();

Manager m = new Manager(name,age,phone,addr,salary,spec,dept);

m.displayManager();

System.out.println("Enter Officer Details");

System.out.print("Name:");

String name1 = sc.nextLine();

System.out.print("Age:");

int age1 = sc.nextInt();sc.nextLine();

System.out.print("Phone Number:");

String phone1 = sc.nextLine();

System.out.print("Address:");

String addr1 = sc.nextLine();

System.out.print("Salary:");

double salary1 = sc.nextDouble();sc.nextLine();

System.out.print("Specialization:");

String spec1 = sc.nextLine();

System.out.print("Department:");

String dept1 = sc.nextLine();

Officer o = new Officer(name1,age1,phone1,addr1,salary1,spec1,dept1);

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{

public static void main(String args[]){

try{

FileReader fin_1 = new FileReader("file1.txt");

FileReader fin_2 = new FileReader("file2.txt");

FileWriter fout = new FileWriter("file3.txt");

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

Enter line of integers = 5 6 10 8 15

Digits in the line are=5 6 10 8 15

Sum of digit = 44

TRY,CATCH,THROWS AND FINALLY

Program
import java.util.Scanner;
class Test{

public static void divide(int a,int b) throws ArithmeticException{

if(b == 0){
throw new ArithmeticException("Divide by zero is not possible");
}

else{
System.out.println("Result = "+a/b);
}

public static void main(String args[]){


int x,y;
try{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of x and
y"); x = sc.nextInt();
sc.nextLine();
y = sc.nextInt();
sc.nextLine(); divide(x,y);
}

catch(ArithmeticException e){
System.out.println(e.getMessage());
} finally{
}

}
System.out.println("End of Program");
}
Output
Enter the value of x and y

120

Divide by zero is not possible

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 Even extends Thread{


private int num;

public Even(int num){


this.num = num;
}

public void run(){


System.out.println("Square of "+num+" ="+num*num);
}

class Odd extends Thread{


private int num;
public Odd(int num){
this.num = num;
}

public void run(){


System.out.println("Cube of "+num +" ="+ num*num*num);
}

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("]");
}

class SyncThread extends Thread{

private Display d;
private String msg;
public SyncThread(Display d,String
msg){ this.d=d;
this.msg = msg;
}

public void run(){


d.print(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);

public void actionPerformed(ActionEvent e){ if(r1.isSelected() == true){

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();
}

public void paintComponent(Graphics g){

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{

public static void main(String args[]){ JFrame f1 = new


JFrame(); f1.setVisible(true); f1.setSize(600,480);
f1.setLayout(null);

TrafficLight t = new
TrafficLight(); f1.add(t);
}

}
DOUBLY LINKED LIST
QUICK SORT

Program
import java.util.Scanner;
class LinkedList{
private Node head;

class Node{

private int data;


private Node left;
private Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
public void insert(int data){
Node temp = new Node(data);
if(head == null){
head = temp;
}
else{
Node ptr = head;
while(ptr.right != null){
ptr = ptr.right;
}
ptr.right = temp;
temp.left = ptr;
}
}
public void delete(){
int x = head.data;
head = head.right;
head.left = null;
System.out.println("Element "+x +" got deleted");
}
public void display(){
if(head == null)
System.out.println("List is Empty");
else{
Node ptr = head;
while(ptr != null){
System.out.print(ptr.data +"\t");
ptr = ptr.right;
}
System.out.println();
}

}
}
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);
}

public static int partition(String A[],int p,int r){ String x =


A[r];
int i = p-1;
for(int j=p;j<=r-1;j++){
if(A[j].compareTo(x)
<=0){ i = i + 1;
String temp =
A[i]; A[i] = A[j];
A[j] = temp;
}
}

String temp = A[i+1]; A[i+1] = A[r];


A[r] = temp; return i +1 ;
}

public static void main(String args[]){


Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit:");
int n = sc.nextInt();
sc.nextLine();
String A[] = new String[n];
System.out.println("Enter the names");
for(int i =0;i<n ;i++){
A[i] = sc.nextLine();
}

quickSort(A,0,n-1);
System.out.println("After Quick Sort");
for(int i =0;i<n;i++)
System.out.println(A[i]);
}

}
Output

Enter the limit:


5
Enter the names
Zara

Olivia
Emma
Ava
Sophia
After Quick Sort

Ava
Emma
Olivia
Sophia
Zara

You might also like