Sunday, January 17, 2010

Abstract Class Program in Java

/*
Salary1.java
-------------------------------------------------------------------
WAP a Java Program to create one Abstract class i.e.
AbstractEmployee which contains Calculate() and Display() Methods.
Employee class inherits AbstractEmployee class which contains TA,DA,HRA,PF,
GS,GD and NetSalary data members.

In this case Display() is Non-Abstract and defind in abstrct class
as well as derived class. So, we have to use super keyward to call
in the subclass version of the Display(). Then super class Display()
will call first then sub class Display() will call.
--------------------------------------------------------------------
TA is 20% of Basic.
DA is 5% of Basic.
Income Tax is 15% of Basic

PF = 5000

GS = Basic+HRA+TA+DA

GD=IT+PF

Display the Employee Details.
-------------------------------------------------------------------
Question:-Can a method declared within abstract class ??
Yes, a non abstract method can be defined with an abstract class.

--------------------------------------------------------------------*/

Answer :


abstract class AbstractEmployee
{
int SlNo;
String Name;
float Basic;

AbstractEmployee(int SlNo, String Name,float Basic)
{

this.SlNo=SlNo;
this.Name=Name;
this.Basic=Basic;
}

abstract void Calculate();

abstract void test();

void Display()
{
System.out.println("APTECH");
System.out.println("NIIT");
System.out.println("SQL STAR");

}
}


class Employee extends AbstractEmployee
{
float ta,da,hra,pf,it,gs,gd,net;


Employee(int SlNo,String Name,float Basic)
{
super(SlNo,Name,Basic);
}

void Calculate()
{
ta=Basic * .20f;
da=Basic * .05f;
it=Basic * .15f;
hra=1000;
pf=5000;
gs=Basic+hra+ta+da;
gd=it+pf;
net=gs-gd;
}

void Display()
{

super.Display();

System.out.println(" SlNo of the Employee is " +SlNo);
System.out.println(" Name of the Employee is"+ Name);
System.out.println(" Basic Salary = "+ Basic);
System.out.println(" TA = " + ta);
System.out.println(" DA = " + da);
System.out.println(" HRA = " + hra);
System.out.println(" IT =" + it);
System.out.println("\n PF = " + pf);
System.out.println("\n GS = " + gs);
System.out.println("\n Net Salary = " + net);

}

void test()
{
System.out.println("Hello");
}

}


class Salary1
{
public static void main(String args[])
{
Employee e1=new Employee(1,"Saroj",25000);
e1. Calculate();
e1.Display();
e1.test();
}
}

Frame Program in Java

import java.awt.*;

import java.awt.event.*;

public class GUIFrame extends Frame
{

private Label l1,l2;

TextField t1,t2,t3;

Button badd;

public GUIFrame()
{

super("GUI Application");

setLayout(new FlowLayout());

l1 = new Label("Enter First Number:");

l2 = new Label("Enter Second Number:");

t1 = new TextField(20);

t2 = new TextField(20);

t3 = new TextField(20);

badd = new Button("Add");

add(l1);

add(t1);

add(l2);

add(t2);

add(t3);

add(badd);


badd.addActionListener(new ListenEvent(this)); /*Registration of the button with the event Listener Interface */

setSize(400,300);

setVisible(true);


}


public static void main(String args[])
{
GUIFrame g = new GUIFrame();
}


}


class ListenEvent implements ActionListener
{


GUIFrame g1;

ListenEvent(GUIFrame g1)
{
this.g1 = g1;
}

public void actionPerformed(ActionEvent e)
{
int n1,n2;

n1 = Integer.parseInt(g1.t1.getText());

n2 = Integer.parseInt(g1.t2.getText());

g1.t3.setText(String.valueOf(n1+n2));

}
}

Exception Program in Java

/* Pass 2 parameters from command Line. First parameter should be an integer , second is also an integer.Try to make the situation, that if user does not pass any parameter, then, there should be a message like "invalid parameter due to ArrayIndexOutOfBoundException".Second parameter for an array Size.If Array Size is -ve then, NegativeArraySizeException will be thrown and there should be a message
"ArraySize should not be -ve". Serach an element which is the args[0] position element from an user created Array. If that element not found from Array then a message should be displayed "Element not Found " else "Element Found";
**************************************************************************************/


import java.io.*; //Predefined package for input at run time


class Exceptionsize
{



public void test(String args[]) throws IOException, ArrayIndexOutOfBoundsException
{


try
{
int x,y, len = 0;

x=Integer.parseInt(args[0]);

y=Integer.parseInt(args[1]);

System.out.println(" User Entered from command Line...");

System.out.println("*************************************");

System.out.print(x + " " +y);

int total = x + y;

System.out.println(" Sum of 2 parameters passed from Command Line is : " + total );

System.out.println("***********************************");

len = args.length;

if(len<2)
{
throw new ArrayIndexOutOfBoundsException();
}


int size, i;

size = Integer.parseInt(args[1]);

int Arr[];


Arr = new int[size];

System.out.println("*********************************");

if(size < 0)
{
throw new NegativeArraySizeException();
}



else
{
System.out.println("Array Size is not -ve ");
}

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(in);


//BufferedReader br = new BufferedReader(new InputStreamReadern(System.in));

for(i = 0;i {
Arr[i] =Integer.parseInt(br.readLine());
}

System.out.println("*************************************");

System.out.println("Display Array Element...");

for(i=0;i {
System.out.print(" " + Arr[i]);
}

System.out.println();

System.out.println("Search that element which is the first parameter from command Line ");

System.out.println("*************************************");

boolean temp = false;

try
{

for(i = 0;i < size; i++)
{

if(Arr[i] == Integer.parseInt(args[0]))
{

System.out.println("***********");
System.out.print("Element Found");

temp = true;

break;
}

}

if(temp==false)
{

System.out.println("******************");
System.out.println(" Element not found ");

System.out.println("************");
}


}

catch(Exception e)
{


}
}

catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Pls enter correct number of Argumets !!! ");
}

catch(NegativeArraySizeException e)
{
System.out.println("Array size should not be Negative ! Pls take positive number for Array size");
}

}





public static void main(String args[])
{
Exceptionsize obj=new Exceptionsize();


try
{
obj.test(args);
}

catch(Exception e)
{

}


finally
{

System.out.println("I am in finally block");
}

}

}

Event Handling Program in Java Using Frame

/*Design a Frame to calculate Electricity Bill for Consumers, where Current Month Reading(CMR) unit consumed has been provided with Last Month Reading(LMR) unit consumed.
So, calculate net unit consumed and Net Bill.
User Inputs : Consumer Number , Consumer Name, Address ,Current Month Reading(CMR) , Last Month Reading(LMR), Consumer Code .
What to find :-----
->Net Unit Consumed :(NUC)
->Rate per unit(RPU):
if Type Of Connections (TOC) :
Type Of Connections are as follows : - ******************************************************
# Agriculture - A
# Domestic - D
# Commercial - C
# Industrial - I
# Invalid - Default
_____________________________________________________________________________________________________*/
import java.awt.*;
import java.awt.event.*;

public class ElectricityBill extends Frame implements ActionListener
{
Label LblConsNo,LblConsName,LblConsAddress;
Label LblLMR,LblCMR,LblConsCode,LblConsNUC,LblRPU,LblNetBill,LblTOC;
TextField txtConsNo,txtConsName,txtConsAddress;
private TextField txtLMR, txtCMR,txtConsCode,txtConsNUC,txtRPU,txtNetBill,txtTOC;
Button BtnCalculate,BtnCancel;
public ElectricityBill()
{
super("-:Electricity Bill Calculation:-");
setLayout(new FlowLayout());
LblConsNo = new Label(" Enter Consumer Number: ");
LblConsName = new Label(" Enter Consumer Name: ");
LblConsAddress = new Label(" Enter Consumer Address: ");
LblLMR = new Label(" Enter Last Month Reading:");
LblCMR = new Label(" Enter Current Month Reading:");
LblConsCode = new Label(" Enter Consumer Code:[ A / D / C / I ]");
LblConsNUC = new Label(" Net Unit Consumed:");
LblRPU = new Label(" Rate Per Unit:");
LblNetBill = new Label(" Net Bill:");
LblTOC = new Label(" Type Of Connection:" );
txtConsNo = new TextField(20);
txtConsName = new TextField(20);
txtConsAddress = new TextField(20);
txtLMR = new TextField(20);
txtCMR = new TextField(20);
txtConsCode = new TextField(20);
txtConsNUC = new TextField(20);
txtRPU = new TextField(20);
txtNetBill = new TextField(20);
txtTOC = new TextField(20);
BtnCalculate = new Button("Calculate");
BtnCancel = new Button("Cancel");
add(LblConsNo);
add(txtConsNo);
add(LblConsName);
add(txtConsName);
add(LblConsAddress);
add(txtConsAddress);
add(LblLMR);
add(txtLMR);
add(LblCMR);
add(txtCMR);
add(LblConsCode);
add(txtConsCode);
add(LblConsNUC);
add(txtConsNUC);
add(LblRPU);
add(txtRPU);
add(LblNetBill);
add(txtNetBill);
add(LblTOC);
add(txtTOC);
add(BtnCalculate);
add(BtnCancel);
BtnCalculate.addActionListener(this);/*Registration of the Calculate Button with the event Listener Interface */
BtnCancel.addActionListener(this);
//addWindowListener(this);
setSize(400,500);
setVisible(true);
}
public static void main(String args[]) { ElectricityBill eb = new ElectricityBill(); }

public void actionPerformed(ActionEvent e)
{
int ConsNo;
String ConsName;
String ConsAddress;
int LMR,CMR;
char Code;
float RPU,NUC,NetBill;
String TOC;
if(e.getSource()==BtnCancel) { System.exit(0);
}
if(e.getSource()==BtnCalculate) { ConsNo = Integer.parseInt(txtConsNo.getText());
ConsName = txtConsName.getText();
ConsAddress = txtConsAddress.getText();
LMR = Integer.parseInt(txtLMR.getText());
CMR = Integer.parseInt(txtCMR.getText());
Code = txtConsCode.getText().charAt(0);
switch(Code) { case 'A':
RPU = 1.75f;
txtRPU.setText(String.valueOf(RPU));
NUC = CMR - LMR;
txtConsNUC.setText(String.valueOf(NUC));
NetBill = RPU*NUC;
txtNetBill.setText(String.valueOf(NetBill));
txtTOC.setText("Agriculture");
break; case 'D':
RPU = 3.75f;
txtRPU.setText(String.valueOf(RPU));
NUC = CMR - LMR;
txtConsNUC.setText(String.valueOf(NUC));
NetBill = RPU * NUC;
txtNetBill.setText(String.valueOf(NetBill));
txtTOC.setText("Domestic");
break;
case 'C': RPU = 5.90f;
txtRPU.setText(String.valueOf(RPU));
NUC = CMR - LMR;
txtConsNUC.setText(String.valueOf(NUC));
NetBill = RPU*NUC;
txtNetBill.setText(String.valueOf(NetBill));
txtTOC.setText("Commercial");
break; case 'I':
RPU = 10.75f;
txtRPU.setText(String.valueOf(RPU));
NUC = CMR - LMR;
txtConsNUC.setText(String.valueOf(NUC));
NetBill = RPU*NUC;
txtNetBill.setText(String.valueOf(NetBill));
txtTOC.setText("Inductrial");
break;
default:
RPU=0;
txtRPU.setText(String.valueOf(RPU));
NUC = CMR - LMR;
txtConsNUC.setText(String.valueOf(NUC));
NetBill= RPU * NUC;
txtNetBill.setText(String.valueOf(NetBill));
txtTOC.setText("Invalid Type Connection");
}
}
}
/*public void windowClosing(WindowEvent we) {
System.exit(0); }
public void windowActivated(WindowEvent we) {
}
public void windowClosed(WindowEvent we)
{
}
public void windowDeactivated(WindowEvent we)
{
}
public void windowDeiconified(WindowEvent we)
{
}
public void windowIconified(WindowEvent we)
{ }
public void windowOpened(WindowEvent we)
{ }*/

}