Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

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);
}
}
}

2.Write a java program to display contents of a file in reverse order.

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 invalid_data extends Exception {


}

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() {

Font font = new Font("Lucida Calligraphy", Font.BOLD, 9);


frame = new JFrame("Colourful window");
Color c = new Color(255, 255, 255);

red = new JButton("RED");


red.setBounds(40, 100, 100, 40);
red.setFont(font);
red.setBackground(c);

green = new JButton("GREEN");


green.setBounds(110, 150, 100, 40);
green.setFont(font);
green.setBackground(c);

blue = new JButton("BLUE");


blue.setBounds(180, 200, 100, 40);
blue.setFont(font);
blue.setBackground(c);

pink = new JButton("PINK");


pink.setBounds(250, 250, 100, 40);
pink.setFont(font);
pink.setBackground(c);

yellow = new JButton("YELLOW");


yellow.setBounds(320, 300, 100, 40);
yellow.setFont(font);
yellow.setBackground(c);

magenta = new JButton("MAGENTA");


magenta.setBounds(390, 350, 100, 40);
magenta.setFont(font);
magenta.setBackground(c);

orange = new JButton("ORANGE");


orange.setBounds(460, 400, 100, 40);
orange.setFont(font);
orange.setBackground(c);

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);
}

public void actionPerformed(ActionEvent ae)


{

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);

public static void main(String[] args) {

new changeBackground();

You might also like