Wednesday, January 20, 2010

File Copy program in Java

//FileCopy

import java.io.*;

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

File file1 = new File("f1.txt");
File file2 = new File("f2.txt");;
File file3 = new File("f3.txt");;
FileOutputStream fos = new FileOutputStream(file1);
FileReader fr;

int cnt;
int cap = 65;
int small = 98;
char ch;

//writing in file1
fos.write(cap);cap++;
fos.write(cap);cap++;
fos.write(small);small++;
fos.write(small);small++;
fos.write(small);small++;
fos.write(cap);cap++;

fr = new FileReader(file1);

fos = new FileOutputStream(file2);

while( (cnt = fr.read()) != -1 )
{
ch = (char)cnt;
if( ch >= 'a' && ch <= 'z' )
{
cnt = cnt - 32;
fos.write(cnt);
}
else
if( ch >= 'A' && ch <= 'Z' )
{
cnt = cnt + 32;
fos.write((char)cnt);
}
else
fos.write(cnt);
}
//displaying file1 file2
fr = new FileReader(file1);
System.out.println ("file1::");
while( (cnt = fr.read()) != -1 )
{
ch = (char)cnt;
System.out.print (""+ch);
}
fr = new FileReader(file2);
System.out.println ("");
System.out.println ("file2::");
while( (cnt = fr.read() )!= -1)
{
ch = (char)cnt;
System.out.print (""+ch);
}

fos = new FileOutputStream(file3);
fr = new FileReader(file1);
while( (cnt = fr.read()) != -1 )
{
fos.write(cnt);
}

fr = new FileReader(file2);
while( (cnt = fr.read()) != -1 )
{
fos.write(cnt);
}

fr = new FileReader(file3);
System.out.println ("");
System.out.println ("file3::");
while( (cnt = fr.read() )!= -1)
{
ch = (char)cnt;
System.out.print (""+ch);
}

fos.close();
fr.close();
//file3.
}
catch(Exception exc)
{
System.out.println ("exc::"+exc);
}

}
}

Number Conversion program in Java

//NumberConversion

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NumberConversion extends JFrame
{
Container con = this.getContentPane();

JLabel lbl;
JButton binary;
JButton octal;
JButton hexa;

JTextArea txt1;
JTextArea txt2;
JTextArea txt3;
JTextArea txt4;

public NumberConversion()
{
con.setLayout(null);

lbl = new JLabel("Enter Decimal ");
lbl.setBounds(25,75,200,25);
con.add(lbl);

txt1 = new JTextArea(1,15);
txt1.setBounds(175,75,150,25);
con.add(txt1);

txt2 = new JTextArea(1,15);
txt2.setBounds(175,125,150,25);
con.add(txt2);

txt3 = new JTextArea(1,15);
txt3.setBounds(175,175,150,25);
con.add(txt3);

txt4 = new JTextArea(1,15);
txt4.setBounds(175,225,150,25);
con.add(txt4);

binary = new JButton("Binary");
binary.setBounds(25,125,100,25);
con.add(binary);

octal = new JButton("Octal");
octal.setBounds(25,175,100,25);
con.add(octal);

hexa = new JButton("Hexadecimal");
hexa.setBounds(25,225,100,25);
con.add(hexa);

binary.addActionListener(new w11());
octal.addActionListener(new w11());
hexa.addActionListener(new w11());

}

class w11 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
JButton temp = (JButton)ae.getSource();
String str;
Integer b[] = new Integer[10] ;
int i = 0;
int num;
if( temp.getLabel().equals("Binary"))
{
str = txt1.getText();
try
{
num = Integer.parseInt(str);
while( num != 0 )
{
int rem = num % 2;
b[i] = rem;
i++;
num = num/2;
}
txt2.setText("");
for(i = i -1 ;i >= 0; i--)
txt2.append(""+b[i]);

}
catch(Exception exc)
{
JOptionPane.showMessageDialog(con,"Enter Number\nCharacters invalid","ERROR",JOptionPane.ERROR_MESSAGE);
System.out.println ("Exception genenrated::"+exc);
}
}
else
if( temp.getLabel().equals("Octal"))
{
str = txt1.getText();
try
{
num = Integer.parseInt(str);
while( num != 0 )
{
int rem = num % 8;
b[i] = rem;
i++;
num = num / 8;
}
txt3.setText("");
for(i = i -1 ;i >= 0; i--)
txt3.append(""+b[i]);

}
catch(Exception exc)
{
JOptionPane.showMessageDialog(con,"Enter Number\nCharacters invalid","ERROR",JOptionPane.ERROR_MESSAGE);
System.out.println ("Exception genenrated::"+exc);
}
}
else
if( temp.getLabel().equals("Hexadecimal"))
{
str = txt1.getText();
try
{
num = Integer.parseInt(str);
while( num != 0 )
{
int rem = num % 16;
b[i] = rem;
i++;
num = num / 16;
}
txt4.setText("");
for(i = i -1 ;i >= 0; i--)
{

switch( b[i] )
{
case 10:txt4.append("A"); break;
case 11:txt4.append("B"); break;
case 12:txt4.append("C"); break;
case 13:txt4.append("D"); break;
case 14:txt4.append("E"); break;
case 15:txt4.append("F"); break;
default:txt4.append(""+b[i]); break;
}

}

}
catch(Exception exc)
{
JOptionPane.showMessageDialog(con,"Enter Number\nCharacters invalid","ERROR",JOptionPane.ERROR_MESSAGE);
System.out.println ("Exception genenrated::"+exc);
}
}

}
}

public static void main(String args[])
{
NumberConversion obj = new NumberConversion();
obj.setVisible(true);
obj.setTitle("Number conversion");
obj.setExtendedState(MAXIMIZED_BOTH);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

Binary Calculator Program in Java

//BinaryCalculator


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BinaryCalculator extends JFrame
{
Container con;

int flag;
int num1 , num2;
int num3,num4;
char array1[];
int array[];

JTextArea txtop;
JButton butadd;
JButton butequ;
JButton butcomplement;
JButton butsub;
public BinaryCalculator()
{
con = this.getContentPane();
con.setLayout(null);

txtop = new JTextArea(1,15);
txtop.setBounds(50,50,300,30);
con.add(txtop);

butadd = new JButton("+");
butadd.setBounds(50,120,50,30);
con.add(butadd);

butsub = new JButton("-");
butsub.setBounds(125,120,50,30);
con.add(butsub);

butcomplement = new JButton("~");
butcomplement.setBounds(200,120,50,30);
con.add(butcomplement);

butequ = new JButton("=");
butequ.setBounds(275,120,50,30);
con.add(butequ);

butadd.addActionListener(new w11());
butsub.addActionListener(new w11());
butcomplement.addActionListener(new w11());
butequ.addActionListener(new w11());
}

class w11 implements ActionListener
{
String number;
int res;
public void actionPerformed(ActionEvent ae)
{
JButton temp = (JButton)ae.getSource();
if( temp.getLabel().equals("+") )
{
number = txtop.getText();
if( (res=chkbinary(number))==1 )
{
flag = 1;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}


txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}
else
if( temp.getLabel().equals("~") )
{
number = txtop.getText();
if( number.length() < 1 )
JOptionPane.showMessageDialog(con,"enter number");
else
{
flag = 2;
}
}

else
if( temp.getLabel().equals("-") )
{
number = txtop.getText();
if( (res = chkbinary(number) )==1 )
{
flag = 3;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}

txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}

else

if( temp.getLabel().equals("=") )
{
switch(flag)
{
case 1: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1+num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 3: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1-num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 2:
number = txtop.getText();
txtop.setText(null);
if( ( res = chkbinary(number) )==1 )
{



for( int i = 0 ; i < number.length() ; i++ )
{
if( number.charAt(i) == '1')

array1[i] = '0';
else

array1[i] = '1';

txtop.append(Character.toString((char)array1[i]));
}

}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;


default:System.out.println ("in default");break;
}
}
}
public int getDecimal(String t)
{
int r, j=0;
int ans = 0;
array1 = t.toCharArray();
for( int i = (t.length()- 1); i >= 0 ; i--)
{

r = Integer.parseInt(""+t.charAt(i));
ans += r*(double)Math.pow((double)2,(double)j);
j++;
}
return ans;
}
public int chkbinary(String t)
{
array1 = t.toCharArray();

int i;
for( i = 0 ;i < t.length();i++)
{
if( array1[i] == '1' || array1[i] == '0')
{

}
else
break;
}

if( i == t.length())
return 1;
else return 0;
}
}

public static void main(String arg[])
{
BinaryCalculator obj = new BinaryCalculator();
obj.setVisible(true);
obj.setTitle("Binary Calculator");
obj.setExtendedState(MAXIMIZED_BOTH);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

Age Name Exception program in Java

class AgeNameException extends Exception
{
private String name ;
private int age ;
public AgeNameException(String str,int num)
{
name = str;
age = num;
}
public String toString()
{
String msg = "";
int i;
char ch;
for(i = 0;i < name.length() ; i++)
{
ch = name.charAt(i);
if( ch >= 'A' && ch <= 'Z' )
{
}
else
if( ch >= 'a' && ch <= 'z' )
{
}
else break;
}
if( i == name.length())
msg = "";
else
{
msg = "Invalid Name";
}
if( age <>22 )
msg = msg+" Age is not within range";
return msg;

}
}

public class Student
{
String sname;
int sage;
public Student(String name, int age)
{
sname = name;
sage = age;
}
public static void main(String args[])
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int age;
String name;


System.out.println ("Enter name:");
name = br.readLine();

System.out.println ("Enter age :");
age = Integer.parseInt(br.readLine());

//Student obj = new Student(name,age);
throw new AgeNameException(name,age);
}
catch(AgeNameException ane)
{
System.out.println (""+ane);
}
catch(Exception exc)
{
System.out.println (""+exc);
}

}
protected void finalize()
{
}

}

Friday, December 11, 2009

program to show different types of constructor in c++



#include"iostream.h"
#include"conio.h"
class disp
{
int x,y;

public:
disp();//default constructor
disp(int a,int b) //parameterized constructor
{
x=a;
y=b;
cout<<"Parameterized const values are->"< };
disp(disp &c, disp &g)//copy constructor
{
x=c.x;
y=g.y;
cout<<"\n"<<"copy const values are->"<<" "<
};

};
void main()
{
clrscr();
disp d1(void);
disp d2(6,7);
disp(d2,d2);
}

Your Ad Here
 
Programs Copyright © 2009 Blogger Template Designed by Bie Blogger Template