Java Solved Slips For Practical
Java Solved Slips For Practical
Java Solved Slips For Practical
slip 1-
sem1
Create an abstract class shape .Derive three classes sphere, cone and cylinder from
it .Calculate area and volume of all (use method overriding)
/*
sphere =>4*3.14*r*r
cone=>4*3.14*h
cylinder=>2*3.14*r*h
box=>l*b
sphere volume=>4/3*3.14*r*r*r
cylinder 3.14*r*r*h
box=>l*b*h
*/
import java.util.*;
abstract class Shape
{
static double pi=3.14;
public abstract double cal_area();
public abstract double cal_volume();
}
Sphere()
{
}
Sphere(double r)
{
this.r=r;
}
public double cal_area()
{
return(4*pi*r*r);
}
public double cal_volume()
{
double vol;
vol=(4/3)*pi*r*r*r;
return vol;
}
}
class Cone extends Shape
{
double h;
double r;
}
Cone(double h,double r)
{
this.h=h;
this.r=r;
}
public double cal_area()
{
return(4*pi*h);
}
public double cal_volume()
{
return(0.33*pi*r*r*h);
}
}
class Cylinder extends Shape
{
double h;
double r;
Cylinder()
{
}
Cylinder(double h,double r)
{
this.h=h;
this.r=r;
}
public double cal_area()
{
return(2*3.14*r*h);
}
public double cal_volume()
{
return(3.14*r*r*h);
}
}
class shapeDemo
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
double r;
double h;
System.out.println("Enter the radius :: ");
r=s.nextDouble();
System.out.println("Enter the height :: ");
h=s.nextDouble();
Sphere s1=new Sphere(r);
System.out.println("\n**** For Sphere****\n");
System.out.println("The Area of Sphere is :: "+s1.cal_area());
System.out.println("The Volume of Sphere is :: "+s1.cal_volume());
}
}
output:
[root@localhost gitanjalee]# java ShapeDemo
sem 2
Design an HTML page containing 4 option buttons (Painting, Drawing, Singing
and swimming) and 2 buttons reset and submit. When the user clicks submit, the server
responds by adding a cookie containing the selected hobby and sends a message back to
the client. Program should not allow duplicate cookies to be written.
*************************************************/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Cookie []cookies=req.getCookies();
if(cookies !=null)
{
pw.println("Existing Cookies :");
for(int i=0;i<cookies.length;i++)
{
String name=cookies[i].getName();
String value=cookies[i].getValue();
pw.println("Name = "+name+" Value= "+value+"<br>");
}
for(int i=0;i<cookies.length;i++)
{
if(cookies[i].getValue().equals(data))
{
pw.println("Cookis Exists for "+data);
}
}
}
Cookie c=new Cookie("hobby",data);
c.setMaxAge(6000);
res.addCookie(c);
pw.println("cookie added for Hobby"+data);
pw.println("</body> </html>");
}catch(Exception e){}
}
}
hobby.html
<html>
<body>
<form action=hobbie method=post>
<input type=radio name=hobby value=Painting>Painting<br>
public Numbers()
{
f = new JFrame("Numbers");
sel=1;res=0;
p=new JPanel();
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Compute");
m3=new JMenu("Operation");
m4=new JMenu("Sort");
l=new JLabel("Numbers");
t=new JTextField(20);
b=new ButtonGroup();
r1=new JRadioButtonMenuItem("Ascending");
r1.addItemListener(this);
r2=new JRadioButtonMenuItem("Descending");
r2.addItemListener(this);
b.add(r1);b.add(r2);
String
str[]={"Load","Save","Exit","Sum","Average","Maximum","Minimum","Median","Search"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
{
m[i]=new JMenuItem(str[i]);
m[i].addActionListener(this);
}
f.setLayout(new BorderLayout());
p.add(l);
p.add(t);
f.add(p,BorderLayout.CENTER);
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(m[0]);
m1.add(m[1]);
m1.addSeparator();
m1.add(m[2]);
m2.add(m[3]);
m2.add(m[4]);
m2.add(m[5]);
m2.add(m[6]);
m2.add(m[7]);
m3.add(m[8]);
m3.add(m4);
m4.add(r1);
m4.add(r2);
f.add(mb,BorderLayout.NORTH);
f.setSize(300,200);
f.setVisible(true);
f.setLocation(500,200);
f.setDefaultCloseOperation(3);
else if(r2.isSelected())
{
sortdesc();
}
}
public void sortasc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
public void sortdesc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]<arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("Exit"))
{
System.exit(0);
}
else if(s.equals("Load"))
{
if(arr[0]==0)
{
int i=0;
try
{
BufferedReader r=new BufferedReader(new FileReader("num.txt"));
String s1=r.readLine();
while(s1!=null)
{
ss.append(s1);
ss.append(" ");
arr[i]=Integer.parseInt(s1);
n=++i;
s1=r.readLine();
}
}
catch(Exception eee) {}
t.setText(new String(ss));
}
}
else if(s.equals("Sum"))
{
t.setText(new Integer(givesum()).toString());
}
else if(s.equals("Average"))
{
t.setText(new Double(giveavg()).toString());
}
else if(s.equals("Maximum"))
{
t.setText(new Integer(givemax()).toString());
}
else if(s.equals("Minimum"))
{
t.setText(new Integer(givemin()).toString());
}
else if(s.equals("Median"))
{
t.setText(new Integer(givemed()).toString());
}
else if(s.equals("Save"))
{
char ch;
String sss = t.getText();
try{
FileOutputStream br1 = new FileOutputStream("number.txt");
for(int i=0;i<sss.length();i++)
{
ch=sss.charAt(i);
if(ch == ' ')
br1.write('\n');
else
br1.write(ch);
}
br1.close();
}
catch(Exception eee){ }
}
else if(s.equals("Search"))
{
int ser=Integer.parseInt(JOptionPane.showInputDialog("Enter Number To Search"));
int a=0;
for(int i=0;i<n;i++)
{
if(arr[i]==ser)
{
t.setText("Number Present");
a=1;
}
if(a==0)
t.setText("Number Not Present");
}
}
}
public int givesum()
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
return sum;
}
public double giveavg()
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
return sum/n;
}
public int givemax()
{
int sum=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i] > sum)
sum=arr[i];
}
return sum;
}
public int givemin()
{
int sum=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i] < sum)
sum=arr[i];
}
return sum;
}
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
a. 12 15 2015
b. 31 6 1990
c .29 2 2015
import java.io.*;
class set4b1
{
public static void main(String args[]) throws IOException
{
//BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
try
{
int c = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int a = Integer.parseInt(args[2]);
boolean leap=(c%400==0) || (c%4==0) && (c%100!=0);
if(b<13&&b>0)
{
}
else
throw new InvalidMonthException();
if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
if(a<32 &&a>0)
{
}
else
throw new InvalidDayException();
}
else if(b==4||b==6||b==9||b==11)
{
if(a<31&&a>0)
{
System.out.println("Accepted....");
}
else
throw new InvalidDayException();
}
else
{
if(leap && a>29)
{
System.out.println("Accepted.....");
}
else
throw new InvalidDayException();
}
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
public Numbers()
{
f = new JFrame("Numbers");
sel=1;res=0;
p=new JPanel();
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Compute");
m3=new JMenu("Operation");
m4=new JMenu("Sort");
l=new JLabel("Numbers");
t=new JTextField(20);
b=new ButtonGroup();
r1=new JRadioButtonMenuItem("Ascending");
r1.addItemListener(this);
r2=new JRadioButtonMenuItem("Descending");
r2.addItemListener(this);
b.add(r1);b.add(r2);
String
str[]={"Load","Save","Exit","Sum","Average","Maximum","Minimum","Median","Search"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
{
m[i]=new JMenuItem(str[i]);
m[i].addActionListener(this);
}
f.setLayout(new BorderLayout());
p.add(l);
p.add(t);
f.add(p,BorderLayout.CENTER);
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(m[0]);
m1.add(m[1]);
m1.addSeparator();
m1.add(m[2]);
m2.add(m[3]);
m2.add(m[4]);
m2.add(m[5]);
m2.add(m[6]);
m2.add(m[7]);
m3.add(m[8]);
m3.add(m4);
m4.add(r1);
m4.add(r2);
f.add(mb,BorderLayout.NORTH);
f.setSize(300,200);
f.setVisible(true);
f.setLocation(500,200);
f.setDefaultCloseOperation(3);
}
public void itemStateChanged(ItemEvent ii)
{
if(r1.isSelected())
{
sortasc();
}
else if(r2.isSelected())
{
sortdesc();
}
}
public void sortasc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
t.setText(new String(s5));
}
public void sortdesc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]<arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
char ch;
String sss = t.getText();
try{
FileOutputStream br1 = new FileOutputStream("number.txt");
for(int i=0;i<sss.length();i++)
{
ch=sss.charAt(i);
if(ch == ' ')
br1.write('\n');
else
br1.write(ch);
}
br1.close();
}
catch(Exception eee){ }
}
else if(s.equals("Search"))
{
int ser=Integer.parseInt(JOptionPane.showInputDialog("Enter Number To Search"));
int a=0;
for(int i=0;i<n;i++)
{
if(arr[i]==ser)
{
t.setText("Number Present");
a=1;
}
if(a==0)
t.setText("Number Not Present");
}
}
}
public int givesum()
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
return sum;
}
public double giveavg()
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
return sum/n;
}
public int givemax()
{
int sum=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i] > sum)
sum=arr[i];
}
return sum;
}
public int givemin()
{
int sum=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i] < sum)
sum=arr[i];
}
return sum;
}
public int givemed()
{
return arr[(n/2)];
}
/*Output
[student@localhost ~]$ javac Numbers.java
[student@localhost ~]$ java Numbers*/
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
}
class savingAccount
{
long acNo;
String name;
double balance=500;
Scanner s=new Scanner(System.in);
savingAccount(){}
savingAccount(long acNo,String name)
{
this.acNo= acNo;
this.name=name;
}
void viewBalance()
{
System.out.println("Current Balance :: "+balance);
}
void withdraw() throws InsufficientFundsException
{
System.out.println("Enter the amount to withdraw :: ");
double amt=s.nextDouble();
if((balance-amt)<500)
{
throw new InsufficientFundsException();
//System.out.println("Current Balance :: "+balance);
}
else
{
balance=balance-amt;
System.out.println("Current Balance after withdrwal :: "+balance);
}
}
void deposit()
{
System.out.println("Enter the amount to deposit :: ");
double amt=s.nextDouble();
balance=balance+amt;
}
do
{
System.out.println("Enter\n1.View Balance\n2.Deposit\n3.Withdraw\n");
op=s.nextInt();
switch(op)
{
case 1:
sa.viewBalance();
break;
case 2:
sa.deposit();
break;
case 3:
try
{
sa.withdraw();
}
catch(InsufficientFundsException e)
{
System.out.println(" Insufficient Funds \n");
}
break;
}
System.out.println("Enter 1 to Continue \n2 to exit :: ");
ch=s.nextInt();
}while(ch!=2);
}
}
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
txtDec=new JTextField();
txtBin=new JTextField();
txtOct=new JTextField();
txtHex=new JTextField();
btnCalc=new JButton("Calculate");
btnClear=new JButton("Clear");
setTitle("Conversion");
setSize(300,250);
setLayout(new GridLayout(5,2));
add(lblDec);
add(txtDec);
add(lblBin);
add(txtBin);
add(lblOct);
add(txtOct);
add(lblHex);
add(txtHex);
add(btnCalc);
add(btnClear);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnCalc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int n=Integer.parseInt(txtDec.getText());
txtBin.setText(Integer.toBinaryString(n));
txtOct.setText(Integer.toOctalString(n));
txtHex.setText(Integer.toHexString(n));
}
});
btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
txtDec.setText("");
txtBin.setText("");
txtOct.setText("");
txtHex.setText("");
txtDec.requestFocus();
}
});
}
public static void main(String args[])
{
new Conversion();
}
}
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
import java.util.*;
abstract class Shape
{
static double pi=3.14;
public abstract double cal_area();
public abstract double cal_volume();
}
Sphere()
{
}
Sphere(double r)
{
this.r=r;
}
public double cal_area()
{
return(4*pi*r*r);
}
public double cal_volume()
{
double vol;
vol=(4/3)*pi*r*r*r;
return vol;
}
}
class Cone extends Shape
{
double h;
double r;
}
Cone(double h,double r)
{
this.h=h;
this.r=r;
}
public double cal_area()
{
return(4*pi*h);
}
public double cal_volume()
{
return(0.33*pi*r*r*h);
}
}
class Cylinder extends Shape
{
double h;
double r;
Cylinder()
{
}
Cylinder(double h,double r)
{
this.h=h;
this.r=r;
}
public double cal_area()
{
return(2*3.14*r*h);
}
public double cal_volume()
{
return(3.14*r*r*h);
}
}
class shapeDemo
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
double r;
double h;
System.out.println("Enter the radius :: ");
r=s.nextDouble();
System.out.println("Enter the height :: ");
h=s.nextDouble();
Sphere s1=new Sphere(r);
System.out.println("\n**** For Sphere****\n");
System.out.println("The Area of Sphere is :: "+s1.cal_area());
System.out.println("The Volume of Sphere is :: "+s1.cal_volume());
}
}
output:
sem 2-
Create a table Student with the fields roll number ,name, percentage using
Postgresql .Write a menu driven program (Command line Interface) to perform the
following operations on student table .
a. Insert
b. Modify
c. Delete
d. Search
e. View All
f. Exit
/************************************************************************************
ASSIGNMENT 3 Set B Q (1)
Q 1. Write a menu driven program to perform the following operations on student table.
1. Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit
**************************************************************************************/
import java.io.*;
import java.sql.*;
try
{
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection
("jdbc:mysql://localhost/student","root","");
while(true)
{
System.out.println("1.Insert");
System.out.println("2.Modify");
System.out.println("3.Delete");
System.out.println("4.View all");
System.out.println("5.Search");
System.out.println("6.Exit");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
ps=con.prepareStatement
("insert into student values(?,?,?)");
ps.setInt(1,Integer.parseInt(br.readLine()));
ps.setString(2,br.readLine());
ps.setInt(3,Integer.parseInt(br.readLine()));
ps.executeUpdate();
break;
case 2:
ps=con.prepareStatement
("update student set per=? where roll_no=?");
case 3:
ps=con.prepareStatement
("delete from student where roll_no=?");
System.out.println("Enter Roll_no :");
ps.setInt(1,Integer.parseInt(br.readLine()));
ps.executeUpdate();
break;
case 4:
ps=con.prepareStatement("select * from student");
rs=ps.executeQuery();
while(rs.next())
{
System.out.print(rs.getInt("roll_no")+"\t");
System.out.print(rs.getString("sname")+"\t");
System.out.print(rs.getInt("per")+"\n");
}
break;
case 5:
System.out.print("Enter Rollno to Search:");
int rno = Integer.parseInt(br.readLine());
ps=con.prepareStatement
("select * from student where roll_no=?");
ps.setInt(1,rno);
rs=ps.executeQuery();
while(rs.next())
{
System.out.print(rs.getInt("roll_no")+"\t");
System.out.print(rs.getString("sname")+"\t");
System.out.print(rs.getInt("per")+"\n");
}
break;
case 6:
con.close();
System.exit(0);
}//end of switch
}//end of while
}//end of try
catch(Exception e){}
}//end of class
}//end of main()
/************output*************
[root@localhost JDBC]# java studentrecord
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
1
Enter Roll Name Per:
622
Anand
74
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
1
Enter Roll Name Per:
519
Gaurav
75
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
1
Enter Roll Name Per:
508
Neeeraj
75
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
1
Enter Roll Name Per:
619
Anil
76
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
4
622 ANAND 74
519 GAURAV 75
622 Anand 74
519 Gaurav 75
508 Neeeraj 75
619 Anil 76
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
2
Enter new per & Roll_no :
78
622
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
4
622 ANAND 78
519 GAURAV 75
622 Anand 78
519 Gaurav 75
508 Neeeraj 75
619 Anil 76
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
3
Enter Roll_no :
508
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
4
622 ANAND 78
519 GAURAV 75
622 Anand 78
519 Gaurav 75
619 Anil 76
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
5
Enter Rollno to Search:622
622 ANAND 78
622 Anand 78
1.Insert
2.Modify
3.Delete
4.View all
5.Search
6.Exit
6
***********************************************************************************/
slip 8
sem1:-
Write a program to create the following GUI and apply the changes to the text in
the TextField .
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public FontDemo(){
lblFont = new JLabel("Font");
lblSize = new JLabel("Size");
lblStyle = new JLabel("Style");
for(int i=0;i<fontName.length;i++)
cmbFont.addItem(fontName[i]);
lblFont.setBounds(10,10,70,25);
cmbFont.setBounds(10,40,170,25);
lblSize.setBounds(10,70,70,25);
cmbSize.setBounds(10,100,170,25);
txtMsg.setBounds(10,150,250,30);
lblStyle.setBounds(200,10,70,25);
cbBold.setBounds(200,40,70,25);
cbItalic.setBounds(200,70,70,25);
setTitle("Font Demo");
setLayout(null);
setSize(450,300);
add(lblFont);
add(cmbFont);
add(lblSize);
add(cmbSize);
add(txtMsg);
add(lblStyle);
add(cbBold);
add(cbItalic);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cbBold.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
if(cbBold.isSelected())
valBold = Font.BOLD;
else
valBold = Font.PLAIN;
change();
}
});
cbItalic.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
if(cbItalic.isSelected())
valItalic = Font.ITALIC;
else
valItalic = Font.PLAIN;
change();
}
});
cmbFont.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
change();
}
});
cmbSize.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
change();
}
});
}
Q3. Write a servlet which counts how many times a user has visited a web page. If the
user is visiting the page for the first time, display a welcome message. If the user is re-
visiting the page, display the number of times visited.
--------------------------------------------------------------------------------------------*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class count1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
int j;
Cookie[] c=req.getCookies();
Cookie c1;
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
if(c == null){
c1=new Cookie("count","1");
res.addCookie(c1);
j=1;
pw.println("VIEWING THE PAGE "+j+" TIME ");
}
else{
for(int i=0;i< c.length;i++)
{
if(c[i].getName().equals("count")){
c1=c[i];
j=Integer.parseInt(c1.getValue());
j++;
pw.println("VIEWING THE PAGE FOR :"+j+"time");
Integer o=new Integer(j);
c1.setValue(o.toString());
res.addCookie(c1);
}else{break;}
}
}
}
slip 9
sem1:-
Create an Applet which displays a massage in the center of the screen .The
massage indicates the events taking place on the applet window. Handle events like
mouse click ,mouse moves ,mouse dragged ,mouse pressed .The massage should
update each time an event occurs . The massage should give details of the event
such as which mouse button was pressed (Hint :Use repaint (),Mouse Listener ,
Mouse Motion Listener)
//Demonstrate Mouse Events.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="mouseeventsdemo" height=300 width=300>
</applet>
*/
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
public Calculator(){
btnDigits = new JButton[10];
for(int i=0;i<10;i++){
btnDigits[i] = new JButton(Integer.toString(i));
}
setTitle("Calculator");
setSize(400,400);
add(txtDisplay,"North");
add(panButtons,"Center");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnDot.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s = txtDisplay.getText();
int i = s.indexOf(".");
if(i==-1) s += ".";
txtDisplay.setText(s);
}
});
for(int i=0;i<10;i++){
btnDigits[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s = txtDisplay.getText();
s += ae.getActionCommand();
txtDisplay.setText(s);
}
});
}
for(int i=0;i<4;i++){
btnOp[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
oper = ae.getActionCommand().charAt(0);
a = Float.parseFloat(txtDisplay.getText());
txtDisplay.setText("");
}
});
}
btnEqual.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
b = Float.parseFloat(txtDisplay.getText());
switch(oper)
{
case '+':
ans = a+b;
break;
case '-':
ans = a-b;
break;
case '*':
ans = a*b;
break;
case '/':
ans = a/b;
}
txtDisplay.setText(Float.toString(ans));
}
});
}
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
a. If it is a directory, list the names of text files .Also, display a count showing
class set5a1
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String dirname=args[0],ext;
int ch,i,cnt=0;
File f1=new File(dirname);
ext="txt";
if(f1.isFile())
{
System.out.println(f1+" is a File\n");
System.out.println("Path : "+f1.getPath());
System.out.println("File Size : "+f1.length()+" bytes\n");
}
else if(f1.isDirectory())
{
System.out.println(args[0]+" Is a Directory\n");
System.out.println("Contents Of : "+dirname);
String s[]=f1.list();
for(i=0;i<s.length;i++)
{
File f=new File(dirname,s[i]);
if(f.isFile())
{
cnt++;
System.out.println(s[i]+" is a File\n");
}
else
System.out.println(s[i]+" is a Directory\n");
}
Contents Of : Diya
abc.txt is a File
def.txt is a File
soyal is a Directory
Contents Of : Diya
abc.txt is a File
def.txt is a File
soyal is a Directory
Contents Of : Diya
soyal is a Directory
sem 2
Define a thread called “Print Text Thread” for printing text on command prompt
for ‘n’ number of times. Create three threads and run them. Pass the text and ‘n’ as
parameters to the thread constructor.
Example:
import java.io.*;
import java.lang.String.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public seta3(String s)
{
super(s);
b1=new ButtonGroup();
p1=new JPanel();
p2=new JPanel();
b=new JButton("Clear");
b.addActionListener(this);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
b1.add(r1);
b1.add(r2);
b1.add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
p1.setLayout(new GridLayout(5,2));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(l3);
p1.add(r1);p1.add(c1);
p1.add(r2); p1.add(c2);
p1.add(r3);p1.add(c3);
p2.setLayout(new FlowLayout());
p2.add(b);
p2.add(t2);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==r1)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
s1.append(" Class = FY");
}
else if(e.getSource()==r2)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
s1.append(" Class = SY");
}
else if(e.getSource()==r3)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
s1.append(" Class = TY");
}
else if(e.getSource()==c1)
{
s1.append(" Hobbies = Music");
}
else if(e.getSource()==c2)
{
s1.append(" Hobbies = Dance");
}
else if(e.getSource()==c3)
{
s1.append(" Hobbies = Sports");
}
t2.setText(new String(s1));
// t2.setText(s2);
if(e.getSource()==b)
{
t2.setText(" ");
t1.setText(" ");
}
}
public static void main(String arg[])
{
seta3 s=new seta3("Profile");
s.setSize(400,200);
s.setVisible(true);
s.setLocation(400,400);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
sem 2:-
Write a program to calculate the sum and average of an array of 1000 integers
(Generated randomly) using 10 threads. Each thread calculates the sum of 100
integers.Use these values to calculate average. [Use join method]
class SumThread extends Thread{
private int x[],start,end,sum;
class ThreadEx5{
public static void main(String args[]){
int nos[] = new int[100];
for(int i=0;i<100;i++)
nos[i] = i+1;
for(int i=0;i<5;i++)
{
t[i] = new SumThread(nos,i*20,(i+1)*20-1);
t[i].start();
}
for(int i=0;i<5;i++)
{
try{
t[i].join();
}
catch(Exception e){}
}
float avg=0;
for(int i=0;i<5;i++){
avg+=t[i].getSum();
}
System.out.println("Sum:"+avg+
"\nAvg:"+(avg/100));
}
}
slip 13
sem1-
Write a program to accept a string as command line argument and check
class set5a1
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String dirname=args[0],ext;
int ch,i,cnt=0;
File f1=new File(dirname);
ext="txt";
if(f1.isFile())
{
System.out.println(f1+" is a File\n");
System.out.println("Path : "+f1.getPath());
System.out.println("File Size : "+f1.length()+" bytes\n");
}
else if(f1.isDirectory())
{
System.out.println(args[0]+" Is a Directory\n");
System.out.println("Contents Of : "+dirname);
String s[]=f1.list();
for(i=0;i<s.length;i++)
{
File f=new File(dirname,s[i]);
if(f.isFile())
{
cnt++;
System.out.println(s[i]+" is a File\n");
}
else
System.out.println(s[i]+" is a Directory\n");
}
Contents Of : Diya
abc.txt is a File
def.txt is a File
soyal is a Directory
Contents Of : Diya
abc.txt is a File
def.txt is a File
soyal is a Directory
Contents Of : Diya
soyal is a Directory
sem2
Create a table student with fields roll number, name, percentage using postgresql.
Insert values in the table .Display all the details of the student table in a tabular
format on the screen. (Using Swing)
/*************************************************************
Statement smt=con.createStatement();
System.out.print(rs.getString("sname")+"\t");
System.out.print(rs.getInt("per")+"\n");
}
con.close();
}catch(Exception e){}
}
}
/******output******************
34 anand 80
35 anil 90
36 sanket 99
37 swapnil 100
/******************************/
slip 14
sem 1-
Write a menu driven program to perform the following operations on a text file
“phone .txt” Which contains name and phone number pairs. The menu should have
options:
class Phone
{
public static void main(String[] args)throws IOException
{
Scanner s=new Scanner(System.in);
RandomAccessFile raf=new RandomAccessFile("phone.txt","rw");
boolean found=false;
sem 2
Construct a Linked List containing name of colors: red, blue, yellow and
class collection2
{
public static void main(String[] args)
{
LinkedList ll=new LinkedList();
ll.add("Red");
ll.add("Blue");
ll.add("Yellow");
ll.add("Orange");
Iterator i=ll.iterator();
System.out.println("contents of the List using an Iterator:");
while(i.hasNext())
{
String s=(String)i.next();
System.out.println(s);
}
ListIterator litr = ll.listIterator();
while(litr.hasNext())
{
String elt = (String)litr.next();
}
System.out.println("contents of the List in reverse order using a ListIterator : ");
while(litr.hasPrevious())
{
System.out.println(litr.previous());
}
ll.add(2,"Pink");
ll.add(3,"Green");
System.out.println("list between blue and yellow is:");
System.out.println(ll);
}
}
/*---------------------output-----------
[root@localhost ~]# java collection2
contents of the List using an Iterator:
Red
Blue
Yellow
Orange
contents of the List in reverse order using a ListIterator :
Orange
Yellow
Blue
Red
list between blue and yellow is:
[Red, Blue, Pink, Green, Yellow, Orange]
-----------------------------------------------------*/
slip 15
sem 1-
Write the program to read item information (id, name, price, qty)from the file
“item.dat’. Write a menu driven program to perform the following operations using
Random access file:
class ItemInfo
{
int id,quantity;
String name;
long price;
ItemInfo()
{
}
sid+=id;
sprice+=price;
squantity+=quantity;
long pos=f.getFilePointer();
f.writeUTF(sid);
f.writeUTF(name);
f.writeUTF(sprice);
f.writeUTF(squantity);
return pos;
}
System.out.println(toString());
}
class set5b1
{
public static void main(String args[])throws Exception
{
RandomAccessFile f=new RandomAccessFile("Item.dat","rw");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ItemInfo I;
long max=0,total=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter Details");
I=new ItemInfo(id,name,price,quantity);
nameTable[i]=name;
costTable[i]=(price*quantity);
posTable[i]=I.store(f);
}
f.close();
boolean done=false;
while(!done)
{
System.out.println("1.Search by name");
System.out.println("2.Find costliest item");
System.out.println("3.Display all item and total cost");
System.out.println("Enter choice");
int choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1 :
System.out.println("Enter Name to search :");
String name=br.readLine();
boolean found=false;
for(int i=0;i<n;i++)
{
if(nameTable[i].equals(name))
{
found=true;
f1.seek(posTable[i]);
I1.access(f1);
}
}
if(!found)
System.out.println("Sorry , record not found . . . ");
break;
case 2 :
for(int i=0;i<n;i++)
{
if(costTable[i] > max)
max=costTable[i];
}
for(int i=0;i<n;i++)
{
if(costTable[i]==max)
{
f1.seek(posTable[i]);
I1.access(f1);
}
}
break;
case 3 :
for(int i=0;i<n;i++)
{
f1.seek(posTable[i]);
I1.access(f1);
total+=costTable[i];
}
System.out.println("\t\t\t\tTatal Amount : "+total);
break;
}
System.out.println("Do you want to continue (Y/N) :");
String ans=br.readLine();
if(ans.equals("N"))
done=true;
}
}
}
/*-----------------------------------------------------------------------------------
output =
[user@localhost ~]$ javac set5b1.java
[user@localhost ~]$ java set5b1
How many records :
2
Enter Details
Enter Item Id :
101
Enter Item Name :
Mango
Enter Item Price :
150
Enter Item Quantity :
12
Enter Details
Enter Item Id :
102
Enter Item Name :
Pinapal
Enter Item Price :
100
Enter Item Quantity :
123
1.Search by name
2.Find costliest item
3.Display all item and total cost
Enter choice
1
Enter Name to search :
Mango
Id = 101 Name= Mango Price= 150 Quantity = 12
Do you want to continue (Y/N) :
Y
1.Search by name
2.Find costliest item
3.Display all item and total cost
Enter choice
1
Enter Name to search :
Banana
Sorry , record not found . . .
Do you want to continue (Y/N) :
Y
1.Search by name
2.Find costliest item
3.Display all item and total cost
Enter choice
2
Id = 102 Name= Pinapal Price= 100 Quantity = 123
Do you want to continue (Y/N) :
Y
1.Search by name
2.Find costliest item
3.Display all item and total cost
Enter choice
3
Id = 101 Name= Mango Price= 150 Quantity = 12
Id = 102 Name= Pinapal Price= 100 Quantity = 123
Tatal Amount : 14100
Do you want to continue (Y/N) :
N
[user@localhost ~]$
*/
sem2
Create a Hash table containing student name and percentage .Display the
Details of the hash table .Also search for a specific and display percentage of
that student.
/*-------------------------------------------------------------------------------------
3. Create a Hash table containing student name and percentage. Display the details of
the hash table. Also search for a specific student and display percentage of that student.
-------------------------------------------------------------------------------------*/
import java.util.*;
class Collection3
{
public static void main(String[] args) throws Exception
{
Scanner s=new Scanner(System.in);
Hashtable ht=new Hashtable();
ht.put("Anand",66.2);
ht.put("Anil",65.7);
ht.put("Shashank",64.8);
Enumeration keys=ht.keys();
Enumeration values=ht.elements();
while(keys. hasMoreElements())
{
while(values. hasMoreElements())
{
System.out.println("student name:"+keys.nextElement()+"\tpercentage:"+values.nextElement());
}
}
System.out.println("enter specific name:");
String str=s.next();
System.out.println("specific student name is "+str+"& percentage is:"+ht.get(str));
}
}
/*-----------------output---------------------------
[root@localhost ~]# java collection3
student name:Anand percentage:66.2
student name:Shashank percentage:64.8
student name:Anil percentage:65.7
specific student name is Anand & percentage is:66.2
---------------------------------------------------------*/
slip 16
sem1
Define a class cricket player (name, no_ of_ innings, no_ of_ times_ not out, total
_runs, bat_ avg ).Create an array of “n” player object. Calculate the batting average
for each player using a static method avg (). Handle appropriate exception while
calculating average .Define static method “short player” Which short array on the
basis of average .Display the player detail in sorted order.
import java.util.*;
import java.io.*;
class CricketPlayer
{
String name;
int inn;
int notout;
int trun;
static int batavg1;
int batavg;
}
public String toString()
{
return("name="+name+"\ninn="+inn+"\nnot out="+notout+"\ntrun="+trun+"\nbatavg="+batavg);
}
static void sort(CricketPlayer[] c)
{
int temp ,i; //= new CricketPlayer();
for(i=0;i<c.length;i++)
{
for(int j=0;j<c.length-1;j++)
{
if(c[j].batavg > c[j+1].batavg)
{
temp = c[j].batavg;
c[j].batavg = c[j+1].batavg;
c[j+1].batavg = temp;
}
}
}
for(i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
}
}
public class CricketPlay
{
public static void main(String[] args)
{
int n,i;
Scanner s =new Scanner(System.in);
System.out.println("Enter Size");
n = s.nextInt();
String name;
int inn,notout,trun,batavg;
batavg = CricketPlayer.avg(inn,notout,trun);
sem2:-
Create a application to store city names and there STD coded using an
Appropriate collection. Display the GUI should allow the following operations:
import java.util.*;
import java.lang.*;
import java.io.*;
class Collection4
{
public static void main(String[] args) throws Exception
{
//Scanner s=new Scanner(System.in);
int op1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Hashtable ht=new Hashtable();
ht.put("Pune",6622);
ht.put("Mumbai",6577);
ht.put("Delhi",6488);
ht.put("chennai",6699);
do
{
System.out.println("1.add city");
System.out.println("2.remove city");
System.out.println("3.search city");
System.out.println("4.display");
System.out.println("5.exit");
case 4:System.out.println("********CITY-CODE********");
Enumeration keys=ht.keys();
Enumeration values=ht.elements();
while(keys.hasMoreElements())
{
while(values.hasMoreElements())
{
System.out.println("city
name:"+keys.nextElement());
System.out.println("city
code:"+values.nextElement());
}
}
break;
}
System.out.println("enter 1 to continue:");
op1=Integer.parseInt(br.readLine());
}while(op1==1);
}}
slip 17
sem 1
Define an abstract class “Staff” with member name and address .Define two sub-
class of this class - “Fulltimestaff” or (department, salary) and part time
staff (number _of_ hours ,rate _per_ hour).Define appropriate constructors.
FullTimeStaff()
{
this.dept=dept;
this.sal=sal;
}
void display()
{
System.out.println(" Name :: "+name);
System.out.println(" Address :: "+addr);
System.out.println("Department :: "+dept);
System.out.println("Salary :: "+sal);
}
}
PartTimeStaff()
{
this.hrs=hrs;
this.rate=rate;
}
void display()
{
sal1=hrs*rate;
System.out.println(" Name :: "+name);
System.out.println(" Address :: "+addr);
System.out.println("Number of hours worked :: "+hrs);
System.out.println("Salary :: "+sal1);
}
}
class staffDemo
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
word entered by user from the login table you have created. The servlet sends back
an appropriate response.
a1.html
<html>
<head>
<title>1st program</title></head>
<body>
<FORM ACTION="a1.java" method=GET>
<h1>Komal Shinge</h1>
</FORM>
</body>
</html>
a1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
PrintWriter pw = resp.getWriter();
resp.setContentType("text/html");
String u = req.getParameter("uname");
String p = req.getParameter("pwd");
}
catch(Exception e){}
}}
slip 18
sem 1
Define a class my number having one positive integer data member .Write a
difficult constructer to initialize it to 0 and other constructor to initialize it to a
value (Use this ).Write a method is Negative ,isZero, isOdd , isEven. Create an
object in main . Use command line adguments to pass a value to the object and
perform the above test
class MyNumber
{ int n;
MyNumber()
{
n=0;
}
MyNumber(int n)
{
this.n=n;
}
void isNegative()
{
if(n<0)
System.out.println(n+"is negative");
}
void isPositive()
{
if(n>0)
System.out.println(n+"is positive");
}
void isZero()
{
if(n==0)
System.out.println(n+"is zero");
}
void isOdd()
{
if(n%2!=0)
System.out.println(n+"is odd");
}
void isEven()
{
if(n%2==0)
System.out.println(n+"is even");
}
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
MyNumber m=new MyNumber();
m.isNegative();
m.isPositive();
m.isZero();
m.isOdd();
m.isEven();
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
sem1:-
. Create a Applet which display a massage in the center of the screen . the massage
indicated the events taking place on the applet window .Handle various keyboard
related event .The massage should update each time an event occurs. The massage
should give details of the event such as which key was pressed, released, typed etc.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="mouseeventsdemo" height=300 width=300>
</applet>
*/
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
package SY;
public class symark
{
int ct,mt,et;
public symark()
{
ct=0;mt=0;et=0;
}
public symark(int c,int m,int e)
{
this.ct=c;
this.mt=m;
this.et=e;
}
package ty
package TY;
public class tymark
{
int tt,pt;
public tymark()
{
tt=0;pt=0;
}
public tymark(int t,int p)
{
this.tt=t;
this.pt=p;
}
}
prog
import java.io.*;
import SY.symark;
import TY.tymark;
public class set2b2
{
private int rno;
private String name;
private symark sy1;
private tymark ty1;
private String grade;
public set2b2()
{
rno=0;
name="";
sy1=new symark();
ty1=new tymark();
grade="";
}
public set2b2(int r,String na,int ct1,int mt1,int et1,int t1,int p,String g)
{
rno=r;
name=na;
sy1=new symark(ct1,mt1,et1);
ty1=new tymark(t1,p);
grade=g;
}
public void display(int ct1,int mt1,int et1,int t1,int p,String g)
{
System.out.println("roll no\t"+rno);
System.out.println("name\t"+name);
System.out.println("total of computer for sy: "+ct1);
System.out.println("total of maths for sy: "+mt1);
System.out.println("total of electronic for sy: "+et1);
System.out.println("total of theory for ty: "+t1);
System.out.println("total of practicals for ty: "+p);
System.out.println("grade:\n"+g);
}
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
slip 21
sem 1
Define a Student class (roll number, name, percentage).Define a default and
parameterized constructor. Keep a count of objects created. Create objects using
parameterized constructor and display the object count after each object is created.
(Use static member and method). Also display the contents of each object. Modify
program to create “n” objects of the Student class. Accept details for each object.
Define static method “sort Student” which sorts the array on the basis of
percentage.
import java.io.*;
class Student
{
int rollno;
String name;
float perc;
static int count;
Student()//defoult constructor
{
rollno=0;
name=" ";
perc=0.0f;
}
this.rollno=rollno;
this.name=name;
this.perc=perc;
count++;
System.out.println("Object created No ="+count);
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public Numbers()
{
f = new JFrame("Numbers");
sel=1;res=0;
p=new JPanel();
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Compute");
m3=new JMenu("Operation");
m4=new JMenu("Sort");
l=new JLabel("Numbers");
t=new JTextField(20);
b=new ButtonGroup();
r1=new JRadioButtonMenuItem("Ascending");
r1.addItemListener(this);
r2=new JRadioButtonMenuItem("Descending");
r2.addItemListener(this);
b.add(r1);b.add(r2);
String
str[]={"Load","Save","Exit","Sum","Average","Maximum","Minimum","Median","Search"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
{
m[i]=new JMenuItem(str[i]);
m[i].addActionListener(this);
}
f.setLayout(new BorderLayout());
p.add(l);
p.add(t);
f.add(p,BorderLayout.CENTER);
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(m[0]);
m1.add(m[1]);
m1.addSeparator();
m1.add(m[2]);
m2.add(m[3]);
m2.add(m[4]);
m2.add(m[5]);
m2.add(m[6]);
m2.add(m[7]);
m3.add(m[8]);
m3.add(m4);
m4.add(r1);
m4.add(r2);
f.add(mb,BorderLayout.NORTH);
f.setSize(300,200);
f.setVisible(true);
f.setLocation(500,200);
f.setDefaultCloseOperation(3);
else if(r2.isSelected())
{
sortdesc();
}
t.setText(new String(s5));
char ch;
String sss = t.getText();
try{
FileOutputStream br1 = new FileOutputStream("number.txt");
for(int i=0;i<sss.length();i++)
{
ch=sss.charAt(i);
if(ch == ' ')
br1.write('\n');
else
br1.write(ch);
}
br1.close();
}
catch(Exception eee){ }
}
else if(s.equals("Search"))
{
int ser=Integer.parseInt(JOptionPane.showInputDialog("Enter Number To Search"));
int a=0;
for(int i=0;i<n;i++)
{
if(arr[i]==ser)
{
t.setText("Number Present");
a=1;
}
if(a==0)
t.setText("Number Not Present");
}
}
}
/*Output
[student@localhost ~]$ javac Numbers.java
[student@localhost ~]$ java Numbers*/
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
slip 23
sem 1
Create an interface “CrediteCardInterface” with methods: view credit amount (),
use Card(),pay Card (),and increaseLimit(). Create a class “SilverCardCustemer”
(name, card number (16 digit),credit amount –initialized to 0 ,credit Limit –set to
50000 ) which implements above interface . Inherit class GoldCardCustmer from
SilverCardCustemer having same methods but credit Limit of 100000 .Create an
object of each class and perform operations .Display appropriate a massages for
success or failure of transaction (Use method overriding)
Credit Limit.
c. Increase Limit () increases the credit Limit for a Gold Card Customer (only
3
import java.util.*;
interface CreditCardInterface
{
public void viewAmount();
public void useCard();
public void payCredit();
public void increaseLimit();
SilverCardCustomer()
{
}
SilverCardCustomer(String n,long cnumber)
{
name=n;
cardnumber=cnumber;
}
}
else
{
creditAmount=creditAmount+amt;
System.out.println("Your Credit Card Amount is :: "+creditAmount);
}
GoldCardCustomer()
{
}
GoldCardCustomer(String n,long cnumber)
{
name=n;
cardnumber=cnumber;
}
}
else
{
creditAmount=creditAmount+amt;
System.out.println("Your Credit Card Amount is :: "+creditAmount);
}
class CreditCard
{
public static void main(String[] args)
{
int op,ch;
Scanner s=new Scanner(System.in);
op=s.nextInt();
switch(op)
{
case 1:
scc.viewAmount();
break;
case 2:
scc.useCard();
break;
case 3:
scc.payCredit();
break;
case 4:
scc.increaseLimit();
break;
}
System.out.println("Enter 1 to continue...");
ch=s.nextInt();
}while(ch==1);
do
{
System.out.println("For Gold card Customers :-\n");
System.out.println("Enter\n1.View Balance\n2.To use card\n3.To Pay amount
\n4.Increase credit card limit\n");
op=s.nextInt();
switch(op)
{
case 1:
gcc.viewAmount();
break;
case 2:
gcc.useCard();
break;
case 3:
gcc.payCredit();
break;
case 4:
gcc.increaseLimit();
break;
}
System.out.println("Enter 1 to continue...");
ch=s.nextInt();
}while(ch==1);
}
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
class Vehicle
{
String company;
double price;
Scanner s=new Scanner(System.in);
void accept()
{
System.out.println("Enter Company Name :: ");
company=s.next();
System.out.println("Enter Price :: ");
price=s.nextDouble();
}
void display()
{
System.out.println("Company Name ::"+company);
System.out.println("Price ::"+price);
}
void accept()
{
super.accept();
System.out.println("Enter Mileage :: ");
mileage=s.nextDouble();
}
void display()
{
System.out.println("For Light Motor Vehicle :: ");
super.display();
System.out.println("Mileage ::"+mileage);
}
void accept()
{
super.accept();
System.out.println("Enter Capacity in tons :: ");
capacity=s.nextDouble();
}
void display()
{
System.out.println("For Heavy Motor Vehicle :: ");
super.display();
System.out.println("Capacity in tons :: "+capacity);
}
class vehicleDemo
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter how many objects to be created :: ");
int n=s.nextInt();
int lcnt=0;
int hcnt=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter \n 1.Light Motor Vehicle\n 2.Heavy Motor Vehicle");
int ch=s.nextInt();
if(ch==1)
{
l[lcnt]=new LightMotorVehicle();
l[lcnt].accept();
lcnt++;
}
else if(ch==2)
{
h[hcnt]=new HeavyMotorVehicle();
h[hcnt].accept();
hcnt++;
}
}
for(int i=0;i<lcnt;i++)
{
l[i].display();
}
for(int i=0;i<hcnt;i++)
{
h[i].display();
}
}
}
sem 2
Accept “n” integers from the user and store them in a collection .Display them in
the sorted order .The collection should not accept duplicate elements (Use suitable
collection ).search for a particular element using predefined search method in the
collection framework .
/*---------------------------------------------------------------------------------------
1. Accept n integers from the user and store them in a collection. Display them in the
sorted order. The collection should not accept duplicate elements. (Use a suitable
collection).
------------------------------------------------------------------------------------------*/
import java.util.*;
import java.io.*;
class collection1
{
public static void main(String[] args) throws Exception
{
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ArrayList al=new ArrayList();
System.out.println("Enter the no for how many element to have:");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
System.out.println("Enter the elements in position:"+i);
ele=Integer.parseInt(br.readLine());
al.add(ele);
}
class Emp
{
private int id;
private String ename,dept;
double salary;
Emp()
{
}
Emp(int id,String ename,String dept,double salary)
{
this.id=id;
this.ename=ename;
this.dept=dept;
this.salary=salary;
}
void accept()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Employee id : ");
id=s.nextInt();
void display()
{
System.out.println(" Employee id : "+id);
System.out.println(" Employee name : "+ename);
System.out.println(" Employee department : "+dept);
System.out.println(" Employee salary : "+salary);
}
}
class Manager extends Emp
{
private double bonus;
double totalsal;
void accept()
{
super.accept();
System.out.println("Enter the bonus amount : ");
Scanner s=new Scanner(System.in);
bonus=s.nextDouble();
totalsal=salary+bonus;
}
void display()
{
System.out.println("***********************************");
super.display();
System.out.println("The total salary amount = "+totalsal);
System.out.println("***********************************");
}
class EmpInfo
{
public static void main(String[] args)
{
Manager m[]=new Manager[3];
Scanner s=new Scanner(System.in);
System.out.println("Enter number of Employee");
int n=s.nextInt();
System.out.println("Enter information of "+n+" Employees");
for(int i=0;i<m.length;i++)
{
m[i]=new Manager();
m[i].accept();
}
Manager.sort(m);
for(int i=0;i<m.length;i++)
{
m[i].display();
}
System.out.println("Maximum salary = ");
m[0].display();
}
}
sem 2
/*
1.1. Write a program to create a shopping mall. User must be allowed to do purchase
from two pages. Each page should have a page total. The third page should display
a bill, which consists of a page total of what ever the purchase has been done and
print the total. (Use HttpSession)
*/
->first.html
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="Shop1" action="get">
<h3>Plz, Fill the following Cutomer Information </h3><br>
Name<input type="text" name="cname"> <br>
Address<input type="text" name="cadd"><br>
Mobile No<input type="text" name="cmob"><br>
<hr>
<h3> ****** Product List PAGE 1*****</h3>
<input type="checkbox" name="page1" value="300"> C programming <br>
<input type="checkbox" name="page1" value="400"> C++ programming<br>
<input type="checkbox" name="page1" value="700"> JAVA programming<br>
<input type="checkbox" name="page1" value="300"> Data Structure<br>
<input type="submit" value="Next">
</form>
</body>
</html>
->shop1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public Shop1() {
super();
// TODO Auto-generated constructor stub
}
HttpSession hs=request.getSession(true);
hs.setAttribute("cname",cname);
hs.setAttribute("cadd",cadd);
hs.setAttribute("cmob",cmob);
hs.setAttribute("page1",page1);
pw.println("<html>");
pw.println("<body>");
pw.println("<form action=Shop2 method=post>");
pw.println("<h3> ****** Products List Page2 *********</h3>");
pw.println("<input type=checkbox name=page2 value=200> PHP <br>");
pw.println("<input type=checkbox name=page2 value=500> HTML <br>");
pw.println("<input type=checkbox name=page2 value=600> PL/SQL <br>");
pw.println("<input type=checkbox name=page2 value=700> Advance JAVA <br>");
pw.println("<input type=submit name=Submit value= Next <br>");
pw.println("</form>");
pw.println("</body></html>");
}
->shop2.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public Shop2() {
super();
// TODO Auto-generated constructor stub
}
double page1total=0,page2total=0;
for(int i=0;i<page1.length;i++)
page1total=page1total+Double.parseDouble(page1[i]);
for(int i=0;i<page2.length;i++)
page2total=page2total+Double.parseDouble(page2[i]);
double total=0;
total=page1total+page2total;
pw.println("<html>");
pw.println("<body>");
pw.println("<h2>Customer Information</h2>");
pw.println("<h3>Customer Name:"+cname);
pw.println("<h3>Customer Address:"+cadd);
pw.println("<h3>Customer Phone No:"+cmob);
pw.println("<h3>Page 1 Total:"+page1total);
pw.println("<h3>Page 2 Total:"+page2total);
pw.println("<h3>Total Amount:"+total);
pw.println("</body>");
pw.println("</html>");
}
}