Posts

Custom Exception example for BankAccount

class BankAccount {      int accountNumber;     double accountBalance;     public BankAccount(int accountNumber, double accountBalance) {         this.accountNumber = accountNumber;         this.accountBalance = accountBalance;     }     public int getAccountNumber() {         return accountNumber;     }     public double getAccountBalance() {         return accountBalance;     }           public boolean withdraw (double amount) throws WithdrawLimitException     {         if( amount > accountBalance) {                      throw new WithdrawLimitException("Withdrawing Amount is more than Balance amount.. ");                       ...

Application class for Generic Stack Implementation

class MyThread extends Thread {       MyGenericStack<Integer> iobj;     String thname;       public MyThread(MyGenericStack<Integer> iobj, String n) {         this.iobj = iobj;         thname=n;     }           public void run() {          int no=1001;        for (int i=0;i<40;i++)         {             if (thname.equals("1")) {                         if (iobj.push(new Integer(no))==true) {                     System.out.println("Thread : "+ thname + " has pushed... " + (no++) );                 }             }             else if (t...

Generic Stack Class with Synchronized methods

import java.util.ArrayList; public class MyGenericStack<E> {     E obj;         int capacity;     ArrayList<E> list;     public MyGenericStack(int c) {         capacity=c;         list= new ArrayList<E>(capacity);             }                  public synchronized boolean push(E o){             try { Thread.sleep(1000); } catch (Exception e) { System.out.println(e);     }              if (list.size()<20) {              list.add(o);            notifyAll();            return true;        }                 return false;     }     ...

enum example 2

public class testdays {      public enum DaysofWeek {SUN,MON,TUE,WED,THU,FRI,SAT};         public static void main(String[] args) {                 DaysofWeek dw= DaysofWeek.MON;              System.out.println("Days of Week : "+ DaysofWeek.valueOf("MON") );                       System.out.println(dw);                DaysofWeek[] dws = DaysofWeek.values();                 for(DaysofWeek dd:dws)         {             System.out.println(dd);         }             } }

Static Import

import static java.lang.Math.*; import java.util.Date; import static java.lang.System.*; public class StaticImportdemo {     public static void main(String[] args) {               out.println(new Date());         out.println(sqrt(25));         out.println(PI);         out.println(abs(-245.56));         out.println(random());                 out.println(pow(2, 8));                     } }

JFrame with writing in File using FileWriter and anonymous class example

import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.FileWriter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class FrameDemo  extends JFrame  { JButton btstore; JTextField jtf, jtf2; FileWriter fw ;     FrameDemo()     {         jtf = new JTextField(20);         jtf2 = new JTextField(10);         btstore = new JButton("Store");         setLayout(new FlowLayout());                 add(jtf); add(jtf2);         add(btstore);               btstore.addActionListener(                 new ActionListener() {           ...

enum type having private constructor

public enum SeasonTest {     WINTER(2), SUMMER(3), FALL(4), SPRING(1);       private int st;       SeasonTest(int t) { st=t; }       public int getSeason() { return st; }     public static void main(String[] args) {                 for (SeasonTest ss : SeasonTest.values()) {             System.out.println(ss + " : " + ss.getSeason());         }         SeasonTest s = SeasonTest.SPRING;         System.out.println(s.getSeason());               SeasonTest s1 = SeasonTest.SPRING;         switch(s1) {             case SPRING  : System.out.println("Spring"); break;             case SUMMER :   System.out.println("Summer"); break;       ...