Professional Documents
Culture Documents
Sodapdf
Sodapdf
Write a java program to copy the data from one file to another file ,while copying changes the case of characters i
n target file and replaces all digits by '*' symbol.
import java.io.*;
class Slip2
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("a.txt");
FileWriter fw=new FileWriter("b.txt");
while((c=fr.read())!=-1)
{
if(c>=65&&c<=90)
{
c=c+32;
fw.write(c);
}
else if(c>=97&&c<=122)
{
c=c-32;
fw.write(c);
}
else if(c>=48&&c<=57)
{
fw.write('*');
}
else
{
fw.write(c);
}
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
import java.util.*;
class FileReverse
{
File f,f2;
String str;
FileReverse()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the source path: ");
String path=s.nextLine();
System.out.println("Enter the source name: ");
String name=s.nextLine();
f=new File(path,name);
}
void reverse()
{
try
{
RandomAccessFile file = new RandomAccessFile(f,"r");
long n= f.length()-1;
int i=0;
while (n>=0)
{
if (n==-1)
break;
else
{
file.seek(n);
i= file.read();
n=n-1;
System.out.print((char)i);
}
}
file.close();
}catch(Exception e)
{
System.out.print(e);
}
}
}
class TestFileReverse
{
public static void main(String args[])
{
FileReverse obj=new FileReverse();
obj.reverse();
}
}
3.Write a java program to validate PAN number and Mobile Number .If it is invalid then throw user defined Exce
ption "invalid Data", otherwise display it.
import java.io.*;
class Third {
static int n;
/**
* @param arg
*/
public static void main(String arg[]){
DataInputStream d = new DataInputStream(System.in);
try{
System.out.print("1.Mobile Number : \n2.PAN Card Number : \nChoose the Number : ");
n = Integer.parseInt(d.readLine());
switch(n)
{
case 1:
System.out.print("Enter Mobile No : ");
final Long number : Long.parseLong(d.readLine());
if(number.toString().matches("(0/91)?[7-9][0-9]{9}"))
{
System.out.print("Your Enterd Mobile Number : " +number+"\n");
else
{
throw new invalid_data();
}
break;
case 2:
System.out.print("Enter PAN Card Number : ");
String str = d.readLine();
if(str.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}"))
{
System.out.println("Your Entered PAN Card Number : " + str+"\n");
else
{
throw new invalid_data();
}
break;
default:
throw new invalid_data();
}
}catch(final invalid_data)
{
System.out.println("Invalid Data");
}
catch(NumberFormatException e)
{
System.out.println("Invalid Data");
}
catch(Exception e)
{}
}
}
}
}
4..Write a java program to accept the details of employee eno , ename, sal and display it on next frame using app
ropriate even.
import java.awt.*;
import java.awt.event.*;
class Fourth
{
Label l1,l2,l3,l;
TextField txt1,txt2,txt3;
Button submit,clear;
Panel p1;
Slip24()
{
l=new Label("EMPLOYEE INFORMTION");
l1=new Label("Name ");
l2=new Label("Address ");
l3=new Label("Salary ");
txt1=new TextField(20);
txt2=new TextField(20);
txt3=new TextField(20);
submit=new Button("submit");
submit.addActionListener(this);
clear=new Button("Clear");
clear.addActionListener(this);
p1=new Panel();
//p1.setLayout(new GridLayout(6,2));
p1.add(l1);
p1.add(txt1);
p1.add(l2);
p1.add(txt2);
p1.add(l3);
p1.add(txt3);
p1.add(submit);
p1.add(clear
add(p1);
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit)
{
new Employee_Detail(txt1.getText(),txt2.getText(),txt3.getText());
}
if(ae.getSource()==clear)
{
txt1.setText("");
txt2.setText("");
txt3.setText("");
}
}
public static void main(String args[])
{
new Slip24();
}
}
5.Write a java program to change the color of frame . if user clicks on close button then the position of frame should
get change.
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class changeBackground extends JFrame implements ActionListener {
JFrame frame;
JButton red, green, blue, pink, yellow, magenta, orange;
changeBackground() {
frame.add(red);
frame.add(green);
frame.add(blue);
frame.add(pink);
frame.add(yellow);
frame.add(magenta);
frame.add(orange);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
pink.addActionListener(this);
yellow.addActionListener(this);
magenta.addActionListener(this);
orange.addActionListener(this);
frame.getContentPane().setBackground(Color.black);
frame.setLayout(null);
frame.setSize(650, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
if(ae.getSource() == red)
frame.getContentPane().setBackground(Color.RED);
if(ae.getSource() == green)
frame.getContentPane().setBackground(Color.GREEN);
if(ae.getSource() == blue)
frame.getContentPane().setBackground(Color.BLUE);
if(ae.getSource() == pink)
frame.getContentPane().setBackground(Color.PINK);
if(ae.getSource() == yellow)
frame.getContentPane().setBackground(Color.YELLOW);
if(ae.getSource() == magenta)
frame.getContentPane().setBackground(Color.MAGENTA);
if(ae.getSource() == orange)
frame.getContentPane().setBackground(Color.ORANGE);
new changeBackground();