Java Programs

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

BCA JAVA SLIPS

Slip 1. Write a java program to read the characters from a file, if a character is alphabet
then reverse its case, if not then display its category on the Screen. (whether it is Digit or
Space)
import java.io.*;
class Slip1
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter file name
"); String f1=br.readLine();

FileReader fr = new FileReader(f1);

int ch ;
while((ch=fr.read())!= -1)
{
char c = (char)ch;
if(Character.isUpperCase(c))
{
c=Character.toLowerCase(c)
; System.out.print(c);
}
else if(Character.isLowerCase(c))
{
c=Character.toUpperCase(c)
; System.out.print(c);
}
else if(Character.isDigit(c))
{
System.out.println(c+"Digit");
}
else if(Character.isSpace(c))
System.out.println(c+"Space"
);
else
System.out.println(c);
}
fr.close();
}
}

Slip 2. Write a java program to accept n names of cites from user and display them descending order

import java.io.*;
class Slip2
{ public static void main(String a[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter how many cities");
int n = Integer.parseInt(br.readLine());
Page 1
String s[]=new String[n];
for(int i=0;i<s.length;i++)
{
System.out.println("Enter city name ");
s[i]=br.readLine();
}
for(int i=0;i<s.length;i++)
{
for(int j=0;j<s.length-1;j++)
{
if(s[j].compareTo(s[j+1])<1)
{
String
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
System.out.println("Cities in Descending order : ");
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
}
}
}

Slip 3. Write a java program to accept the details of „n‟ employees (EName ,Salary)
from the user, store them into the Hashtable and displays the Employee Names havin
maximum Salary.
import java.util.*;
import java.io.*;

public class Slip3


{
public static void main(String args[])throws Exception
{
int n,sal=0;
String
name="";
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Hashtable h = new Hashtable();

System.out.println("\nEnter number of Employee : ");


n = Integer.parseInt(br.readLine());

for(int i = 1; i <= n; i++)


{
System.out.println("\nEnter name “);
name = br.readLine();
System.out.println("\nEnter Salary :
");
sal=Integer.parseInt(br.readLine());
h.put(name,sal);
}
Page 2
Enumeration v = h.elements();
//sal Enumeration k = h.keys();
//name while(k.hasMoreElements())

Page 3
{
System.out.println(k.nextElement()+" "+v.nextElement());
}
int max = 0;String str="";
k = h.keys();
v = h.elements();
while(v.hasMoreElements())
{
sal=(Integer)v.nextElement();
name =
(String)k.nextElement();
if(sal>max)
{
max = sal;
str = name;
}

}
System.out.println(str +" has maximum salary is "+max);
}
}
Slip4. Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED
and
MOUSE_CLICK and display the position of the Mouse_Click in a TextField.

import java.awt.*;
import java.awt.event.*;

classSlip4 extends Frame


{ TextField statusBar;
Slip4()
{
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
statusBar.setText("Clicked at (" +
e.getX() + "," + e.getY() + ")");
repaint();
}
public void mouseEntered(MouseEvent e)
{

statusBar.setText("Entered at (" + e.getX() + "," +


e.getY() + ")");
repaint();
}
});

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
Page 4
setSize(275,300);
setTitle("Mouse Click Position");

Page 5
statusBar = new
TextField(20);
add(statusBar);
setVisible(true);
}
public static void main(String []args)
{
new Slip4();
}
}

Slip 5. Define a class Student with attributes rollno and name. Define default and
parameterized constructor. Override the toString() method. Keep the count of
Objects created. Create objects using parameterized constructor and Display
the object count after each object is created.
import java.io.*;
class Slip5
{
static int cnt;
int roll;
String
nm;
Slip5()
{ nm="";
}

Slip5(int rno,String name)


{
roll=rno
;
nm=name;
cnt++;
System.out.println("objects created="+cnt);
}

public String toString()


{
return "rno = "+roll+" nm = "+nm;
}

public static void main(String a[]) throws Exception


{
int
n,i,rno;
String
name;

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("enter no of
students");
n=Integer.parseInt(br.readLine());

Slip5 ob[]=new Slip5[n];

Page 6
for(i=0;i<n;i++)
{
System.out.println("enter roll no");
rno=Integer.parseInt(br.readLine());

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

ob[i] = new Slip5(rno,name);

Page 7
}

System.out.println("Students are : ");


for(i=0;i<n;i++)
{
System.out.println(ob[i]);
}
}
}
Slip 6. Create a calculator with functionality in an Applet.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Slip6 extends Applet implements ActionListener
{
String msg=" ";
int
v1,v2,result;
TextField t;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,equals;
char
choice;
Panel p,p1;
public void init()
{
setLayout(new BorderLayout());
p =new Panel();

t=new TextField(20);
p.add(t);

p1=new Panel();

p1.setLayout(new GridLayout(5,4));
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}

equals=new
Button("="); add=new
Button("+"); sub=new
Button("-"); mul=new
Button("*"); div=new
Button("/"); clear=new
Button("C");

for(int i=0;i<10;i++)
{
p1.add(b[i]);
}

p1.add(equals)
; p1.add(add);
p1.add(sub);
Page 8
p1.add(mul);
p1.add(div);
p1.add(clear);

Page 9
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}

add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clear.addActionListener(this)
;
equals.addActionListener(this
); add(p,BorderLayout.NORTH);
add(p1);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
char ch = str.charAt(0);

if ( Character.isDigit(ch))
t.setText(t.getText()+str);

else if(str.equals("+"))
{
v1=Integer.parseInt(t.getText());
choice='+';
t.setText("");
}

else if(str.equals("-"))
{
v1=Integer.parseInt(t.getText());
choice='-';
t.setText("");
}

else if(str.equals("*"))
{
v1=Integer.parseInt(t.getText());
choice='*';
t.setText("");
}

else if(str.equals("/"))
{
v1=Integer.parseInt(t.getText());
choice='/';
t.setText("");
}

if(str.equals("="))
{
v2=Integer.parseInt(t.getText());
if(choice=='+')
result=v1+v2;

Page 10
else if(choice=='-')
result=v1-v2;

Page 11
else if(choice=='*')
result=v1*v2;

else if(choice=='/')
result=v1/v2;

t.setText(""+result);
}

if(str.equals("C"))
{
t.setText("");
}
}
}

/*<applet code="Slip6.class" width="300" height="300"> </applet>*/

Slip 7.. Write a java program to display “Hello Java” with settings Font- Georgia,
oreground color- Red, background color – Blue on the Frame.
import java.awt.*;
class Slip7 extends Frame
{ Label l;
Slip7()
{
l=new Label("Hello java");
l.setFont(new Font("Georgia",Font.BOLD,14));
l.setForeground(Color.RED);
add(l);
setBackground(Color.BLUE);
setSize(300,300);
setLayout(new
FlowLayout());
setVisible(true);

}
public static void main(String a[])
{ new Slip7();
}
}

Slip 8. Write a java program to design a following GUI (Use Swing).

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

class Slip8 extends JFrame


{
JLabel
l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3;
JTextArea t;

Page 12
JPanel p,p1,p2,p3;
ButtonGroup bg;
JRadioButton m,f;
JCheckBox
c1,c2,c3;

Page 13
JButton
b1,b2;
Slip8()
{
p =new JPanel();
p1=new JPanel();
l1=new JLabel("First Name
"); l2=new JLabel("last Name
"); l3=new JLabel("Address
"); l4=new JLabel("mobile No
");

t1=new
JTextField(10);
t2=new
JTextField(10);
t3=new
JTextField(10); t=new
JTextArea(2,10);
p.add(l1); p.add(t1);
p.add(l2); p.add(t2);
p.add(l3); p.add(t);
p.add(l4); p.add(t3);
p.setLayout(new GridLayout(4,2));
add(p);

l5=new JLabel("Gender ");


m = new JRadioButton("male");
f = new JRadioButton("female");

bg = new ButtonGroup();
bg.add(m);
bg.add(f);
p1.add(l5)
;
p1.add(m);
p1.add(f);
p1.setLayout(new GridLayout(1,3));

p2=new JPanel();
l6=new JLabel("Your Interests
"); c1=new
JCheckBox("Computer"); c2=new
JCheckBox("Sports");
c3=new JCheckBox("Music");

p2.add(l6)
;
p2.add(c1)
;
p2.add(c2)
;
p2.add(c3)
;
p2.setLayout(new GridLayout(1,4));

p3=new JPanel();
Page 14
b1=new
JButton("submit");
b2=new JButton("clear");
p3.add(b1);
p3.add(b2);

add(p);
add(p1)
;
add(p2)
;
add(p3)
;
setSize(300,400);
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);

Page 15
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String a[])
{
new Slip8();
}
}

Slip 9. Write a java program to display the contents of a file in reverse order.

import java.io.*;
class Slip9
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Ente file
name"); String fname=br.readLine();

FileInputStream fin = new FileInputStream(fname);


BufferedInputStream bis = new
BufferedInputStream(fin);

int s =
bis.available();
System.out.println(s);

for(int i=s-1;i>=0;i--)
{ bis.mark(i); bis.skip(i);
System.out.print((char)bis.read()
); bis.reset();
}
fin.close();
}
}

SLIP 10. Write an applet application in Java for designing Temple.

import java.applet.Applet;

import java.awt.*;

public class Slip10 extends Applet

{ public void paint(Graphics g)

{
g.drawRect(100,150,90,120);

g.drawRect(130,230,20,40);

g.drawLine(150,100,100,150)

Page 16
g.drawLine(150,100,190,150)

g.drawLine(150,50,150,100);

Page 17
g.drawRect(150,50,20,20);

}
}
/*<applet code="Slip10.class" width="300" height="300">
</applet>*/

SLIP 11. Write a java program to accept Employee name from the user and check
whether it is valid or not. If it is not valid then throw user defined Exception
“Name is Invalid” otherwise display it.
import java.io.*;
class InvalidNameException extends Exception
{}

class Slip11
{
String nm;
Slip11(String
nm)
{
this.nm=nm;
}

void display()
{
System.out.println("Name = "+nm);
}
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Ente
name"); String
name=br.readLine();
try
{
int len=name.length();
int flag=0;
for(int i=0;i<len;i++)
{
char ch=name.charAt(i);
if(Character.isLowerCase(ch)
|| Character.isUpperCase(ch))
{
flag=1;
}
else
{
flag=-1;
break;
}
}
if(flag==-1)
{
{
}
else
Page 18
} throw new InvalidNameException();

Slip11 s=new Slip11(name);


s.display();

Page 19
}
catch(InvalidNameException e)
{
System.out.println("Name is invalid");
}

}
}

SLIP 12. Write a java program to accept list of file names through command line and
delete the files having extension “.txt”. Display the details of remaining files
such as
FileName and size.

import java.io.*;
class Slip12
{
public static void main(String args[]) throws Exception
{
for(int i=0;i<args.length;i++)
{
File file=new File(args[i]);
if(file.isFile())
{
String name = file.getName();
if(name.endsWith(".txt"))
{
file.delete();
System.out.println("file is deleted " +
file);
}
else
{
System.out.println(name + "
"+file.length()+" bytes");
}
}
else
{ System.out.println(args[i]+ "is not a file");
}

}
}
}

SLIP 13. Write a java program to copy the contents of one file into the another file,
while copying change the case of alphabets and replace all the digits by „*‟ in target
file import java.util.*;
import java.io.*;
class Slip13
{
public static void main(String a[]) throws Exception
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

Page 20
System.out.println("enter file name to
copy"); String f1=br.readLine();

Page 21
System.out.println("enter destination
file"); String f2=br.readLine();

FileReader fr=new
FileReader(f1); FileWriter
fw=new FileWriter(f2);

int ch;
while((ch=fr.read() ) != -1)
{
char ch1=(char)ch;
if(Character.isUpperCase(ch1))
{
ch1=Character.toLowerCase(ch1);
fw.write(ch1);
}
else if(Character.isLowerCase(ch1))
{
ch1=Character.toUpperCase(ch1);
fw.write(ch1);
}
else if(Character.isDigit(ch1))
{
ch1='*';
fw.write(ch1);
}
else if(Character.isSpace(ch1))
{
fw.write(ch1);
}
}

fr.close();
fw.close();
}
}

SLIP 14. Write a Java program which will create a frame if we try to close it, it should
change it‟s color and it remains visible on the screen(Use swing).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Slip14 extends JFrame
{
JPanel p = new
JPanel(); Slip14()
{ setVisible(true);
setSize(400,400);
setTitle("Swing
Background");
add(p);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
Page 22
{ p.setBackground(Color.RED);

Page 23
JOptionPane.showMessageDialog(null,"Close
window","Login",JOptionPane.INFORMATION_MESSAGE
);

}
});
}

public static void main(String args[])


{
new Slip14();
}
}

SLIP 15. Define an abstract class Shape with abstract methods area() and volume().
Write a java program to calculate area and volume of Cone and Cylinder.

import java.util.*;
abstract class Shape
{
abstract public void area();
abstract public void vol();
}
class Cone extends Shape
{
int r,s,h;
Cone(int r,int s,int h)
{
thi
s.r=r;
this.s=s
;
this.h=h
;
}
public void area()
{
System.out.println("Area of Cone = "+(3.14*r*s));
}
public void vol()
{
System.out.println("volume of Cone = "+(3.14*r*r*h)/3);
}
}

class Cylinder extends Shape


{ int r,h;
Cylinder(int r,int h)
{ this.r=r;
this.h=h;
}
public void area()
{ System.out.println("Area of Cylinder = "+(2*3.14*r*h));
}
public void vol()
Page 24
{
System.out.println("volume of Cylinder
= "+(3.14*r*r*h));
}
}

Page 25
class Slip15
{
public static void main(String a[])
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter radius, side and height
for
cone");
int r =sc.nextInt();
int s1
=sc.nextInt(); int h
=sc.nextInt(); Shape
s;
s=new Cone(r,s1,h);
s.area();
s.vol();

System.out.println("Enter radius, height for cylinder");


r =sc.nextInt();
h =sc.nextInt();
s=new
Cylinder(r,h);
s.area();
s.vol();
}
}

SLIP 16. Write an application in Java using Awt to display 4 X 4 squares on the screen.
One of the block will be active with black color. All other blocks should be filled
with blue color. Provide command buttons as follows to move the active cell.
The active cell should be changed only if it is within the boundary of the
squares otherwise give the beep.

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

public class Slip16 extends JFrame implements ActionListener


{
JButton moveBtns[]=new
JButton[4]; JPanel
gridPanels[]=new JPanel[16];
String icons[]={"Left","Up","Right","Down"};
int
cnt=1;
Slip16()
{
setLayout(new GridLayout(5,4));
for(int i=0;i<16;i++)
{
gridPanels[i]=new JPanel();
add(gridPanels[i]);

gridPanels[i].setBorder(BorderFactory.createLineBorder( Color.black
,1

Page 26
));
gridPanels[i].setBackground(Color.blue);
}
gridPanels[0].setBackground(Color.black);
for(int i=0;i<4;i++)
{
moveBtns[i]=new JButton(icons[i]);
add(moveBtns[i]);

Page 27
moveBtns[i].addActionListener(this);
}

setSize(300,300);
setVisible(true);
setTitle("Grid
Example");
}
public void actionPerformed(ActionEvent e)
{
gridPanels[cnt-1].setBackground(Color.blue);
if(e.getSource()==moveBtns[0])
{
if(cnt%4!=1)
cnt--;
}
if(e.getSource()==moveBtns[1])
{
if(cnt>4)
cnt=cnt-4;
}
if(e.getSource()==moveBtns[2])
{
if(cnt%4!=0)
cnt++;
}
if(e.getSource()==moveBtns[3])
{
if(cnt<13)
cnt=cnt+4;
}
gridPanels[cnt-1].setBackground(Color.black);
}

public static void main(String args[])


{
new Slip16();
}
}

SLIP 17. Write a Java program to design a screen using Awt that will take a user
name and password. If the user name and password are not same, raise
an Exception with appropriate message. User can have 3 login chances only. Use
clear button to clear the TextFields.

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

class InvalidPasswordException extends Exception


{}

class Slip17 extends JFrame implements ActionListener


{
JLabel name, pass;
JTextField nameText;
Page 15
JPasswordField
passText; JButton
login, end;

Page 16
static int cnt=0;

Slip17()
{
name = new JLabel("Name : ");
pass = new JLabel("Password : ");

nameText = new JTextField(20);


passText = new JPasswordField(20);

login = new JButton("Login");


end = new JButton("End");

login.addActionListener(this);
end.addActionListener(this);

setLayout(new GridLayout(3,2));
add(name);
add(nameText)
; add(pass);
add(passText)
; add(login);
add(end);

setTitle("Login Check");
setSize(300,300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==end)
{
System.exit(0)
;

}
if(e.getSource()==login)
{
try
{
String user = nameText.getText();
String pass = new String(passText.getPassword());

if(user.compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,"Login
Successful","Login",JOptionPane.INFORMATION_MESSAG
E); System.exit(0);
}

else
{
throw new InvalidPasswordException();
}
}
Page 17
catch(Exception e1)

Page 18
{
cnt++;
JOptionPane.showMessageDialog(null,"Log
in
Failed","Login",JOptionPane.ERROR_MESSAGE);
nameText.setText("");
passText.setText("");
nameText.requestFocus()
; if(cnt == 3)
{
JOptionPane.showMessageDialog(null,"3
Attempts
Over","Login",JOptionPane.ERROR_MESSAGE
);
System.exit(0);

}
}
}
}

public static void main(String args[])


{
new Slip17();
}
}
SLIP 18. Write a java program that displays the number of characters, lines & words
from a file.
import java.io.*;
class Slip18
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in))
;
System.out.println("Enter source file name
"); String f1=br.readLine();
FileReader fr = new FileReader(f1);
int ch ;
int wc= 0,line=0, cc=0;
while((ch=fr.read())!= -1)
{ char c = (char)ch;
cc++;
if(c=='\n')
{ line++;
wc++;
}
else if(c==' ' || c=='\n')
wc++;
}

System.out.println("Line count = "+line+"\n


Character count = "+cc+"\n word count = "+wc);
fr.close();
}

Page 17
}

SLIP 19.Write a java program to accept a number from the user, if number is zero
then throw user defined Exception “Number is 0” otherwise calculate the sum of first
and last
digit of a given number (Use static keyword).

Page 18
import java.io.*;
class NumberZero extends Exception
{
}

class Slip19
{
static void add_digit(int no)
{ int
l=0,f=0; l
= no%10;
if(no>9)
{
while(no>0)
{
f = no%10;
no=no/10;
}
}
System.out.println("Addotion of first and last digit =
"+(f+l));
}

public static void main(String a[]) throws IOException


{ BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter
no");
int no=Integer.parseInt(br.readLine());
try
{ if(no==0)
{
throw new NumberZero();
} else
add_digit(no)
;
}//end of try
catch(NumberZero
e)
{
System.out.println("no is zero");
}
}
}

SLIP 20.Write a package for Games in Java, which have two classes Indoor and Outdoor.
Use a function display () to generate the list of players for the specific games.
(Use
Parameterized constructor, finalize() method and Array Of Objects)

import game.Indoor;
import
game.Outdoor;
import java.io.*;

class Slip20
Page 19
{
protected void finalize()
{
System.out.println("Finalized mthod is invoked");
}
public static void main(String a[]) throws IOException

Page 20
{
String nm,gm;
int i;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter no indoor player");
int n=

Integer.parseInt(br.readLine());

Indoor in[]=new Indoor[n];

for(i=0;i<n;i++)
{
System.out.println("enter palyer
name"); nm=br.readLine();
System.out.println("enter gamename");
gm=br.readLine();
in[i]=new Indoor(nm,gm);
}

System.out.println("enter no outdoor player");


int n1=

Integer.parseInt(br.readLine());

Outdoor o[]=new Outdoor[n1];

for(i=0;i<n1;i++)
{
System.out.println("enter palyer
name"); nm=br.readLine();
System.out.println("enter gamename");
gm=br.readLine();
o[i]=new Outdoor(nm,gm);
}
System.out.println("enter game name for indoor : ");
gm=br.readLine();
System.out.println("Player name of specified indoor game
: ");
for(i=0;i<n;i++)
{
in[i].display(gm);
}
System.out.println("enter game name for outdoor : ");
gm=br.readLine();
System.out.println("Player name of specified outdoor
: "); game

for(i=0;i<n1;i++)
{
o[i].display(gm);
}
Slip20 ob = new Slip20();
ob.finalize();
}
}
Page 21
SLIP 21. Write a Java program to design a screen using Swing that will create four
TextFields. First for the text, second for what to find and third for replace. Display

Page 22
result in the fourth TextField. Display the count of total no. of replacements
made. The button clear to clear the TextFields.

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

public class Slip21 extends JFrame implements ActionListener


{
JLabel ltext, lfind, lreplace,
loccurance; JTextArea text;
JTextField findText, replaceText,
occurrenceText; JButton find, replace, clear;
JPanel pan1,pan2;
int occurrences=0,i=0;

Slip21()
{
ltext= new JLabel("Enter Text : ");
lfind = new JLabel("Text to Find : ");
lreplace = new JLabel("Text to Replace : ");
loccurance = new JLabel("No.of Occurrences : ");

text = new JTextArea(1,20);


findText = new JTextField(20);
replaceText = new JTextField(20);
occurrenceText = new
JTextField(20);

pan1 = new JPanel();


pan1.setLayout(new
GridLayout(4,2)); pan1.add(ltext);
pan1.add(text);
pan1.add(lfind);
pan1.add(findText);
pan1.add(lreplace);
pan1.add(replaceText);
pan1.add(loccurance);
pan1.add(occurrenceText)
;

find = new JButton("Find");


replace = new
JButton("Replace"); clear= new
JButton("Clear");

find.addActionListener(this);
replace.addActionListener(this
);
clear.addActionListener(this);

pan2 = new JPanel();


pan2.setLayout(new
FlowLayout()); pan2.add(find);
pan2.add(replace);
pan2.add(clear);
Page 23
add(pan1,"Center");
add(pan2,"South");

Page 24
setTitle("Find And Replace"); setSize(300,
200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
); setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == find)
{
String s = text.getText();
String f =
findText.getText(); i =
s.indexOf(f);
if(i != -1)
{
occurrences++;

occurrenceText.setText(Integer.toString(occurrences
)); text.select(i,i+f.length());
text.requestFocus();

}
}
if(ae.getSource() == replace)
{
if(text.getSelectedText().length()!=0)
{
String r = replaceText.getText();
text.replaceSelection(r);
}
}
if(ae.getSource() == clear)
{
text.setText("");
findText.setText("");
replaceText.setText("");
occurrenceText.setText("")
;
}
}

public static void main(String[] args)


{
new Slip21();
}
}

SLIP 22. Define an Interface Shape with abstract method area(). Write a java program to
calculate an area of Circle and Sphere.(use final keyword)

import java.io.*;
interface Shape
{
float pi=3.14f;
void area();
}

Page 25
class Circle implements Shape
{ int r;
Circle(int
r)

Page 26
{
this.r = r;
}
public void area()
{
System.out.println("Area of Circle = "+(pi*r*r));
}

class Sphere implements Shape


{ int r;
Sphere(int
r)
{ this.r = r;
}
public void area()
{ System.out.println("Area of Sphere = "+(4*pi*r*r));
}
}

class Slip22
{
public static void main(String a[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in))
;
System.out.println("Enter radius for circle : ");
int r =Integer.parseInt(br.readLine());

Shape s;
s=new Circle(r);
s.area();

System.out.println("Enter radius for sphere : ");


r =Integer.parseInt(br.readLine());
s=new Sphere(r);
s.area();

}
}

SLIP 23.. Write a java program to accept the details of n Cricket Players from user
(Player code, name, runs, innings- played and number of times not out). The
program should contain following menus:
-Display average runs of a single player.

-Display average runs of all players. (Use array of objects, Method overloading
and static keyword)

import java.util.*;
class Slip23
{ float avg2=0;
int pcode, runs,ing,
out; String pname;
static int cnt;
Page 27
void accept()
{ Scanner s=new Scanner(System.in);
System.out.println("Enter pid,
pname");

Page 28
pcode=s.nextInt();
pname=s.next();
System.out.println("Enter runs, inings & no of
not out"); times

runs=s.nextInt()
;
ing=s.nextInt();
} out=s.nextInt();
cnt++;

void display()
{ System.out.print(" name = "+pname+" pcode
"+pcode); =

System.out.print(" runs = "+runs+" inings


=
"+ing+"out = "+out);
}
void display(int n)
{ int a;
a=n;
float avg1=runs/ing;
System.out.println("Avg runs of single player
"+avg1); =

avg2=avg2+runs;
}
void display1()
{ float ag=avg2;
ag=ag/cnt;
System.out.println("Avg runs of all players="+ag);
}
public static void main(String a[])
{ int n,ch;
System.out.println("Enter no of players
"); Scanner s=new Scanner(System.in);
n=s.nextInt();
Slip23 ob[]=new Slip23[n];
System.out.println("Enter details ");
for(int i=0;i<n;i++)
{ ob[i]=new Slip23();
ob[i].accept()
;
}
System.out.println(" players are \n ");
for(int i=0;i<n;i++)
{ ob[i].display();
}

do
{ System.out.println("Enter choice ");
System.out.println("1.avg of 1 player
\n2.avg
of all player \n exit");
ch=s.nextInt()
; switch(ch)

Page 29
{ case 1: for(int i=0;i<n;i++)
{
ob[i].display(n);
}
break;
case 2: ob[1].display1();

Page 30
break;
case 3:System.exit(1);
default:
choice"); System.out.println("wrong

}
}while(ch!=3);
}
}

SLIP 24. Write a Java Program to accept the details of Employee(Eno, EName,Sal) from
the
user and display it on the next Frame. (Use AWT)

import java.awt.*;
import java.awt.event.*;

class Slip24 extends Frame implements ActionListener


{
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.add(l1);
p1.add(txt1);
p1.add(l2);
p1.add(txt2);
p1.add(l3);
p1.add(txt3);

Page 31
p1.add(submit)
;
p1.add(clear);

add(p1);
setVisible(true)
;
setSize(400,400)
;
}

public void actionPerformed(ActionEvent ae)


{

Page 32
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();
}
}

Employee_Detail.java

import javax.swing.*;
import
java.awt.event.*;
import java.awt.*;
class Employee_Detail extends Frame
{ Label
l1,l2,l3,l,l4,l5,l6;
Panel p=new Panel();
Employee_Detail(String e,String add,String s )
{ l=new Label("EMPLOYEE INFORMTION");
l1=new Label("Emp Name ");
l2=new Label("Address ");
l3=new Label("Salary ");

l4=new Label(e);
l5=new
Label(add);
l6=new Label(s);
p.add(l1);
p.add(l4);
p.add(l2);
p.add(l5);
p.add(l3);
p.add(l6);
add(p);
setVisible(true);
setSize(400,400);
}
Page 33
}

SLIP 25. Define an Employee class with suitable attributes having getSalary() method,
which returns salary withdrawn by a particular employee. Write a class Manager
which extends a class Employee, override the getSalary() method, which will
return salary
of manager by adding traveling allowance, house rent allowance etc.

Page 34
import java.io.*;
class Emp
{
String
nm; float
sal; int
no;
BufferedReader br = new
BufferedReader(new
InputStreamReader(System.in));
void accept() throws
IOException
{
System.out.println("Enter emp no");
no=Integer.parseInt(br.readLine());
System.out.println("Enter emp
name"); nm=br.readLine();
System.out.println("Enter sal");
sal=Float.parseFloat(br.readLine())
;

}
float getsalary()
{
return sal;
}
void display() throws IOException
{
System.out.println("id = "+no+"\n name = "+nm+"\n salary
= "+sal);
}
}
class Manager extends Emp
{
float tr,ha,t_sal;
void accept() throws IOException
{
super.accept();
System.out.println("Enter travelling
all"); tr=Float.parseFloat(br.readLine());
System.out.println("Enter house rent
allo");
ha=Float.parseFloat(br.readLine());
float s=super.getsalary();
t_sal=s+tr+ha;
}
float getsalary()
{
return t_sal;
}
void display() throws IOException
{
super.display();
System.out.println("total salary ="+getsalary());
}
public static void main(String a[]) throws IOException
{

Page 35
Manager ob =new Manager();
ob.accept();
ob.display();
}
}

Page 36
SLIP 26.Write a java Program to accept „n‟ no‟s through the command line and store all
the prime no‟s and perfect no‟s into the different arrays and display both the
arrays.

class Slip26
{
public static void main(String a[])
{
int no[]=new
int[a.length];
int
i,k=0,l=0;
int pm[]=new
int[a.length]; int
pr[]=new int[a.length];
for(i=0;i<a.length;i++)
{
no[i]=Integer.parseInt(a[i]);
}

System.out.println("Given array is ");


for(i=0;i<a.length;i++)
{
System.out.print(no[i]);
}

for(i=0;i<a.length;i++)
{
int j=2;
int flag=0;
while(j<=no[i]/2)
{
if(no[i]%j==0)
{
flag=1;
break;
}
j++;
}

if(flag==0)
{
pm[k]=no[i];
k++;
}
else
{
int sum=0; j=1;
while(j<=no[i]/2
)
{
if(no[i]%j==0
)
sum=sum+j;
j++;
}
if(no[i]==sum)

Page 37
{
pr[l]=no[i];
l++;
}
}
}//end of for

Page 38
System.out.println("prime numbers are");
for(i=0;i<k;i++)
{
System.out.print(pm[i]+" ");
}

System.out.println("perfect numbers are");


for(i=0;i<l;i++)
{
System.out.print(pr[i]+" ");

}
}
}

SLIP 28. Write a java program to read n Students names from user, store them into the
Array List collection. The program should not allow duplicate names. Display
the names in Ascending order.

import java.util.*;
import java.io.*;

public class Slip28


{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

ArrayList a = new ArrayList();

System.out.println("\nEnter number of Employee :

");
int n = Integer.parseInt(br.readLine());

System.out.println("\nEnter name : ");


for(int i = 1; i <= n; i++)
{
a.add(br.readLine());
}
TreeSet tr = new TreeSet(a);
System.out.println("Sorted name are :
"+tr);
}
}

SLIP 29. Write a java program to accept n employee names from user, store them into the
LinkedList class and display them by
using. a. Iterator Interface
b. ListIterator Interface
import java.util.*;
import java.io.*;

Page 39
public class Slip29
{
public static void main(String args[])throws Exception

Page 40
{
int n;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

LinkedList li = new LinkedList ();

System.out.println("\nEnter number of Employee :

");
n = Integer.parseInt(br.readLine());

System.out.println("\nEnter name : ");


for(int i = 1; i <= n; i++)
{
li.add(br.readLine());
}

System.out.println("\nLink List Content :


"); Iterator it = li.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}

System.out.println("\nReverse order :
"); ListIterator lt =
li.listIterator(); while(lt.hasNext())
{
lt.next();
}
while(lt.hasPrevious())
{
System.out.println(lt.previous());
}
}
}

SLIP 30. Write an applet application in Java for smile face.


import java.applet.Applet;
import java.awt.*;

public class Slip30 extends Applet


{
public void paint(Graphics g)
{
g.drawOval(50,15,200,200);
g.fillOval(80,90,30,15);
g.fillOval(190,90,30,15);
g.drawOval(17,90,30,50);
g.drawOval(250,90,30,50);
g.drawLine(150,110,150,150)
;
//g.fillOval(80,160,10,35);
g.drawArc(100,160,100,30,170,200);

Page 41
}
}
/*<applet code="Slip30.class" width="300" height="300"> </applet>
*/

Page 42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy