Color example using Applet

import java.awt.*;
import java.applet.*;

public class Color1 extends Applet {

  public void paint (Graphics g)
  {

    Color Pigment;
    int Red;
    int Green;
    int Blue;
    int X;
    Red = 255;
    Green = 0;
    Blue = 0;
    X = 0;
    while (X < 255) {
      Pigment = new Color (Red, Green, Blue);
      g.setColor (Pigment);
      g.drawLine (X, 0, X, 100);
      Red = Red - 1;
      Green = Green + 1;
      X = X + 1;
    }
    while (X < 511) {
      Pigment = new Color (Red, Green, Blue);
      g.setColor (Pigment);
      g.drawLine (X, 0, X, 100);
      Green = Green - 1;
      Blue = Blue + 1;
      X = X + 1;
    }
  }
}
READ MORE - Color example using Applet

Program to demonstrate - toString()

class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?



CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}


public String toString()
{
return "CD details : \nTitle : "+title + "\nLength : " + length + "\nAvail : "+avail;
}

}

class toStringDemo
{
public static void main(String args[])
{
CD cd=new CD("Jaws", 120, true);
String s="CD : \n"+cd;
System.out.println(cd);
System.out.println(s);
}
}
/*
CD details :
Title : Jaws
Length : 120
Avail : true

CD :
CD details :
Title : Jaws
Length : 120
Avail : true
*/
READ MORE - Program to demonstrate - toString()

Program to demonstrate inheritance (Example-2)



class Car
{
    public String make;
    protected int weight;
    private String color;
    private Car(String make,int weight,String color)
    {
        this.make=make;
        this.weight=weight;
        this.color=color;
    }
    public Car()
    {
        this("unknown",-1,"white");
    }
}

class ElectricCar extends Car
{
    private int rechargeHour;
    public ElectricCar()
    {
        this(10);
    }

    private ElectricCar(int charge)
    {
        super();
        rechargeHour=charge;
    }
}

class TestCar
{
    public static void main(String args[])
    {
        Car mycar1,mycar2;
        ElectricCar myelec1,myelec2;
        mycar1=new Car();
        mycar2=new Car("Ford",1200,"Green");
        myelec1=new ElectricCar();
        myelec2=new ElectricCar(15);
    }
}

READ MORE - Program to demonstrate inheritance (Example-2)

Program to demonstrate super() in java



class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}


}


class Movie extends CD
{

String director;          // name of the director
String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}

public void show()
{
System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
System.out.println("Director : "+director+"\nRating : "+rating);

}
}




class SuperObjRef
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
CD cd=new CD();
m.show();

cd=m;      //assign movie reference to CD reference

cd.title="Titanic";        //legal
cd.length=120;             //legal
cd.avail=true;             //legal
cd.director="Spielberg";   //illegal
cd.rating="PG";            //illegal
cd.show();                 //illegal
}
}

/*
Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG

Title : null
Length : 0
Avail : false
Director : null
Rating : null

Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG
*/
READ MORE - Program to demonstrate super() in java

Program to demonstrate super() in java



class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}


}


class Movie extends CD
{

String director;          // name of the director
String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}

public void show()
{
System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
System.out.println("Director : "+director+"\nRating : "+rating);

}
}




class SuperObjRef
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
CD cd=new CD();
m.show();

cd=m;      //assign movie reference to CD reference

cd.title="Titanic";        //legal
cd.length=120;             //legal
cd.avail=true;             //legal
cd.director="Spielberg";   //illegal
cd.rating="PG";            //illegal
cd.show();                 //illegal
}
}

/*
Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG

Title : null
Length : 0
Avail : false
Director : null
Rating : null

Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG
*/
READ MORE - Program to demonstrate super() in java

Program to demonstrate static variables, methods in java

class UseStaticMember
{
    //static variable
static int x=4;

//static method
static void display()
{
    System.out.println("x : "+x);
}

}

class StaticmemDemo2
{
public static void main(String args[])
{
System.out.println("x : "+UseStaticMember.x);
UseStaticMember.display();
}
}
/*
x : 4
x : 4
*/
READ MORE - Program to demonstrate static variables, methods in java

Program to demonstrate static variables in java




class StaticDemo1
{
static int x=4;
static int y;
static void display(int a)
{
System.out.println("x : "+x);
System.out.println("y : "+y);
System.out.println("a : "+a);
}
static {
System.out.println("Static block=====");
y=x*x;
}

public static void main(String args[])
{
display(10);
}
}
/*
Static block=====
x : 4
y : 16
a : 10
*/
READ MORE - Program to demonstrate static variables in java

Program to demonstrate Single inheritance

//Single inheritance

//create a superclass
class A
{
    int a1;            //public by default
    protected int a2;   //protected to A
    void showa1a2()
    {
            System.out.println("a1 : "+a1+", a2 : "+a2);
    }

}

//create a sunclass by extending A
class B extends A
{
    int b;
    void showb()
    {
        System.out.println("b : "+b);
    }
    void sum()
    {
        System.out.println("a1+a2+b : "+(a1+a2+b));
    }

}

class SimpleInheritance2
{
    public static void main(String args [])
    {

        B subob=new B();


        subob.a1=10;
        subob.a2=20;
        subob.b=30;
        subob.showa1a2();
        subob.showb();
        subob.sum();
    }
}

/*output
a1 : 10, a2 : 20
b : 30
a1+a2+b : 60
*/
READ MORE - Program to demonstrate Single inheritance

Program to demonstrate inheritance

//Simple example of inheritance

//create a superclass
class A
{
    private int a;
    void showa()
    {
        System.out.println("a : "+a);
    }
}

//create a sunclass by extending A
class B extends A
{
    int b;
    void showb()
    {
        System.out.println("b : "+b);
    }
    void sum()
    {
        System.out.println("a+b : "+(a+b));
    }

}

class SimpleInheritance
{
    public static void main(String args [])
    {
        A superob=new A();
        B subob=new B();

        //the superclass may be used by itself
        superob.a=10;
        System.out.println("Contents of superob : ");
        superob.showa();

        //the subclass has access to all public members of its superclass*/
        subob.a=20;
        subob.b=30;
        System.out.println("Contents of subob : ");
        subob.showa();
        subob.showb();

        System.out.println("Sum of a and b : ");
        subob.sum();
    }
}

/*output
Contents of superob :
a : 10
Contents of subob :
a : 20
b : 30
Sum of a and b :
a+b : 50
Press any key to continue . . .
*/
READ MORE - Program to demonstrate inheritance

Program to demonstrate movie database using Method overriding(Example-2))




//Method overriding
class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}

public void show()
{
System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
}

}


class Movie extends CD
{

String director;          // name of the director
String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}

public void show()
{

System.out.println("Director : "+director+"\nRating : "+rating);
}
}




class OverrideMethod1
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
m.show();
}
}

/*
Director : Spielberg
Rating : PG
*/
READ MORE - Program to demonstrate movie database using Method overriding(Example-2))

Program to demonstrate movie database using Method overriding

 Program to demonstrate movie database using Method overriding

//Method overriding
class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}

public void show()
{
System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
}

}


class Movie extends CD
{

String director;          // name of the director
String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}

public void show(String s)
{
System.out.println(s);
System.out.println("Director : "+director+"\nRating : "+rating);
}
}




class OverloadMethod
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
m.show();
m.show("This is subclass's show method");
System.out.println(m);
}
}

/*Title : Jaws
Length : 120
Avail : true
This is subclass's show method
Director : Spielberg
Rating : PG
*/
READ MORE - Program to demonstrate movie database using Method overriding

Program to demonstrate movie database

 Program to demonstrate movie database

class CD
{
private String title;           // name of the item
private int length;             // number of minutes
private boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}

void DisplayCD()
{
    System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
}
}


class Movie extends CD
{

private String director;          // name of the director
private String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}
void DisplayMovie()
{
    System.out.println("Director : "+director+"\nRating : "+rating);
}
}


class DocumentaryFilm extends Movie
{
private String subject;
DocumentaryFilm(DocumentaryFilm df)
{
super(df);
subject=df.subject;
}

DocumentaryFilm(String t, int l,boolean a,String d,String r,String s)
{
super(t,l,a,d,r);
subject=s;
}

DocumentaryFilm()
{
super();
subject=null;
}

public void DisplayDocumentaryFilm()
{

System.out.println("Subject : "+subject);
}
}

class Multilevel
{
public static void main(String args[])
{
DocumentaryFilm df=new DocumentaryFilm("Take care", 120, true,"Spielberg", "PG","Self defense");
df.DisplayCD();
df.DisplayMovie();
df.DisplayDocumentaryFilm();
}
}
/*
Title : Take care
Length : 120
Avail : true
Director : Spielberg
Rating : PG
Subject : Self defense
*/
READ MORE - Program to demonstrate movie database

Program to find which day of week today using Interface






Program to find which day of week today  using Interface
 


interface Week
{
int Monday=1;
int Tuesday=2;
int Wednesday=3;
int Thursday=4;
int Friday=5;
int Saturday=6;
int Sunday=7;
}

class Day implements Week
{
void display(int day)
{
switch(day)
{
case Monday : System.out.println("\n Its Monday");
    break;
case Tuesday : System.out.println("\n Its Tuesday");
    break;
case Wednesday : System.out.println("\n Its Wednesday");
    break;
case Thursday : System.out.println("\n Its Thursday");
    break;
case Friday : System.out.println("\n Its Friday");
    break;
case Saturday : System.out.println("\n Its Saturday");
    break;
case Sunday : System.out.println("\n Its Sunday");
    break;
}
}
}

class InterfaceTest3
{
public static void main(String args[])
{
Day day=new Day();
day.display(5);
}
}

/*
 Its Friday
*/
READ MORE - Program to find which day of week today using Interface

Program to find which day of week today using Interface






Program to find which day of week today  using Interface
 


interface Week
{
int Monday=1;
int Tuesday=2;
int Wednesday=3;
int Thursday=4;
int Friday=5;
int Saturday=6;
int Sunday=7;
}

class Day implements Week
{
void display(int day)
{
switch(day)
{
case Monday : System.out.println("\n Its Monday");
    break;
case Tuesday : System.out.println("\n Its Tuesday");
    break;
case Wednesday : System.out.println("\n Its Wednesday");
    break;
case Thursday : System.out.println("\n Its Thursday");
    break;
case Friday : System.out.println("\n Its Friday");
    break;
case Saturday : System.out.println("\n Its Saturday");
    break;
case Sunday : System.out.println("\n Its Sunday");
    break;
}
}
}

class InterfaceTest3
{
public static void main(String args[])
{
Day day=new Day();
day.display(5);
}
}

/*
 Its Friday
*/
READ MORE - Program to find which day of week today using Interface

Program to find area of rectangle and circle using Interface

Program to find area of rectangle and circle using Interface

interface Area  //interface defined
{
    float pi=3.14F;
    float compute(float x, float y);
}

class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}

}

class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class InterfaceTest2
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area a;
a=rect;
System.out.println("Area of rectangle : "+a.compute(5,10));
a=cir;
System.out.println("Area of circle : "+a.compute(5,0));

}
}
/*
Area of rectangle : 50.0
Area of circle : 78.5
*/
READ MORE - Program to find area of rectangle and circle using Interface

Program to find area of rectangle using Interface

Program to find area of rectangle  using Interface

interface Area  //interface defined
{
    float pi=3.14F;
    float compute(float x, float y);
}

class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}

void message()
{
System.out.println("Hi...this is first program based on interface");
}

}

/*class Triangle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}*/
class InterfaceTest1
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
rect.message();
System.out.println("Area of rectangle : "+rect.compute(5,10));
}
}
/*
Hi...this is first program based on interface
Area of rectangle : 50.0
*/
READ MORE - Program to find area of rectangle using Interface

Program to demonstrate Students Marks using Interface

class student
{
    int roll_number;
        void get_number(int a)
        {
                roll_number = a;
        }
        void put_number()
        {
                System.out.println("Roll No.  : "+roll_number);
        }
}

class test extends student
{

           float sem1, sem2;
        void get_marks (float s1, float s2)
        {
                sem1 = s1;
                sem2 =  s2;
        }
        void put_marks()
        {
                 System.out.println("Marks obtained  : ");
                 System.out.println("Sem1 = "+sem1);
                 System.out.println("Sem2 = "+sem2);
        }
}

interface sports
{
        float score=6.0F;
        void put_score();

}

class Result extends test implements sports
{
        float total;
        void display()
        {
            total = sem1 + sem2 + score;
            put_number( );
            put_marks( );
            put_score( );
            System.out.println("Total Marks : "+total);
        }
        public void put_score()
        {
                 System.out.println("Sports weight : "+score);
        }

}


class Hybrid
{
public static void main(String args[])
{
Result R1=new Result();
R1.get_number(123);
R1.get_marks(1200,1000);

R1.display( );
}
}
/*
Roll No.  : 123
Marks obtained  :
Sem1 = 1200.0
Sem2 = 1000.0
Sports weight : 6.0
Total Marks : 2206.0
*/
READ MORE - Program to demonstrate Students Marks using Interface

Program to demonstrate Interface in Java

Program to demonstrate Interface in Java


interface NewShape
{
    void draw();
}

interface Circle extends NewShape
{
    void getRadius();
    int radious=10;
}

class NewCircle implements Circle
{
    public void getRadius()
    {
        System.out.println(radious);
    }
}
class ExtendInterface extends NewCircle
{
    public static void main(String args[])
    {
        Circle nc=new NewCircle();
        nc.getRadius();
    }
}


         //NewCircle is not abstract and does not override abstract method draw() in NewShape
READ MORE - Program to demonstrate Interface in Java

Program to demonstrate Employee Informatiom by passing parameters in constructors of different class


Program to demonstrate Employee Informatiom by passing parameters in constructors of different class

class staff
{

    private int code;
    private String name;
    private String address;

    staff(int c,String n,String a)
    {
        code=c;
        name=n;
        address=a;
    }

    void showdata()
    {
          System.out.println("Code = "+code);
          System.out.println("Name = "+name);
          System.out.println("Address = "+address);
    }
}


class teacher extends staff
{
    private    String subject;
    private String publication;

    teacher(int c,String n,String a,String s,String p)
    {
        super(c,n,a);
        subject=s;
        publication=p;
    }

    void showdata()
    {
        super.showdata();
        System.out.println("Subject = "+subject);
        System.out.println("Publication = "+publication);
    }
}

class officer extends staff
{
    private    String grade;

    officer(int c,String n,String a,String g)
    {
        super(c,n,a);
        grade=g;
    }

    void showdata()
    {
        super.showdata();
        System.out.println("Grade = "+grade);
    }
}

class typist extends staff
{
    private int speed;

    typist(int c,String n,String a,int s)
    {
        super(c,n,a);
        speed=s;
    }

    void showdata()
    {
        super.showdata();
        System.out.println("Speed = "+speed);
    }
}


class casual extends typist
{
    private    int dailywages;

    casual(int c,String n,String a,int s,int dw)
    {
        super(c,n,a,s);
        dailywages=dw;
    }

    void showdata()
    {
        super.showdata();
        System.out.println("Dailywages = "+dailywages);
    }
}

class regular extends typist
{
    private    int pay;

    regular(int c,String n,String a,int s,int p)
    {
        super(c,n,a,s);
        pay=p;
    }

    void showdata()
    {
        super.showdata();
        System.out.println("Monthly Pay = "+pay);
    }
}

class Employees
{

public static void main(String args[])
{
 casual c=new casual(1001,"Sharad","Borivali",40,100);
 officer o=new officer(2001,"Shrikanth","Dadar","A");
 teacher t=new teacher(3001,"Smita","Parel","Maths","Tect-Max Publications");
 regular r=new regular(4001,"Anil","Vidyavihar",30,5000);

 c.showdata();
 o.showdata();
 t.showdata();
 r.showdata();
}
}
READ MORE - Program to demonstrate Employee Informatiom by passing parameters in constructors of different class

Program to demonstrate Dynamic method dispatch

 Program to demonstrate Dynamic method dispatch


 //Dynamic method dispatch
class CD
{
void message()
{
System.out.println("Inside CD's method");
}
}

class Movie extends CD
{
//override message
void message()
{
System.out.println("Inside Movie's method");
}
}

class DocumentoryFilm extends CD
{
//override message
void message()
{
System.out.println("Inside DocumentoryFilm's method");
}
}

class DynamicDispatch
{
public static void main(String args[])
{
CD cd=new CD();   //object of class CD
Movie m=new Movie();  //object of class Movie
DocumentoryFilm df=new DocumentoryFilm();   //objet of class DocumentoryFilm
CD r;                  //obtain a reference of type CD

r=cd;                   // r refers to CD object
r.message();

r=m;                   // r refers to Movie object
r.message();

r=df;                   // r refers to DocumentoryFilm object
r.message();
}
}

/*
Inside CD's method
Inside Movie's method
Inside DocumentoryFilm's method
*/
READ MORE - Program to demonstrate Dynamic method dispatch

Program to demonstrate Constructors


class CD
{
CD()
{
System.out.println("Inside CD's constructor");
}
}

class Movie extends CD
{
Movie()
{
System.out.println("Inside Movie's constructor");
}
}

class DocumentoryFilm extends Movie
{
DocumentoryFilm()
{
System.out.println("Inside DocumentoryFilm's constructor");
}
}

class ConsOrder
{
public static void main(String args[])
{
DocumentoryFilm df=new DocumentoryFilm();
}
}
READ MORE - Program to demonstrate Constructors

Program to demonstrate Company Record of Person

Program to demonstrate Company Record of Person

//super class
class Person
{
    protected String name;
    protected int code;

    Person(String n,int c)
    {
        name=n;
        code=c;
    }
    void put_namecoade( )
    {
        System.out.println("\n Name of person is: "+name);
        System.out.println("\n Code is: "+code);
    }
}

//intermediate super class
class Account extends Person
{
    protected int pay;

    Account(String n,int c,int p)
    {
        super(n,c);
        pay=p;
    }

    void put_pay( )
    {
        put_namecoade();
        System.out.println("\n Salary : "+pay);
    }
}

//suub class
class Admin extends Account
{
    protected int exp;

    Admin(String n,int c,int p,int e)
    {
        super(n,c,p);
        exp=e;
    }

    void put_exp( )
    {
        put_pay();
        System.out.println("\n Experience : "+exp);
    }

}

class CompanyRecords
{

public static void main( String args[])
{
 Admin m=new Admin("Akshay",1001,10000,5);
 m.put_exp( );
}
}
READ MORE - Program to demonstrate Company Record of Person

Program to demonstrate CD store

Program to demonstrate CD store

class CD
{
String title;
int length;
boolean avail;

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}


}


class Movie extends CD
{

String director;
String rating;

Movie(Movie m)
{
title=m.title;
length=m.length;
avail=m.avail;
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
title=t;
length=l;
avail=a;
director=d;
rating=r;
}

Movie()
{
title=null;
length=0;
avail=false;
director=null;
rating=null;
}

public void show()
{
System.out.println("Titlet : "+title + "\nLength : " + length + "\nAvail : "+avail );
System.out.println("Director : "+director+"\nRating : "+rating);

}
}




class CDStore1
{
public static void main(String args[])
{
Movie m1=new Movie("Jaws", 120, true,"Spielberg", "PG");
Movie m2=new Movie();
Movie m3=new Movie(m1);

m1.show();
m2.show();
m3.show();
}
}
READ MORE - Program to demonstrate CD store

Program to demonstrate Bank Account using Abstract class and Abstract method


Program to demonstrate  Bank Account using Abstract class and Abstract method


abstract class Account
{
        protected int number;
        protected String name;
        Account(int no, String na)
        {
            number=no;
            name=na;
        }
        abstract void display();
}


class SavingsAccount extends Account
{
        int balance;
        SavingsAccount(int no,String na,int ba)
        {
            super(no,na);
            balance=ba;
        }
        void display()
        {
            System.out.println("Savings Account Details --- ");
            System.out.println("Number : "+number);
            System.out.println("Name   : "+name);
            System.out.println("Balance : "+balance);
        }
}



abstract class DepositAccount extends Account
{
        protected int amount;
        protected String maturity_date;
        protected String opening_date;
        DepositAccount(int no,String na,int amt,String md,String od)
        {
            super(no,na);
            amount=amt;
            maturity_date=md;
            opening_date=od;
        }

}

class ShortTerm extends DepositAccount
{
        int no_of_months;
        ShortTerm(int no,String na,int amt,String md,String od,int nm)
        {
            super(no,na,amt,md,od);
            no_of_months=nm;
        }
        void display()
        {
            System.out.println("Short Term Deposit Account Details --- ");
            System.out.println("Number : "+number);
            System.out.println("Name   : "+name);
            System.out.println("Amount : "+amount);
            System.out.println("Maturity Date : "+maturity_date);
            System.out.println("Date of Opening : "+opening_date);
            System.out.println("Duration in Months : "+no_of_months);
    }
}


class LongTerm extends DepositAccount
{
        int no_of_years;

        LongTerm(int no,String na,int amt,String md,String od,int ny)
        {
            super(no,na,amt,md,od);
            no_of_years=ny;

        }

        void display()
        {
            System.out.println("Long Term Deposit Account Details --- ");
            System.out.println("Number : "+number);
            System.out.println("Name   : "+name);
            System.out.println("Amount : "+amount);
            System.out.println("Maturity Date : "+maturity_date);
            System.out.println("Date of Opening : "+opening_date);
            System.out.println("Duration in Years : "+no_of_years);

        }

}



class Bank
{

public static void main(String args[])
{
    SavingsAccount sa=new SavingsAccount(101,"Bhakti Raul",1000000);
    ShortTerm st=new ShortTerm(405,"Samarth Rane",1000000,"1/1/2008", "1/1/2007",12);
    LongTerm lt=new LongTerm(302,"Stavan Chavan",1000000,"1/3/2010","1/3/2006",4);
    sa.display();
    st.display();
    lt.display();


}
}
READ MORE - Program to demonstrate Bank Account using Abstract class and Abstract method

Program to demonstrate Abstract class and Abstract method

 Program to demonstrate Abstract class and Abstract method


abstract class Shape
{

    protected double bs,ht,area;
    void getdata(double b, double h)
    {
    bs=b;
    ht=h;
    }
    abstract void displayarea();

}


class Triangle extends Shape
{

    void displayarea()
    {
        area=0.5*bs*ht;
        System.out.println("Area of triangle is : "+area);
    }
}


class Rectangle extends Shape
{

    void displayarea()
    {
         area=bs*ht;
         System.out.println("Area of rect is : "+area);
    }
}


class Area
{

public static void main(String main[])
{
Triangle t = new Triangle();
Rectangle r =new Rectangle();
t.getdata(3.5,7.5);
t.displayarea();
r.getdata(4,4);
r.displayarea();
}
}

/*
Area of triangle is : 13.125
Area of rect is : 16.0
*/
READ MORE - Program to demonstrate Abstract class and Abstract method

Program to demonstrate System.out.write()

/* Program to demonstrate System.out.write() */

class WriteDemo
{
    public static void main(String args[ ])
    {
        int i; char c ; float f;double d;

        i = 'J';
        c = 'A';
        f = 'V';
        d = 'A';

        System.out.write(i);
        System.out.write(c);
        System.out.write((int)f);   // convertion from float to integer   
        System.out.write((int)d);   // convertion from double to integer   
        System.out.write('\n');   
    }
}   
       
   
READ MORE - Program to demonstrate System.out.write()

Program to read a string and rewrite it in alphabetical order

/* Program to read a string and rewrite it in alphabetical order  */

import java.io.DataInputStream;     // to load DataInputStream class        

class P27
{
    public static void main(String args[ ])
    {
        String str=new String();
        char c[] = new char[15];
        char temp;
        int len=0;
        DataInputStream in = new DataInputStream(System.in);

        try
        {
             System.out.print(" Enter String : ");
             str = in.readLine();
        }
        catch(Exception e) {  System.out.println("I/O Error");   }

        len=str.length();
        c = str.toCharArray();
        for(int i=0;i<=len-1;i++)
           for (int j=i+1; j<=len-1; j++)
           {
              if (c[i] > c[j])
                         {
                           temp = c[i];
                          c[i] = c[j];
                          c[j] = temp;
                   }         
                 }

        System.out.print(" Sorted String is : "); 
        for(int i=0;i<=len-1;i++)
            System.out.print(c[i]);                
    }
}

READ MORE - Program to read a string and rewrite it in alphabetical order

Program to demonstrates the charAt( )

/* Program to demonstrates the - charAt( ) */

class charAtDemo
{
    public static void main(String args[ ])
    {
        String s = "INDIA";
        char ch;

        System.out.println("String is : "+s);
        ch= s. charAt(2);
        System.out.println("Character at Index 2 (i.e. 3rd position) is : "+ch);
    }
}

READ MORE - Program to demonstrates the charAt( )

MCQ's on Java Fundamentals


1. Which is a valid keyword in java?

A. interface
B. Float
C. string
D. unsigned
Click for answer 
A. interface


2. Which is a reserved word in the Java programming language?

A. method
B. array
C. native
D. reference
D. subclasses
Click for answer 
C. native


3. Which will legally declare, construct, and initialize an array?

A. int [] myList = {"1", "2", "3"};
B. int [] myList = (5, 8, 2);
C. int myList [] [] = {4,9,7,0};
D. int myList [] = {4, 3, 7};
Click for answer 
D. int myList [] = {4, 3, 7};


4. Which three are legal array declarations?

1. int [] myScores [];
2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];

A. 1,2,4
B. 2,4,5
C. 2,3,4
D. All are correct.
Click for answer 
A. 1,2,4


5. Which one of the following will declare an array and initialize it with five numbers?

A. Array a = new Array(5);
B. int [] a = {23,22,21,20,19};
C. int a [] = new int[5];
D. int [5] array;
Click for answer 
B. int [] a = {23,22,21,20,19};


6. Which three are valid declarations of a char?
1. char c1 = 064770;
2. char c2 = 'face';
3. char c3 = 0xbeef;
4. char c4 = \u0022;
5. char c5 = '\iface';
6. char c6 = '\uface';

A. 1,2,4
B. 1,3,6
C. 3,5,2
D. 4,1,6
Click for answer 
B. 1,3,6


7. Which is the valid declarations within an interface definition?

A. public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D. protected void methoda(double d1);
Click for answer 
A. public double methoda();


8. Which one is a valid declaration of a boolean?

A. boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no;
Click for answer 
B. 1,3,6


9. Which three are valid declarations of a float?

1. float f1 = -343;
2. float f2 = 3.14;
3. float f3 = 0x12345;
4. float f4 = 42e7;
5. float f5 = 2001.0D;
6. float f6 = 2.81F;

A. 1,2,4
B. 2,3,5
C. 1,3,6
D. 2,4,6
Click for answer 
C. 1,3,6


10. Which is a valid declarations of a String?

A. String s1 = null;
B. String s2 = 'null';
C. String s3 = (String) 'abc';
D. String s4 = (String) '\ufeed';
Click for answer 
A. String s1 = null;

READ MORE - MCQ's on Java Fundamentals

Program to illustrate reading data from keyboard

 Program to illustrate reading data from keyboard

import java.io.DataInputStream;      // load class for reading purpose

class StringtoNumber
{
    public static void main(String args[])
    {
        DataInputStream in = new DataInputStream(System.in); // creating object of class DataInputStream.
        int intNumber =0;
        float floatNumber = 0.0F;

        try
        {
            System.out.print("Enter an integer Number :");
            intNumber = Integer.parseInt(in.readLine());
            System.out.print("Enter a float Number :");
            floatNumber = Float.valueOf(in.readLine()).floatValue();
       
        }
        catch(Exception e) {    }

        System.out.println("Integer Number is : "+intNumber);
        System.out.println("Float Number is : "+floatNumber);
    }
}

       
READ MORE - Program to illustrate reading data from keyboard

Program to demonstrate static variables, methods, and blocks

/* Program to demonstrate static variables, methods, and blocks */

class StaticDemo1
{
    static int a = 5;
    static int b;

    static void display(int c)
    {
        System.out.println(" a = "+a);
        System.out.println(" b = "+b);
        System.out.println(" c = "+c);
    }

    static
    {
        System.out.println(" static block initialized....");
        b = a * 5;
    }

    public static void main(String args[])
    {
        display(40);
    }
}
       
READ MORE - Program to demonstrate static variables, methods, and blocks

Program to Demonstrates- Returning an Object

/* Program to Demonstrates- Returning an Object */

class Rational
{
    int numerator;
    int denominator;

    Rational(int a, int b)
    {
        numerator = a;
        denominator = b;
    }

    Rational()
    {
        numerator = 0;
        denominator = 0;
    }

    // Function accepting object argument & returning object
    Rational sum(Rational n)
    {                                       
        Rational Tobject = new Rational();
        int temp = 0;
        temp += numerator * n.denominator;
        temp += denominator * n.numerator;
        Tobject.numerator = temp;
        Tobject.denominator = denominator * n.denominator;
        return Tobject;
    }

    void display()
    {
        System.out.print( numerator+"/"+denominator);       
    }

}

class ReturnObject
{
    public static void main(String args[])
    {
        Rational r1 = new Rational(2,5);
        Rational r2 = new Rational(3,7);
        Rational r3 = new Rational();
       
        r3 = r1.sum(r2);    // Objects passed as arguments & return type of called function is object

        r1.display();
        System.out.print(" + ");
        r2.display();
        System.out.print(" = ");
        r3.display();
    }
}
READ MORE - Program to Demonstrates- Returning an Object

Program to demonsrates Recursion - Factorial of a given number

/* Program to demonsrates Recursion - Factorial of a given number */

class Factorial
{
    int fact(int n)
    {
        int result;
        if(n==1)
             return 1;
        result=fact(n-1)*n;   // recursive call to fact() function
        return result;
    }
}

class RecursionDemo
{
    public static void main(String args[])
    {
        Factorial f = new Factorial();

        System.out.println(" Factorial of 5 is : "+f.fact(5));
        System.out.println(" Factorial of 6 is : "+f.fact(6));
        System.out.println(" Factorial of 7 is : "+f.fact(7));
        System.out.println(" Factorial of 8 is : "+f.fact(8));
    }
}
READ MORE - Program to demonsrates Recursion - Factorial of a given number

Program to demonstrate Using Object as Parameters

/* Program to demonstrate ‘Using Object as Parameters’ */

class Rectangle
{
    int length;
    int width;

    // Construct clone of an object
    Rectangle(Rectangle obj)
    {       
        length = obj.length;
        width = obj.width;
    }

    //  Constructor used when all values are specified
    Rectangle(int l,int w)
    {       
        length = l;
        width = w;
    }

    //  Constructor used when no values are specified
    Rectangle()
    {   
        length = 0;       
        width = 0;
    }

    //  Constructor used when one value is specified for both,length and width
    Rectangle(int l)
    {       
        length = width = l;
       
    }

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }
   
}


class RectangleDemo9
{
    public static void main(String args[])
    {
        // create Rectangles using the various constructors
        Rectangle rect1 = new Rectangle(20,15);
        Rectangle rect2 = new Rectangle();
        Rectangle rect3 = new Rectangle(20);
        int a;

        Rectangle rect4 = new Rectangle(rect1);
   
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);

        // get area of Third rectangle
        a= rect3.area();         
        System.out.println("Area of Third Rectangle(length:"+rect3.length+", width:"+rect3.width+") is: "+a);

        // get area of clone
        a= rect4.area();         
        System.out.println("Area of Clone(length:"+rect4.length+", width:"+rect4.width+") is: "+a);
    }
}
READ MORE - Program to demonstrate Using Object as Parameters

Program to demonstrate 'this' keyword

/* Program to demonstrate  'this' keyword */

class Rectangle
{
    int length;
    int width;

    //  use 'this' to resolve name-space collisions.
    Rectangle(int length ,int width)
    {       
        this.length = length ;
        this.width = width;
    }

   
    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }
   
}


class RectangleDemo8
{
    public static void main(String args[])
    {
        // create Rectangles
        Rectangle rect1 = new Rectangle(20,15);
        Rectangle rect2 = new Rectangle(25,17);
       
        int a;
   
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);

       
    }
}
READ MORE - Program to demonstrate 'this' keyword

Program to demonstrate Constructor overloading

/* Program to demonstrate  Constructor overloading */

class Rectangle
{
    int length;
    int width;

    //  Constructor used when all values are specified
    Rectangle(int l,int w)
    {       
        length = l;
        width = w;
    }

    //  Constructor used when no values are specified
    Rectangle()
    {   
        length = 0;       
        width = 0;
    }

    //  Constructor used when one value is specified for both,length and width
    Rectangle(int l)
    {       
        length = width = l;
       
    }

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }
   
}


class RectangleDemo7
{
    public static void main(String args[])
    {
        // create Rectangles using the various constructors
        Rectangle rect1 = new Rectangle(20,15);
        Rectangle rect2 = new Rectangle();
        Rectangle rect3 = new Rectangle(20);
        int a;
   
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);

        // get area of Third rectangle
        a= rect3.area();         
        System.out.println("Area of Third Rectangle(length:"+rect3.length+", width:"+rect3.width+") is: "+a);
    }
}
READ MORE - Program to demonstrate Constructor overloading

Program to demonstrate parametrized Constructor

/* Program to demonstrate parametrized Constructor */

class Rectangle
{
    int length;
    int width;

    // This is parameterized Constructor for Rectangle
    Rectangle(int l,int w)
    {       
        length = l;
        width = w;
    }

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }
   
}


class RectangleDemo6
{
    public static void main(String args[])
    {
        // Declare, allocate, and initialize Rectangle Object
        Rectangle rect1 = new Rectangle(20,15);
        Rectangle rect2 = new Rectangle(25,17);
        int a;
   
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);
    }
}
READ MORE - Program to demonstrate parametrized Constructor

Program to demonstrate Constructor

/* Program to demonstrate Constructor */

class Rectangle
{
    int length;
    int width;

    // This is Constructor for Rectangle
    Rectangle()
    {
        System.out.println(" Constructing Rectangle....");
        length = 20;
        width = 15;
    }

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }

   
}


class RectangleDemo5
{
    public static void main(String args[])
    {
        // Declare, allocate, and initialize Rectangle Object
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
        int a;
   
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println(" Area of Rectangle is : "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println(" Area of Rectangle is : "+a);
    }
}

READ MORE - Program to demonstrate Constructor

Program to demonstrate method that takes parameters

/* Program to demonstrate method that takes parameters */

class Rectangle
{
    int length;
    int width;

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }

    // sets values of rectangle
    void setdata(int l,int w)
    {
        length = l;
        width = w;
    }
}


class RectangleDemo4
{
    public static void main(String args[])
    {
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
        int a;
   
        // initialize each rectangle
        rect1.setdata(20,15);
        rect2.setdata(25,17);
       
        // get area of first rectangle
        a = rect1.area();         
        System.out.println(" Area of First Rectangle is : "+a);
       
        // get area of second rectangle
        a= rect2.area();         
        System.out.println(" Area of Second Rectangle is : "+a);
    }
}

READ MORE - Program to demonstrate method that takes parameters

Program to demonstrate Method returning a value

/* Program to demonstrate returning a value */

class Rectangle
{
    int length;
    int width;

    // compute and return Area of a Rectangle
    int area()
    {
        return length*width;
    }
}


class RectangleDemo3
{
    public static void main(String args[])
    {
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
        int a;
   
        //assign values to rect1's instance variables
        rect1.length= 20;
        rect1.width=15;

        //assign different values to rect2's instance variables
        rect2.length= 25;
        rect2.width=17;

        System.out.print(" First Recangle (length : "+rect1.length+" , width : "+rect1.width+") --->");
        a = rect1.area();          // get area of first rectangle
        System.out.println(" Area is : "+a);
       
        System.out.print(" Second Recangle (length : "+rect2.length+" , width : "+rect2.width+") --->");
        a= rect2.area();          // get area of second rectangle
        System.out.println(" Area is : "+a);
    }
}

READ MORE - Program to demonstrate Method returning a value

Program to demonstrate method

/* Program to demonstrate method */

class Rectangle
{
    int length;
    int width;

    // display Area of a Rectangle
    void area()
    {
        System.out.println(" Area is "+(length*width));
    }
}


class RectangleDemo2
{
    public static void main(String args[])
    {
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
           
        //assign values to rect1's instance variables
        rect1.length= 20;
        rect1.width=15;

        //assign different values to rect2's instance variables
        rect2.length= 25;
        rect2.width=17;

        System.out.print(" First Recangle (length : "+rect1.length+" , width : "+rect1.width+") --->");
        rect1.area();          // display area of first rectangle

       
        System.out.print(" Second Recangle (length : "+rect2.length+" , width : "+rect2.width+") --->");
        rect2.area();          // display area of second rectangle
    }
}

READ MORE - Program to demonstrate method

Program to demonstrate the use of other class

/* Program to demonstrate the use of  other class.
   Call this file RectangleDemo1.java */

class Rectangle
{
    int length;
    int width;
}

// This class declares an object of type Rectangle.

class RectangleDemo1
{
    public static void main(String args[])
    {
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
        int area;
   
        //assign values to rect1's instance variables
        rect1.length= 20;
        rect1.width=15;

        //assign different values to rect2's instance variables
        rect2.length= 25;
        rect2.width=17;

        // compute area of first rectangle
        area= rect1.length* rect1.width;
        System.out.println(" Area of Rectangle (length : "+rect1.length+" , width : "+rect1.width+") is : "+area);


        // compute area of second rectangle
        area= rect2.length* rect2.width;
        System.out.println(" Area of Rectangle (length : "+rect2.length+" , width : "+rect2.width+") is : "+area);
    }
}

READ MORE - Program to demonstrate the use of other class

Program to compute value of x1 and x2 of linear equations

 Program to compute value of x1 and x2 of linear equations

import java.io.DataInputStream;     // to load DataInputStream class        

class LinearEq
{
    public static void main(String args[])
    {
        int a=0,b=0,c=0,d=0,m=0,n=0,deno=0;
        double x1=0.0,x2=0.0;
        DataInputStream in = new DataInputStream(System.in);

        System.out.println(" Two Linear Equations are :");
        System.out.println("\t ax1 + bx2 = m ");
        System.out.println("\t cx1 + dx2 = n \n");

        try
        {
                  System.out.print("Enter a : ");
                  a= Integer.parseInt(in.readLine());
            System.out.print("Enter b : ");
            b = Integer.parseInt(in.readLine());
            System.out.print("Enter c : ");
            c = Integer.parseInt(in.readLine());
            System.out.print("Enter d : ");
            d = Integer.parseInt(in.readLine());
            System.out.print("Enter m : ");
            m = Integer.parseInt(in.readLine());
            System.out.print("Enter n : ");
            n = Integer.parseInt(in.readLine());
        }
        catch(Exception e) {  System.out.println("I/O Error");   }

        deno = (a*d)-(c*b);
        if(deno==0)
            System.out.println(" Denominator is zero ");
        else
        {
            x1 = (m*d - b*n)/deno;
            x2 = (n*a - m*c)/deno;
            System.out.println(" x1 = "+(double)x1);
            System.out.println(" x2 = "+(double)x2);
        }
    }
}

               
READ MORE - Program to compute value of x1 and x2 of linear equations

Program to represent Bank Account- Using Constructor

 Program to represent Bank Account- Using Constructor

import java.io.DataInputStream;   // to load DataInputStream class

class Bank
{
    String name,type;
    int acno,bal,wit,dep;

    // To assign initial values by constructor
    Bank(String n, String t,int b)
    {
        name=n;
        type=t;
        bal=b;
    }
   

    // To deposit an amount
    void deposit()
    {
        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);

        try
           {

            System.out.print(" Enter amt to deposit : ");
            dep = Integer.parseInt(in.readLine());
   
            bal=bal+dep;
            show();
        }
        catch(Exception e) {  System.out.println("I/O Error");   }
    }

    // To withdraw an amount after checking balance
    void withdraw()
    {
       
        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);

        try
           {

            System.out.println("\nEnter amt to withdraw : ");
            wit = Integer.parseInt(in.readLine());
            if(bal-wit>=500)
                bal=bal-wit;
            else
                System.out.println("You cannot withdraw....");
            show();
        }

        catch(Exception e) {  System.out.println("I/O Error");   }
    }


    // To display the name and balance
    void show()
    {
        System.out.println("\nDepositor name :"+name);
        System.out.println("Type of Account :"+type);
        System.out.println("Balance :"+bal);
    }
}

class P25
{
    public static void main(String args[ ])
    {
        int choice;
        int ans;
        Bank b1=new Bank("Stavan","Saving",50000);  //providing initial value by constructor
       
        b1.show();

        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);
       
        try
           {
            do
            {
                System.out.println("\n1. Deposit");
                System.out.println("2. Withdraw");
                System.out.print("Enter your choice (1/2) :");
                choice = Integer.parseInt(in.readLine());

                if(choice ==1)
                     b1.deposit();       
                if(choice ==2)
                     b1.withdraw();
                System.out.println("\nDo you want to continue ?(1: yes /0 : No)");
                ans = Integer.parseInt(in.readLine());
            }while(ans==1);
        }
        catch(Exception e) {  System.out.println("I/O Error");   }

    }
}   
READ MORE - Program to represent Bank Account- Using Constructor

Program to represent Bank Account

 Program to represent Bank Account

import java.io.DataInputStream;   // to load DataInputStream class

class Bank
{
    String name,type;
    int acno,bal,wit,dep;

    // To assign initial values
    void getdata()
    {
        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);
        try
           {
            System.out.print(" Enter Name of Account Holder : ");
            name=in.readLine();
            System.out.print(" Enter Type of Account : ");
            type=in.readLine();               
            System.out.print(" Enter initial amount in Account : ");
            bal = Integer.parseInt(in.readLine());
        }
        catch(Exception e) {  System.out.println("I/O Error");   }
    }

    // To deposit an amount
    void deposit()
    {
        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);

        try
           {

            System.out.print(" Enter amt to deposit : ");
            dep = Integer.parseInt(in.readLine());
   
            bal=bal+dep;
            show();
        }
        catch(Exception e) {  System.out.println("I/O Error");   }
    }

    // To withdraw an amount after checking balance
    void withdraw()
    {
       
        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);

        try
           {

            System.out.println("\nEnter amt to withdraw : ");
            wit = Integer.parseInt(in.readLine());
            if(bal-wit>=500)
                bal=bal-wit;
            else
                System.out.println("You cannot withdraw....");
            show();
        }

        catch(Exception e) {  System.out.println("I/O Error");   }
    }


    // To display the name and balance
    void show()
    {
        System.out.println("\nDepositor name :"+name);
        System.out.println("Type of Account :"+type);
        System.out.println("Balance :"+bal);
    }
}

class P24
{
    public static void main(String args[ ])
    {
        int choice;
        int ans;
        Bank b1=new Bank();
        b1.getdata();

       

        //creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);
       
        try
           {
            do
            {
                System.out.println("\n1. Deposit");
                System.out.println("2. Withdraw");
                System.out.print("Enter your choice (1/2) :");
                choice = Integer.parseInt(in.readLine());

                if(choice ==1)
                     b1.deposit();       
                else
                     b1.withdraw();
                System.out.println("\nDo you want to continue ?(1: yes /0 : No)");
                ans = Integer.parseInt(in.readLine());
            }while(ans==1);
        }
        catch(Exception e) {  System.out.println("I/O Error");   }

    }
}   
READ MORE - Program to represent Bank Account

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|