Posts

Showing posts from April 9, 2020

Write an small application in Java to develop Banking Application in which user deposits the amount Rs 1000.00 and then start withdrawing of Rs 400.00, Rs 300.00 and it throws exception "Not Sufficient Fund" when user withdraws Rs. 500 thereafter.

Image
Write an small application in Java to develop Banking Application in which user deposits the amount Rs 1000.00 and then start withdrawing of Rs 400.00, Rs 300.00 and it throws exception "Not Sufficient Fund" when user withdraws Rs. 500 thereafter. import java.util.*; public class Bank {     float fund;     void deposit(float amount)     {         fund=amount;     }     void withdraw(float money) throws Exception     {         float newFund=fund-money;         if(newFund<500)         {             throw new Exception("Not Sufficient Fund");         }         else         {             fund=newFund;             System.out.println("Balance After Withdraw : "+fund);         }     }         public static void main(String arg[])         {             Bank b=new Bank();             b.deposit(1000.00f);             try             {                 float money;                 Scanner sc=new Scanner

Write an application that illustrates method overriding.

Write an application that illustrates method overriding. class A { void show() { System.out.println("Inside Class-A"); } } class B extends A { // Overriding method show of class A void show() { System.out.println("Inside Class-B"); } } public class Example { public static void main(String[] args) { B obj = new B(); obj.show(); //show() method of class-B will be called. } } OUTPUT:- Inside Class-B Also demonstrate accessibility rules in inside and outside packages. package p1; public class Protection { int n = 1; public int n_pub = 2; } public class Example { public static void main(String[] args) { Protection p1 = new Protection(); System.out.println("n = " + p1.n); System.out.println("n_pub = " +p1.n_pub); } } OUTPUT:- n = 1 n_pub = 2

Write a programm in java to find maximum of three numbers using conditional operator

Image
Write a programm in java to find maximum of three numbers using conditional operator import java.util.*; public class Maximum {             public static void getMaximum(int a,int b, int c)           {                      int d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);         System.out.println("Largest Number:"+d);                         }                   public static void main(String args[])           {                    int a,b,c;                                    Scanner ob = new Scanner(System.in);                                       System.out.println("Enter the Number A : ");                                     a = ob.nextInt();                                           System.out.println("Enter the Number B : ");                                      b = ob.nextInt();                                           System.out.println("Enter the Number C : ");                  

Write a program that executes two threads. One thread will print the even numbers and the another thread will print odd numbers from 1 to 50.

Write a program that executes two threads. One thread will print the even numbers and the another thread will print odd numbers from 1 to 50. class NewThread extends Thread { NewThread(String threadname) { super(threadname); } public void run() { if( getName().equals("Odd")==true) { for(int i=1;i<=50;i=i+2) { System.out.print(" " + i); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Exception Occurred."); } } } else { for(int i=2;i<=50;i=i+2) { System.out.print(" " + i); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Exception Occurred."); } } } } } public class Example { public static void main(String[] args) { NewThread t1 = new NewThread("Odd"); NewThread t2 = new NewThread("Even"); t1.start(); t2.start(); } } OUTPUT:- 1 2 4 3

Write a program that executes two threads. One thread displays “Thread1” every 2,000 milliseconds, and the other displays “Thread2” every 4,000 milliseconds. Create the threads by extending the Thread class.

Write a program that executes two threads. One thread displays “Thread1” every 2,000 milliseconds, and the other displays “Thread2” every 4,000 milliseconds. Create the threads by extending the Thread class. class NewThread extends Thread { NewThread(String threadname) { super(threadname); } public void run() { if( getName().equals("Thread1")==true) { for(int i=1;i<=5;i++) { System.out.println("Thread1"); try { Thread.sleep(2000); } catch(InterruptedException e) { System.out.println("Exception Occurred."); } } } else { for(int i=1;i<=5;i++) { System.out.println("Thread2"); try { Thread.sleep(4000); } catch(InterruptedException e) { System.out.println("Exception Occurred."); } } } } } public class Example { public static void main(String[] args) { NewThread t1 = new NewThread("Thread1"); NewThread t2 = new NewThread(&quo

Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object.

Image
Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object. public class OverloadCopyConst {     int n;     OverloadCopyConst()     {         System.out.println("Default Constructor");     }         OverloadCopyConst(int x, int y)     {         System.out.println("Addition is "+(x+y));     }         OverloadCopyConst(int a)     {         n=a;         System.out.println("Number is "+n);                  int i,j,flag;                 for(i=1;i<=n;i++)         {             flag=0;             for(j=2;j<=i/2;j++)             {                 if(i%j==0)                 {                     flag++;                     break;                 }             }                         if(flag==0)             {                 System.out.println("Prime is "+i);             }