Java Programs
Java Programs
Java Programs
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();
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.*;
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.*;
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="";
}
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();
Page 7
}
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("");
}
}
}
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();
}
}
import java.awt.*;
import javax.swing.*;
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);
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();
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();
}
}
import java.applet.Applet;
import java.awt.*;
{
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();
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
);
}
});
}
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);
}
}
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();
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;
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);
}
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.*;
Page 16
static int cnt=0;
Slip17()
{
name = new JLabel("Name : ");
pass = new JLabel("Password : ");
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);
}
}
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);
}
}
}
}
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));
}
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());
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);
}
Integer.parseInt(br.readLine());
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.*;
Slip21()
{
ltext= new JLabel("Enter Text : ");
lfind = new JLabel("Text to Find : ");
lreplace = new JLabel("Text to Replace : ");
loccurance = new JLabel("No.of Occurrences : ");
find.addActionListener(this);
replace.addActionListener(this
);
clear.addActionListener(this);
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("")
;
}
}
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 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();
}
}
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); =
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.*;
Slip24()
{
l=new Label("EMPLOYEE INFORMTION");
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)
;
}
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("")
;
}
}
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]);
}
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]+" ");
}
}
}
}
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.*;
");
int n = Integer.parseInt(br.readLine());
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));
");
n = Integer.parseInt(br.readLine());
System.out.println("\nReverse order :
"); ListIterator lt =
li.listIterator(); while(lt.hasNext())
{
lt.next();
}
while(lt.hasPrevious())
{
System.out.println(lt.previous());
}
}
}
Page 41
}
}
/*<applet code="Slip30.class" width="300" height="300"> </applet>
*/
Page 42