Java Programs

complexaddn.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 class Complex{ int real,imag; Complex(){ real = 5; imag = 10; } Complex(int real,int imag){ this.real = real; this.imag = imag; } void displayComplex(){ System.out.print("\n"+real+"+"+imag+"i"+"\n"); } Complex addComplex(Complex c){ Complex sumComplex = new Complex(); int sumreal = this.real + c.real; int sumimag = this.imag + c.imag; sumComplex.real = sumreal; sumComplex.imag = sumimag; return sumComplex; } } class complexaddn{ public static void main(String args[]){ Complex num1 = new Complex(); Complex num2 = new Complex(2,7); num1.displayComplex(); num2.displayComplex(); Complex sum = num1.addComplex(num2); System.out.print("Sum of the complex numbers are \n"); sum.displayComplex(); } }

copyfile.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.io.*; public class copyfile { public static void main(String args[]){ File fileIn = new File("input.txt"); File fileOut = new File("output.txt"); FileReader ins = null; FileWriter outs = null; try{ ins = new FileReader(fileIn); outs = new FileWriter(fileOut); int ch; int a[]; a = new int[20]; int count = 0; while((ch = ins.read())!=-1){ a[count] = ch; count = count + 1; } while(count>0){ count--; outs.write(a[count]); } } catch(IOException e){ System.out.print("Error found" + e); } finally{ try{ ins.close(); outs.close(); } catch(IOException e){ System.out.println("Error found"+e ); } } } }

dynamicDispatch.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Animal{ void makeSound(){ System.out.print("Unknown\n"); } } class Tiger extends Animal{ void makeSound(){ System.out.print("Grrr\n"); } } class dynamicDispatch{ public static void main(String args[]){ Animal ref; Animal A = new Animal(); ref = A; ref.makeSound(); Tiger B = new Tiger(); B.makeSound(); } }

exceptionTest.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 class divideMethod{ static int divide(int num1, int num2) throws ArithmeticException, NumberFormatException, ArrayIndexOutOfBoundsException{ int result = num1/num2; return result; } } class exceptionTest{ public static void main(String args[]){ try{ int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.print("Result is "+divideMethod.divide(a,b)+'\n'); } catch(ArithmeticException e){ System.out.print("Error found, Cant divide by Zero\n"); } catch(ArrayIndexOutOfBoundsException e){ System.out.print("Insufficient arguments given\n"); } catch(NumberFormatException e){ System.out.print("Invalid datatype entered, please enter integer\n"); } finally{ System.out.print("Thanks\n"); } } }

inheritancedemo.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 //demonstrate inheritance in java by finding area and volume of a room by extending the rectangle class. use proper constructors class Rectangle{ int length,breadth,area; Rectangle(int length, int breadth){ this.length = length; this.breadth = breadth; } Rectangle(){ this.length = 5; this.breadth = 7; } int rectArea(){ area = length*breadth; return area; } } class Room extends Rectangle{ int volume,height; Room(){ super(); height = 7; } Room(int length,int breadth,int height){ super(length,breadth); this.height = height; } int Volume(){ this.volume = this.area*height; return this.volume; } } class inheritancedemo{ public static void main(String args[]){ Room r = new Room(1,2,3); int RoomArea = r.rectArea(); int RoomVolume = r.Volume(); System.out.print("Area = "+RoomArea+"\n"); System.out.print("Volume = "+RoomVolume+"\n"); } }

matrixaddn.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 import java.util.*; class Matrix{ int mat[][],i,j; int row,col; Scanner sc = new Scanner(System.in); Matrix(int row,int col){ mat = new int[row][col]; this.row = row; this.col = col; } void readMatrix(){ for(i=0;i<row;i++){ for(j=0;j<col;j++){ mat[i][j] = sc.nextInt(); } } } void displayMatrix(){ for(i=0;i<row;i++){ System.out.print("\n"); for(j=0;j<col;j++){ System.out.print(mat[i][j]+"\t"); } } } Matrix addMatrix(Matrix B){ Matrix sum = new Matrix(row,col); for(i=0;i<row;i++){ for(j=0;j<col;j++){ sum.mat[i][j] = mat[i][j]+B.mat[i][j]; } } return sum; } } class matrixaddn{ public static void main(String args[]){ int r,c; Scanner sc = new Scanner(System.in); System.out.print("Enter no of rows"); r = sc.nextInt(); System.out.print("Enter no of columns"); c = sc.nextInt(); Matrix A = new Matrix(r,c); System.out.print("Enter values for Matrix1"); A.readMatrix(); Matrix B = new Matrix(r,c); System.out.print("Enter values for Matrix2"); B.readMatrix(); Matrix C = A.addMatrix(B); System.out.print("\nMatrix1"); A.displayMatrix(); System.out.print("\nMatrix2"); B.displayMatrix(); System.out.print("\nSum of the matrix is"); C.displayMatrix(); } }

matrixtranspose.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import java.util.*; class matrixtranspose{ public static void main(String args[]){ int r,c,A[][],B[][]; int i,j; A = new int [50][50]; B = new int [50][50]; Scanner sc = new Scanner(System.in); System.out.print("Enter no of rows"); r = sc.nextInt(); System.out.print("Enter no of columns"); c = sc.nextInt(); System.out.print("Enter the element in the matrix"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ A[i][j] = sc.nextInt(); } } System.out.print("Current matrix is"); for(i=0;i<r;i++){ System.out.print("\n"); for(j=0;j<c;j++){ System.out.print(A[i][j]+"\t"); } } for(i=0;i<c;i++){ for(j=0;j<r;j++){ B[i][j] = A[j][i]; } } System.out.print("Tranposed matrix"); for(i=0;i<c;i++){ System.out.print("\n"); for(j=0;j<r;j++){ System.out.print(B[i][j]+"\t"); } } } }

methodoverloading.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class demoClass{ void addSum(String A, String B){ System.out.print("\nString\n"); String sum = A + B; System.out.print(sum+"\n"); } void addSum(int A, int B){ System.out.print("\nInteger\n"); int sum = A+B; System.out.print(sum+"\n"); } void addSum(float A, float B){ System.out.print("\nFloat\n"); float sum = A+B; System.out.print(sum+"\n"); } } class methodoverloading{ public static void main(String args[]){ demoClass demo = new demoClass(); demo.addSum(3,5); float a = 3; float b = 4; demo.addSum(a,b); String c1 = "Hello"; String c2 = "Hi"; demo.addSum(c1,c2); } }

methodoverriding.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class A{ void Display(){ System.out.print("A"); } } class B extends A{ void Display(){ System.out.print("B is displayed\n"); } } class methodoverriding{ public static void main(String args[]){ B demo = new B(); demo.Display(); } }

multithreading.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 //Program to demonstrate multi threading //Create new threads // 1st thread generates all the numbers upto 10 // 2nd thread generates even numbers upto 10 // First thread should be created using runnable interface // 2nd thread should be created using thread class //Execute both the threads concurrently using the main method class OddNum implements Runnable{ public void run(){ for(int i=1;i<11;i=i+2) System.out.print(i+"\n"); } } class EvenNum extends Thread{ public void run(){ for(int i=2;i<11;i=i+2) System.out.print(i+"\n"); } } class multithreading{ public static void main(String args[]){ OddNum A = new OddNum(); EvenNum B = new EvenNum(); Thread t = new Thread(A); t.start(); B.start(); } }

multithreadingsleep.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 //Implement sleep (Thread.sleep()) class OddNum implements Runnable{ public void run(){ try{ for(int i=1;i<11;i=i+2){ Thread.sleep(100); System.out.print(i+"\n"); } } catch(InterruptedException e){ System.out.print("Interrupted Exception"); } } } class EvenNum extends Thread{ public void run(){ try{ for(int i=2;i<11;i=i+2){ Thread.sleep(101); System.out.print(i+"\n"); } } catch(InterruptedException e){ System.out.print("Interrupted Exception"); } } } class multithreadingsleep{ public static void main(String args[]){ OddNum A = new OddNum(); EvenNum B = new EvenNum(); Thread t = new Thread(A); t.start(); //Prints odd numbers B.start(); ///Prints even numbers } }

multithreadingstring.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class display{ static synchronized void displayString(String num){ System.out.print(num+"\n"); } } class NameOne implements Runnable{ public void run(){ display.displayString("Cristiano"); display.displayString("Ronaldo"); } } class NameTwo extends Thread{ public void run(){ display.displayString("Lionel"); display.displayString("Messi"); } } class multithreadingstring{ public static void main(String args[]){ NameOne A = new NameOne(); NameTwo B = new NameTwo(); Thread t = new Thread(A); t.start(); B.start(); } }

multithreadingsync.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 //Program to demonstrate multi threading //Create new threads // 1st thread generates all the numbers upto 10 // 2nd thread generates even numbers upto 10 // First thread should be created using runnable interface // 2nd thread should be created using thread class //Execute both the threads concurrently using the main method class display{ static synchronized void displayNum(int num){ try{ //Thread.sleep(250); System.out.print(num+"\n"); } catch(Exception e){ System.out.print("..."); } } } class OddNum implements Runnable{ public void run(){ for(int i=1;i<11;i=i+2) display.displayNum(i); } } class EvenNum extends Thread{ public void run(){ for(int i=2;i<11;i=i+2) display.displayNum(i); } } class multithreadingsync{ public static void main(String args[]){ OddNum A = new OddNum(); EvenNum B = new EvenNum(); Thread t = new Thread(A); t.start(); B.start(); } }

packageimporter.java

1 2 3 4 5 6 7 8 9 10 import primepackage.*; import java.util.*; class packageimporter{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.print("Enter number to be checked whether its prime or not"); int num = sc.nextInt(); primenumber.checkPrime(num); } }

palindrome.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 //String handling functions // Check whether a string is a palindrome //str.length =>length //str.charAt(location) gives a char at a particular location import java.util.*; class isBool{ static int checkPalindrome(int stringlen, String s){ for(int i=0;i<stringlen;i++){ if (s.charAt(i)!=s.charAt(stringlen-1-i)){ return 1; } } return 0; } } class palindrome{ public static void main(String args[]){ System.out.print("Enter string"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int stringlen = s.length(); int flag = isBool.checkPalindrome(stringlen,s); if (flag == 1) System.out.print("Not a palindrome\n"); else System.out.print("Palindrome\n"); } }

primenumber.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package primepackage; public class primenumber{ public static void checkPrime(int num){ int flag = 0,i = 0; if (num == 0 || num == 1 || num == 2) System.out.print("The given number is a prime number\n"); else{ for(i=2;i<num/2;i++){ if (num%i == 0){ flag = 1; break; } } if (flag == 0) System.out.print("The given number is a prime number\n"); else System.out.print("The given number is not a prime number\n"); } } }

reversearray.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.*; class reversearray { public static void main(String args[]){ int n,A[],B[]; int i; A = new int [50]; B = new int [50]; System.out.print("Enter length"); Scanner sc = new Scanner(System.in); n = sc.nextInt(); System.out.print("Enter values in array"); for(i=0;i<n;i++){ A[i] = sc.nextInt(); } for(i=0;i<n;i++){ B[i] = A[n-i-1]; } System.out.print("Reverse Array is"); for(i=0;i<n;i++){ System.out.print(B[i]); } } }

swingpgm.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Events implements ActionListener{ JFrame f = new JFrame(); JLabel l1 = new JLabel("First Number"); JTextField t1 = new JTextField(20); JLabel l2 = new JLabel("Second Number"); JTextField t2 = new JTextField(20); JLabel l3 = new JLabel("Result Number"); JTextField t3 = new JTextField(20); JButton b1 = new JButton("+"); JButton b2 = new JButton("-"); JButton b3 = new JButton("*"); JButton b4 = new JButton("/"); JButton b5 = new JButton("CLEAR"); public Events(){ f.setSize(500,500); f.setVisible(true); f.setLayout(new FlowLayout()); f.add(l1); f.add(t1); f.add(l2); f.add(t2); f.add(l3); f.add(t3); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1){ String s1 = t1.getText(); int a = Integer.parseInt(s1); String s2 = t1.getText(); int b = Integer.parseInt(s2); int c = a+b; t3.setText(Integer.toString(c)); } else if(e.getSource()==b2){ String s1 = t1.getText(); int a = Integer.parseInt(s1); String s2 = t1.getText(); int b = Integer.parseInt(s2); int c = a-b; t3.setText(Integer.toString(c)); } else if(e.getSource()==b3){ String s1 = t1.getText(); int a = Integer.parseInt(s1); String s2 = t1.getText(); int b = Integer.parseInt(s2); int c = a*b; t3.setText(Integer.toString(c)); } else if(e.getSource()==b4){ String s1 = t1.getText(); int a = Integer.parseInt(s1); String s2 = t1.getText(); int b = Integer.parseInt(s2); int c = a/b; t3.setText(Integer.toString(c)); } else if(e.getSource()==b5){ t1.setText(""); t2.setText(""); t3.setText(""); } } } public class swingpgm { public static void main(String args[]){ Events e = new Events(); } }