class Alphabets { public static void main(String args[]) { char ch; for( ch = 'a' ; ch <= 'z' ; ch++ ) System.out.println(ch); } }
Saturday, 29 June 2013
Print alphabets in java
First program in java
class First
{
public static void main(String[] arguments)
{
System.out.println("Welcome to Java technology.");
}
}
Friday, 28 June 2013
Changing the Cursor in Java awt
/**
* @(#)Text3.java
*
*
* @author
* @version 1.00 2013/6/28
*/
import java.awt.*;
import java.awt.event.*;
class Cursorch extends Frame implements ActionListener
{
Button btn_1,btn_2;
public Cursorch()
{
setLayout(new FlowLayout());
setTitle("Cursor");
setSize(500,500);
setLocation(100,100);
btn_1=new Button("Get Cursor 1");
btn_1.addActionListener(this);
add(btn_1);
btn_2=new Button("Get Cursor 1");
btn_2.addActionListener(this);
add(btn_2);
Cursor cur = btn_1.getCursor();
Cursor cur1 = btn_2.getCursor();
btn_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn_2.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
}
}
class ChangeCursor
{
public static void main(String []args)
{
new Cursorch().setVisible(true);
}
}
Image on Frame in Java AWT
/**
* @(#)DisplayImage.java
*
*
* @author - Anant Mahale
* @version 1.00 2013/6/28
*/
import java.awt.*;
import java.awt.event.*;
class image extends Frame
{
Image img;
public image()
{
setTitle("Image");
setSize(300,300);
setLocation(100,100);
MediaTracker mt=new MediaTracker(this);
img = Toolkit.getDefaultToolkit().getImage("post.jpg");
mt.addImage(img,0);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
if(img != null)
g.drawImage(img, 0, 0, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
}
class DisplayImage
{
public static void main(String []args)
{
new image().setVisible(true);
}
}
Create java .bat OR bat file
1. type java program in your java editor like notepad,textpad or jcreator ...
2. Save that java program with .java. Example "Example.java"
3. Open run then type "cmd" prem ok OR open command prompt set you directory
if you save the program in another drive then type like this ...
C:\Users\Swati>f:
here "C:\Users\Swati>" is defoult path and with "f:" it will change the driver ...
Now you will get your destination driver like
F:\>
As show in image ...
4. Then set the path of java bin folder as show in image ...
5. Then complie program like "javac first.java" ... as show in image
6. In the last run program like "java first" ... as show in image
7. Now you get the out put of your java program ...
8. Now for creating you java bat file, click on the left-top conrer of command prompt and then click on edit section and then click on the "select all"... it will display as show in image
9. Then again , click on the left-top conrer of command prompt and then click on edit section and then click on the "copy" ...
10. Now open "notepad" and paste that matter in note pad ... it will seen like this as show in image ...
11. Now Delete unwanted data and make new look of data as show in image ...
12. now type "pause" in last of data as shown in image ...
13. If you save your java file with any name like "Example.java" so take that name like "example" ... Here I'm save my file with "first.java" so I'll take name "first" and save file as "fist.bat" ... it means you have to save that notepad file like "Example.bat" ... as show in image
14. Your .bat file is ready ... double clicking on .bat file java program will compile and run ...
2. Save that java program with .java. Example "Example.java"
3. Open run then type "cmd" prem ok OR open command prompt set you directory
if you save the program in another drive then type like this ...
C:\Users\Swati>f:
here "C:\Users\Swati>" is defoult path and with "f:" it will change the driver ...
Now you will get your destination driver like
F:\>
As show in image ...
4. Then set the path of java bin folder as show in image ...
5. Then complie program like "javac first.java" ... as show in image
6. In the last run program like "java first" ... as show in image
7. Now you get the out put of your java program ...
8. Now for creating you java bat file, click on the left-top conrer of command prompt and then click on edit section and then click on the "select all"... it will display as show in image
9. Then again , click on the left-top conrer of command prompt and then click on edit section and then click on the "copy" ...
10. Now open "notepad" and paste that matter in note pad ... it will seen like this as show in image ...
11. Now Delete unwanted data and make new look of data as show in image ...
12. now type "pause" in last of data as shown in image ...
13. If you save your java file with any name like "Example.java" so take that name like "example" ... Here I'm save my file with "first.java" so I'll take name "first" and save file as "fist.bat" ... it means you have to save that notepad file like "Example.bat" ... as show in image
14. Your .bat file is ready ... double clicking on .bat file java program will compile and run ...
Sunday, 23 June 2013
How to handle String in switch case of JAVA
public class Test{
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public static void main(String args[]) {
String str="SUNDAY";
switch (Day.valueOf(str))
{
case MONDAY:
System.out.println("Day is Monday");
break;
case TUESDAY:
System.out.println("Day is Tuesday");
break;
case WEDNESDAY:
System.out.println("Day is Wednesday");
break;
case THURSDAY:
System.out.println("Day is Thursday");
break;
case FRIDAY:
System.out.println("Day is Friday");
break;
case SATURDAY:
System.out.println("Day is Saturday");
break;
case SUNDAY:
System.out.println("Day is Sunday");
break;
}
}
}
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public static void main(String args[]) {
String str="SUNDAY";
switch (Day.valueOf(str))
{
case MONDAY:
System.out.println("Day is Monday");
break;
case TUESDAY:
System.out.println("Day is Tuesday");
break;
case WEDNESDAY:
System.out.println("Day is Wednesday");
break;
case THURSDAY:
System.out.println("Day is Thursday");
break;
case FRIDAY:
System.out.println("Day is Friday");
break;
case SATURDAY:
System.out.println("Day is Saturday");
break;
case SUNDAY:
System.out.println("Day is Sunday");
break;
}
}
}
Saturday, 22 June 2013
Draw rectangle in java frame using Mouse Even
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mousedrag extends JFrame implements MouseListener,MouseMotionListener
{
int x,y,x1,y1;
mousedrag()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseMoved(MouseEvent me)
{
}
public void mouseDragged(MouseEvent me)
{
x1=me.getX();
y1=me.getY();
repaint();
}
public void mouseExited(MouseEvent me)
{
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(x,y,Math.abs(x-x1),Math.abs(y-y1));
}
public static void main(String args[])
{
mousedrag f=new mousedrag();
f.setTitle("demo of mouse event");
f.setSize(400,400);
f.setVisible(true);
}
}
demonstration of mouse event
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mousemove extends JFrame implements MouseListener,MouseMotionListener
{
int x,y,X,Y;
mousemove()
{
addMouseListener(this);
addMouseMotionListener(this);
x=0;
y=0;
}
public void mouseEntered(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
repaint();
}
public void mouseDragged(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString("x="+x,50,50);
g.drawString("Y="+y,90,50);
}
public static void main(String args[])
{ mousemove f=new mousemove();
f.setTitle("demo of mouse event");
f.setSize(400,400);
f.setVisible(true);
}
}
Java swing program for multicasting
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class multicast extends JFrame implements ActionListener
{
JButton b,b1;
int c=2,x=50;
multicast()
{
setLayout(new FlowLayout());
b=new JButton("click one time o/p multicast");
b.addActionListener(this);
add(b);
b1=new JButton("cancel");
b1.addActionListener(this);
add(b1);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str=="click one time o/p multicast")
{
myframe f=new myframe();
f.setSize(300,300);
f.setLocation(20*c,20*c);
f.setVisible(true);
c=c+2;
}
if(str=="cancel")
{
System.exit(0);
}
}
public static void main(String args[])
{
multicast m=new multicast();
m.setSize(400,400);
m.setVisible(true);
}
}
write a java program to demo for Paint Mode (XOR Mode) in Swing Programming
//wap to demo for Paint Mode in Swing Programming
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mypanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.red);
g.fillOval(100,50,50,50);
g.setXORMode(Color.pink);
g.setColor(Color.green);
g.fillOval(100,80,60,60);
}
}
class myframe3 extends JFrame
{
Container c;
myframe3()
{
mypanel p=new mypanel();
c=getContentPane();
c.add(p);
}
public static void main(String args[])
{
myframe3 f=new myframe3();
f.setTitle("paint");
f.setSize(600,600);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
Draw Various shapes with radio button in Java swing
//WAP to check JRadioButton in panel
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mypanel extends JPanel implements ActionListener
{
JRadioButton red,blue,green;
ButtonGroup color;
int a;
mypanel()
{
setLayout(null);
red=new JRadioButton("Rect");
red.addActionListener(this);
red.setBounds(50,50,100,30);
add(red);
blue=new JRadioButton("RoundRect");
blue.addActionListener(this);
blue.setBounds(50,100,100,30);
add(blue);
green=new JRadioButton("Circle");
green.addActionListener(this);
green.setBounds(50,150,100,30);
add(green);
color=new ButtonGroup();
color.add(red);
color.add(blue);
color.add(green);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str=="Rect")
a=1;
if(str=="RoundRect")
a=2;
if(str=="Circle")
a=3;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(a==1)
{
g.drawRect(300,200,50,50);
}
if(a==2)
{
g.drawRoundRect(100,250,50,50,20,20);
}
if(a==3)
{
g.drawOval(200,100,50,100);
}
}
}
class myframe extends JFrame
{
Container c;
myframe()
{
c=getContentPane();
mypanel p=new mypanel();
c.add(p);
}
}
class rbpanel
{
public static void main(String args[])
{
myframe f=new myframe();
f.setTitle("RadioButton panel");
f.setSize(500,500);
f.setVisible(true);
}
}
Friday, 21 June 2013
Creat Menu bar in Java Swing and draw various shapes
// TO create Menu Bar
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class menu extends JFrame implements ActionListener
{
JMenuBar mb;
JMenu draw;
JMenuItem rect,line,oval;
//Container c;
menu()
{
//c=getContentPane();
setLayout(null);
mb=new JMenuBar();
draw=new JMenu("draw");
draw.setMnemonic('d');
mb.add(draw);
rect=new JMenuItem("rect");
rect.addActionListener(this);
rect.setMnemonic('r');
draw.add(rect);
line=new JMenuItem("line");
line.addActionListener(this);
line.setMnemonic('l');
draw.add(line);
oval=new JMenuItem("oval");
oval.addActionListener(this);
oval.setMnemonic('o');
draw.add(oval);
setJMenuBar(mb);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
Graphics g=getGraphics();
if(str=="rect")
g.drawRect(100,100,50,50);
if(str=="line")
g.drawLine(300,50,400,350);
if(str=="oval")
g.drawOval(400,50,50,50);
}
public static void main(String args[])
{
menu f=new menu();
f.setTitle("my frame");
f.setSize(500,500);
f.setVisible(true);
}
}
Monday, 17 June 2013
Program for key event to draw various shapes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class keydraw extends JFrame implements KeyListener
{
Label lbl_title;
int ch;
int a;
keydraw()
{
a=0;
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
ch=ke.getKeyCode();
}
public void keyTyped(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
if(ch==ke.VK_LEFT)
a=1;
if(ch==ke.VK_RIGHT)
a=2;
if(ch==ke.VK_UP)
a=3;
if(ch==ke.VK_DOWN)
a=4;
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
if(a==1)
g.drawLine(200,100,300,200);
if(a==2)
g.drawOval(200,100,200,100);
if(a==3)
g.drawRoundRect(200,100,50,50,20,20);
if(a==4)
g.drawRect(300,200,50,50);
}
public static void main(String arge[])
{
keydraw f=new keydraw();
f.setTitle("Press direction keys");
f.setVisible(true);
f.setSize(400,400);
}
}
draw various shape in java swing with JComboBox
//WAP to check combobox in panel
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mypanel extends JPanel implements ItemListener
{
JComboBox cb;
int a=0;
mypanel()
{
setLayout(null);
cb=new JComboBox();
cb.addItem("Rect");
cb.addItem("RoundRect");
cb.addItem("Circle");
cb.setBounds(50,50,100,30);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie)
{
String str=(String)cb.getSelectedItem();
if(str=="Rect")
a=1;
if(str=="RoundRect")
a=2;
if(str=="Circle")
a=3;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(a==1)
{
g.drawRect(300,200,50,50);
}
if(a==2)
{
g.drawRoundRect(100,250,50,50,20,20);
}
if(a==3)
{
g.drawOval(120,100,50,10);
}
}
}
class myframe extends JFrame
{
Container c;
myframe()
{
c=getContentPane();
mypanel p=new mypanel();
c.add(p);
}
}
class cpanel
{
public static void main(String args[])
{
myframe f=new myframe();
f.setTitle("combobox panel");
f.setSize(500,500);
f.setVisible(true);
}
}
Program in java for JComboBox in swing
//WAP to check the combobox
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class combo extends JFrame implements ItemListener
{
JComboBox cb;
Container c;
combo()
{
c=getContentPane();
c.setLayout(null);
cb=new JComboBox();
cb.addItem("Red");
cb.addItem("Blue");
cb.addItem("Pink");
cb.setBounds(50,50,100,30);
cb.addItemListener(this);
c.add(cb);
}
public void itemStateChanged(ItemEvent ie)
{
String str=(String)cb.getSelectedItem();
if(str=="Red")
{
c.setBackground(Color.red);
}
if(str=="Blue")
{
c.setBackground(Color.blue);
}
if(str=="Pink")
{
c.setBackground(Color.pink);
}
}
public static void main(String args[])
{
combo f=new combo();
f.setTitle("demo");
f.setSize(500,500);
f.setVisible(true);
}
}
Java program for typing on frame
//WAP to handle Key event
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class key1 extends JFrame implements KeyListener
{
String msg=" ";
char ch;
key1()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
ch=ke.getKeyChar();
msg=msg+ch;
}
public void keyTyped(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
Graphics g=getGraphics();
g.drawString(msg,100,100);
}
public static void main(String args[])
{
key1 k=new key1();
k.setTitle("Key event");
k.setSize(400,400);
k.setVisible(true);
}
}
This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.
import java.text.*; import java.util.*; public class Main { public static void main(String[] args) { Date dt = new Date(1000000000000L); DateFormat[] dtformat = new DateFormat[6]; dtformat[0] = DateFormat.getInstance(); dtformat[1] = DateFormat.getDateInstance(); dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM); dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL); dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG); dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT); for(DateFormat dateform : dtformat) System.out.println(dateform.format(dt)); } }
This example formats the time into 24 hour format (00:00-24:00) by using sdf.format(date) method of SimpleDateFormat class.
import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("h"); System.out.println("hour in h format : " + sdf.format(date)); } }
Happy Father's Day
/**
* @(#)HappyFathersDay.java
*
*
* @author -> Anant Mahale
* @version 1.00 2013/6/16
*/
import java.awt.*;
import java.awt.event.*;
class ButtonFrame extends Frame implements ActionListener
{
private Button btn_press;
public ButtonFrame()
{
setLayout(new FlowLayout());
setTitle("Wish");
setSize(500,500);
setLocation(200,200);
btn_press=new Button("Wish Your Father");
btn_press.setForeground(Color.orange);
btn_press.addActionListener(this);
add(btn_press);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_press)
{
new dailog().setVisible(true);
}
}
}
class dailog extends Dialog implements ActionListener
{
private Button btn_yes;
private Panel north_panel,south_panel;
public dailog()
{
super(new ButtonFrame(),"Dialog Demo",true);
setBackground(Color.darkGray);
south_panel=new Panel();
setTitle("Father's Day Wishes");
setSize(700,250);
setLocation(50,80);
btn_yes=new Button("Exit");
south_panel.add(btn_yes);
add(south_panel,"South");
btn_yes.addActionListener(this);
}
public void paint(Graphics g)
{
g.setFont(new Font("MV Boli",Font.BOLD,30));
g.setColor(Color.RED);
g.drawString("Your guiding hand on my shoulder,",75,80);
g.drawString("Will remain with me forever.,",75,110);
g.drawString("Thanks for always being there Papa.",75,140);
g.drawString("Happy Father’s Day!",75,170);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_yes)
{
System.exit(0);
}
}
}
class HappyFathersDay
{
public static void main(String args[])
{
new ButtonFrame().setVisible(true);
}
}
Sunday, 16 June 2013
This example shows how to display the current date and time using Calender.getInstance() method of Calender class and fmt.format() method of Formatter class.
import java.util.Calendar; import java.util.Formatter; public class MainClass{ public static void main(String args[]){ Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tc", cal); System.out.println(fmt); } }
This example formats the time by using SimpleDateFormat("HH-mm-ss a") constructor and sdf.format(date) method of SimpleDateFormat class.
import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main(String[] args){ Date date = new Date(); String strDateFormat = "HH:mm:ss a"; SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); System.out.println(sdf.format(date)); } }
The following program demonstrates BufferedReader and the readLine( ) method. The program reads and displays lines of text until you enter the word "end":
// Read a string from console using a BufferedReader. import java.io.*; public class BRReadLines { public static void main(String args[]) throws IOException { // Create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("Enter lines of text."); System.out.println("Enter 'end' to quit."); do { str = br.readLine(); System.out.println(str); } while(!str.equals("end")); } }
The following program demonstrates read( ) by reading characters from the console until the user types a "q":
// Use a BufferedReader to read characters from the console. import java.io.*; public class BRRead { public static void main(String args[]) throws IOException { char c; // Create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter characters, 'q' to quit."); // read characters do { c = (char) br.read(); System.out.println(c); } while(c != 'q'); } }
Stack program in java
class StackException extends Exception
{
private String msg;
public StackException(String s)
{
msg=s;
}
public String getErrorMessage()
{
return msg;
}
}
class MyStack
{
private int stack[];
public MyStack(int s)
{
top=0;
stack=new int[s];
}
void push(int no)
{
try
{
if(top>2)
throw new StackException("Stack is full")
stack[top]=no;
top++;
}
catch(StackException e)
{
for(int i=top-1;i>=0;i--)
System.out.println(" "+stack[i);
}
}
}
class MyStackDemo
{
public static void main(String []args)
{
MyStack m1=new MyStack(3);
m1.push(111);
m1.push(222);
m1.push(333);
m1.push(444);
}
}
{
private String msg;
public StackException(String s)
{
msg=s;
}
public String getErrorMessage()
{
return msg;
}
}
class MyStack
{
private int stack[];
public MyStack(int s)
{
top=0;
stack=new int[s];
}
void push(int no)
{
try
{
if(top>2)
throw new StackException("Stack is full")
stack[top]=no;
top++;
}
catch(StackException e)
{
for(int i=top-1;i>=0;i--)
System.out.println(" "+stack[i);
}
}
}
class MyStackDemo
{
public static void main(String []args)
{
MyStack m1=new MyStack(3);
m1.push(111);
m1.push(222);
m1.push(333);
m1.push(444);
}
}
Abstract class program in java
abstract class Employee
{
private int eno;
private String name;
private float bs;
private int hra,da,ta;
private float HRA,DA,TA,gs;
public Employee()
{
eno=0;
name="Empty";
bs=0.0f;
hra=ta=da=0;
}
public Employee(int eno,String name,float bs,int hra, int da,int ta)
{
this.eno=eno;
this.name=name;
this.bs=bs;
this.hra=hra;
this.da=da;
this.ta=ta;
}
public float getSal()
{
HRA=(bs*hra)/100.0f;
DA=(bs*da)/100.0f;
TA=(bs*ta)/100.0f;
gs=HRA+DA+TA+bs;
return gs;
}
abstract public void calsal();
}
class WageEmp extends Employee
{
private int hours,rate;
public WageEmp()
{
hours=0;
rate=0;
}
public WageEmp(int eno,String name,float bs, int hra,int da,int ta,int hours,int rate)
{
super(eno,name,bs,hra,da,ta);
this.hours=hours;
this.rate=rate;
}
public void calsal()
{
System.out.println("salary -"+getClass()+(hours*rate));
}
}
class Manager extends Employee
{
private int inc;
public Manager()
{
inc=0;
}
public Manager(int eno, String name,float bs, int hra,int da,int ta,int inc)
{
super(eno,name,bs,hra,da,ta);
this.inc=inc;
}
public void calsal()
{
System.out.println("\n Salary - "+getSal()+inc);
}
}
class EmpDemo
{
public static void main(String args[])
{
WageEmp we=new WageEmp(1,"Amit",2000.0f,9,8,8,10,5);
Manager mgr=new Manager(111,"Ajay",10000.0f,11,11,13,5550);
Employee []arr=new Employee[2];
arr[0]=we;
arr[1]=mgr;
for(int i=0;i<arr.length;i++)
arr[i].calsal();
}
}
{
private int eno;
private String name;
private float bs;
private int hra,da,ta;
private float HRA,DA,TA,gs;
public Employee()
{
eno=0;
name="Empty";
bs=0.0f;
hra=ta=da=0;
}
public Employee(int eno,String name,float bs,int hra, int da,int ta)
{
this.eno=eno;
this.name=name;
this.bs=bs;
this.hra=hra;
this.da=da;
this.ta=ta;
}
public float getSal()
{
HRA=(bs*hra)/100.0f;
DA=(bs*da)/100.0f;
TA=(bs*ta)/100.0f;
gs=HRA+DA+TA+bs;
return gs;
}
abstract public void calsal();
}
class WageEmp extends Employee
{
private int hours,rate;
public WageEmp()
{
hours=0;
rate=0;
}
public WageEmp(int eno,String name,float bs, int hra,int da,int ta,int hours,int rate)
{
super(eno,name,bs,hra,da,ta);
this.hours=hours;
this.rate=rate;
}
public void calsal()
{
System.out.println("salary -"+getClass()+(hours*rate));
}
}
class Manager extends Employee
{
private int inc;
public Manager()
{
inc=0;
}
public Manager(int eno, String name,float bs, int hra,int da,int ta,int inc)
{
super(eno,name,bs,hra,da,ta);
this.inc=inc;
}
public void calsal()
{
System.out.println("\n Salary - "+getSal()+inc);
}
}
class EmpDemo
{
public static void main(String args[])
{
WageEmp we=new WageEmp(1,"Amit",2000.0f,9,8,8,10,5);
Manager mgr=new Manager(111,"Ajay",10000.0f,11,11,13,5550);
Employee []arr=new Employee[2];
arr[0]=we;
arr[1]=mgr;
for(int i=0;i<arr.length;i++)
arr[i].calsal();
}
}
Program in java for count the objects
// program for count number of objects
class count
{
private static int cnt;
public count()
{
cnt++;
}
public static void show()
{
System.out.println("Objects are :="+cnt);
}
}
class CountObj
{
public static void main(String []args)
{
count c1=new count();
count c2=new count();
count c3=new count();
count.show();
}
}
class count
{
private static int cnt;
public count()
{
cnt++;
}
public static void show()
{
System.out.println("Objects are :="+cnt);
}
}
class CountObj
{
public static void main(String []args)
{
count c1=new count();
count c2=new count();
count c3=new count();
count.show();
}
}
Program for java package
package mypack;
class Employee
{
private int eno;
private String name;
private String address;
public Employee(int eno, String name,String address)
{
this.eno=eno;
this.name=name;
this.address=address;
}
public String toString()
{
return eno+" "+name+" "+address;
}
}
class Emp
{
public static void main(String []args)
{
Employee e1=new Employee(111,"Amit","Pune");
System.out.println("Emp is -> "+e1);
}
}
class Employee
{
private int eno;
private String name;
private String address;
public Employee(int eno, String name,String address)
{
this.eno=eno;
this.name=name;
this.address=address;
}
public String toString()
{
return eno+" "+name+" "+address;
}
}
class Emp
{
public static void main(String []args)
{
Employee e1=new Employee(111,"Amit","Pune");
System.out.println("Emp is -> "+e1);
}
}
Scrollbar demo in java awt
/**
* @(#)ScrollDemo.java
*
*
* @anant mahale
* @version 1.00 2013/3/18
*/
import java.awt.*;
import java.awt.event.*;
class MyScroll extends Frame implements AdjustmentListener
{
private Scrollbar scr_red,scr_green,scr_blue;
private int r,g,b;
public MyScroll()
{
setTitle("Scrollbar");
setSize(500,500);
setLocation(100,100);
scr_red=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_green=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_blue=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_red.addAdjustmentListener(this);
scr_green.addAdjustmentListener(this);
scr_blue.addAdjustmentListener(this);
setLayout(null);
scr_red.setBounds(10,50,200,20);
scr_green.setBounds(10,80,200,20);
scr_blue.setBounds(10,110,200,20);
add(scr_red);
add(scr_green);
add(scr_blue);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
if(ae.getSource()==scr_red)
{
r=scr_red.getValue();
}
else if(ae.getSource()==scr_green)
{
g=scr_green.getValue();
}
else if(ae.getSource()==scr_blue)
{
b=scr_blue.getValue();
}
setBackground(new Color(r,g,b));
}
}
class ScrollDemo
{
public static void main(String []args)
{
new MyScroll().setVisible(true);
}
}
Frame Demo in Java
import java.awt.*;
class SecondFrame
{
public static void main(String []args)
{
Frame f1=new Frame("This is my second frame");
f1.setVisible(true);
f1.setLocation(100,100);
f1.setResizable(false);
}
}
class SecondFrame
{
public static void main(String []args)
{
Frame f1=new Frame("This is my second frame");
f1.setVisible(true);
f1.setLocation(100,100);
f1.setResizable(false);
}
}
Inheritance program in java
class Employee
{
private int eno;
private String name;
private float bs;
private int hra,da,ta;
public Employee()
{
eno=0;
name="Empty";
bs=0;
}
public Employee(int eno,String name,float bs,int hra,int da,int ta)
{
this.eno=eno;
this.name=name;
this.bs=bs;
}
public String toString()
{
return "\n Eno -"+eno+"\n Name -"+name+"\n Salary -"+bs;
}
}
class WageEmp extends Employee
{
private int hours,rate;
public WageEmp()
{
hours=0;
rate=0;
}
public WageEmp(int eno,String name,float bs, int hra,int da,int ta, int hours, int rate)
{
super(eno,name,bs,hra,da,ta);
this.hours=hours;
this.rate=rate;
}
public String toString()
{
return super.toString()+"\n Hours -"+hours+"\n Rate -"+rate;
}
}
class Manager extends Employee
{
private int inc;
public Manager()
{
inc=0;
}
public Manager(int eno,String name,float bs,int hra,int da,int ta,int inc)
{
super(eno,name,bs,hra,da,ta);
this.inc=inc;
}
public String toString()
{
return super.toString()+"\n Incentave-"+inc;
}
}
class inher
{
public static void main(String args[])
{
WageEmp we=new WageEmp(1,"Amit",2000.0f,9,8,8,10,5);
Manager mgr=new Manager(111,"Ajay",1000.0f,10,11,13,5550);
System.out.println("WageEmp \n"+we);
System.out.println("=======================================");
System.out.println("Manager \n"+mgr);
}
}
{
private int eno;
private String name;
private float bs;
private int hra,da,ta;
public Employee()
{
eno=0;
name="Empty";
bs=0;
}
public Employee(int eno,String name,float bs,int hra,int da,int ta)
{
this.eno=eno;
this.name=name;
this.bs=bs;
}
public String toString()
{
return "\n Eno -"+eno+"\n Name -"+name+"\n Salary -"+bs;
}
}
class WageEmp extends Employee
{
private int hours,rate;
public WageEmp()
{
hours=0;
rate=0;
}
public WageEmp(int eno,String name,float bs, int hra,int da,int ta, int hours, int rate)
{
super(eno,name,bs,hra,da,ta);
this.hours=hours;
this.rate=rate;
}
public String toString()
{
return super.toString()+"\n Hours -"+hours+"\n Rate -"+rate;
}
}
class Manager extends Employee
{
private int inc;
public Manager()
{
inc=0;
}
public Manager(int eno,String name,float bs,int hra,int da,int ta,int inc)
{
super(eno,name,bs,hra,da,ta);
this.inc=inc;
}
public String toString()
{
return super.toString()+"\n Incentave-"+inc;
}
}
class inher
{
public static void main(String args[])
{
WageEmp we=new WageEmp(1,"Amit",2000.0f,9,8,8,10,5);
Manager mgr=new Manager(111,"Ajay",1000.0f,10,11,13,5550);
System.out.println("WageEmp \n"+we);
System.out.println("=======================================");
System.out.println("Manager \n"+mgr);
}
}
polymorphism in java
/* File name : Employee.java */ public class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } }
invoking a superclass version of an overridden method in java
class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); // Animal reference but Dog object b.move(); //Runs the method in Dog class } }
Extending Interfaces in java
/*Extending Interfaces*/
//Filename: Sports.java
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
//Filename: Football.java
public interface Football extends Sports
{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
//Filename: Hockey.java
public interface Hockey extends Sports
{
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
implementing interfaces in java
/*Implementing Interfaces*/
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
java program for addition, subtraction, multiplication and division
import java.io.*;
class allprograms
{
public static void main(String args[])
{
try
{
int a,b,c,d,e;
DataInputStream cin=new DataInputStream(System.in);
System.out.println("Enter the first number");
a=Integer.parseInt(cin.readLine());
System.out.println("Enter the Second number");
b=Integer.parseInt(cin.readLine());
c=a+b;
d=a-b;
e=a*b;
float f=b/a;
System.out.println(a+" + "+b+" = "+c);
System.out.println(a+" - "+b+" = "+d);
System.out.println(a+" * "+b+" = "+e);
System.out.println(b+" / "+a+" = "+f);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class allprograms
{
public static void main(String args[])
{
try
{
int a,b,c,d,e;
DataInputStream cin=new DataInputStream(System.in);
System.out.println("Enter the first number");
a=Integer.parseInt(cin.readLine());
System.out.println("Enter the Second number");
b=Integer.parseInt(cin.readLine());
c=a+b;
d=a-b;
e=a*b;
float f=b/a;
System.out.println(a+" + "+b+" = "+c);
System.out.println(a+" - "+b+" = "+d);
System.out.println(a+" * "+b+" = "+e);
System.out.println(b+" / "+a+" = "+f);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Saturday, 15 June 2013
Demonstration of switch statement in java
public class Test { public static void main(String args[]){ char grade = args[0].charAt(0); switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }
Demonstration of if -else statement
public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } } }
Demonstration of Nested if in Java
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } }
Java program for if-else if OR if-else if program in java
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else statement"); } } }
program for if statement OR use of if statement in java
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); } } }
Demonstration for continue keyword
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } }
Subscribe to:
Posts (Atom)