******************************Que.:Ass:7 set:A Q.1***************************
Que)Write a java program to select the name of a text file and display it
in the text field.Display the contents of the file in a text are when
the user clicks on View. (Use FileChooser)
***********************************Java Code***************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class seta1 extends JFrame implements ActionListener
{
private JLabel l1;
private JTextField t1;
private JButton b1,b2;
private JTextArea a1;
private JPanel p1;
private JFileChooser fc;
private Scrollbar sb;
File f1;
String nm=new String();
public seta1(String s)
{
super(s);
l1=new JLabel("File Name");
t1=new JTextField(20);
b1=new JButton("Browse");
b1.addActionListener(this);
b2=new JButton("View");
b2.addActionListener(this);
a1=new JTextArea(5,20);
p1=new JPanel();
sb=new Scrollbar(JScrollBar.VERTICAL);
p1.setLayout(new GridLayout(2,2));
p1.add(l1);p1.add(t1);
p1.add(b1);p1.add(b2);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(a1,BorderLayout.CENTER);
add(sb,BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
JFrame f =new JFrame("Choose File");
fc=new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.showOpenDialog(f);
f1=fc.getSelectedFile();
nm=f1.getPath();
t1.setText(f1.getName());
}
else if(e.getSource()==b2)
{
try
{
a1.setText(" ");
String s=" ";
StringBuffer s2=new StringBuffer();
BufferedReader br =new BufferedReader(new FileReader(f1));
while((s=br.readLine())!=null)
{
s2.append(s);
s2.append("\n");
}
a1.setText(new String(s2));
}
catch(Exception f)
{
a1.setText("Some Problem Opening File");
}
}
}
public static void main(String arg[])
{
seta1 c =new seta1("My File Displayer");
c.setSize(300,300);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*****************************************************************************************
******************************Que.:Ass:7 set:A Q.2***************************
Que)Write a java program to validate user login and password. If they do
not match,display appropriate message in a dialog box. The user is allowed maximum
3 chances.(Use the screen designed in Assignment 6- Set A , 1 )
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class seta2 extends JFrame implements ActionListener
{
private JTextField t1;
private JPasswordField t2;
private JButton b1,b2;
private JLabel l1,l2;
int n;
public seta2(String s)
{
super(s);
n=0;
t1=new JTextField(10);
t2=new JPasswordField(10);
b1=new JButton("Ok");
b1.addActionListener(this);
b2=new JButton("Cancel");
b2.addActionListener(this);
l1=new JLabel("Login Name");
l2=new JLabel("Password");
t2.setEchoChar('*');
setLayout(new GridLayout(3,2));
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
String s=t1.getText();
char c[]=new char[50];
c=t2.getPassword();
String s1=new String(c);
if(s.equals("root") && s1.equals("root"))
{
JOptionPane.showMessageDialog(null,"Success");
n=0;
}
else
{
JOptionPane.showMessageDialog(null,"Fail");
t1.setText(" ");
t2.setText(" ");
n++;
if(n==3)
System.exit(0);
}
}
else if(e.getSource()==b2)
{
System.exit(0);
}
}
public static void main(String arg[])
{
seta2 s=new seta2("Login");
s.setSize(400,150);
s.setVisible(true);
s.setLocation(200,200);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
********************************************************************************
******************************Que.:Ass:7 set:A Q.3***************************
Que)Write a java program to accept user name in a text box. Accept
the class of the user(FY/SY/TY) using radio buttons and the hobbies of the user using
check boxes. Display the selected options in a text box. (Use the screen designed in
Assignment 6 – Set A, 2 )
***********************************Java Code***************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class seta3 extends JFrame implements ActionListener
{
private JLabel l1,l2,l3;
private JButton b;
private JRadioButton r1,r2,r3;
private JCheckBox c1,c2,c3;
private JTextField t1,t2;
private ButtonGroup b1;
private JPanel p1,p2;
private StringBuffer s1=new StringBuffer();
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);
l1=new JLabel("Your Name");
l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20);
t2=new JTextField(30);
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);
}
}
*********************************************************************************
******************************Que.:Ass:7 set:B Q.1***************************
Que)Write a program to implement a simple calculator. Display appropriate
error messages in a dialog box. (Use the screen designed in Assignment 6 – Set B, 1)
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class setb1 extends JFrame implements ActionListener
{
private JPanel p1,p2;
private JTextField t1;
private JButton b[],b1;
StringBuffer s1 = new StringBuffer();
double n1,n2;
char ch;
public setb1(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
t1=new JTextField(20);
b1=new JButton("Reset");
String str[]={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
b=new JButton[str.length];
for(int i=0;i<str.length;i++)
b[i]=new JButton(str[i]);
p1.setLayout(new BorderLayout());
p1.add(t1,BorderLayout.NORTH);
p1.add(b1,BorderLayout.EAST);
p2.setLayout(new GridLayout(4,4));
b1.addActionListener(this);
for(int i=0;i<b.length;i++)
{
p2.add(b[i]);
b[i].addActionListener(this);
}
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
n1=n2=0;
ch=' ';
t1.setText(" ");
}
for(int i=0;i<b.length;i++)
if(e.getSource()==b[i])
{
String s=b[i].getActionCommand();
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
{
ch=s.charAt(0);
n1=Double.parseDouble(new String(s1));
s1.replace(0,s1.length()," ");
}
else if(s.equals("."))
{
s1.append(".");
String s22=new String(s1);
t1.setText(s22);
}
else if(s.equals("="))
{
double res=0;
n2=Double.parseDouble(new String(s1));
if(ch == '+')
res=n1+n2;
else if(ch == '-')
res=n1-n2;
else if(ch == '*')
res=n1*n2;
else if(ch == '/')
res=n1/n2;
t1.setText(new Double(res).toString());
s1.replace(0,s1.length()," ");
n1=res;
res=0;ch=' ';
}
else
{
for(int j=0;j<=b.length;j++)
if(s.equals(new Integer(j).toString()))
{
s1.append(new Integer(j).toString());
String s22=new String(s1);
t1.setText(s22);
}
}
}
}
public static void main(String arg[])
{
setb1 c =new setb1("My Calculator");
c.setSize(300,300);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
******************************************************************************
******************************Que.:Ass:7 set:B Q.2***************************
Que)Write a program to create two lists and transfer elements from one list to another.Multiple selection is allowed. The Add button allows an element to be added and the Remove button allows an element to be removed (Accepted in an input dialog). Do not add duplicate elements. (Use the screen designed in Assignment 6 – Set B, 2)
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class setb2 extends JFrame implements ActionListener
{
private JPanel p1,p2,p3;
private JButton b1,b2,b3,b4,b5,b6,b7,b8;
private JList l1,l2;
private DefaultListModel d1,d2;
public setb2(String s)
{
super(s);
d1=new DefaultListModel();
d2=new DefaultListModel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
b1=new JButton("<");
b1.addActionListener(this);
b2=new JButton("<<");
b2.addActionListener(this);
b3=new JButton(">");
b3.addActionListener(this);
b4=new JButton(">>");
b4.addActionListener(this);
b5=new JButton("Add");
b5.addActionListener(this);
b6=new JButton("Remove");
b6.addActionListener(this);
b7=new JButton("Add");
b7.addActionListener(this);
b8=new JButton("Remove");
b8.addActionListener(this);
l1=new JList(d1);
l2=new JList(d2);
p1.setLayout(new GridLayout(4,1));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p2.setLayout(new GridLayout(1,3));
p2.add(l1);
p2.add(p1);
p2.add(l2);
p3.setLayout(new GridLayout(1,4));
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
setLayout(new BorderLayout());
add(p2,BorderLayout.NORTH);
add(p3,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b5) //add l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Add in List 1");
d1.addElement(s1);
}
if(e.getSource()==b6) //remove l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Remove From List 1");
d1.removeElement(s1);
}
if(e.getSource()==b7) //add l2
{
String s1=JOptionPane.showInputDialog("Enter Element To Add in List 2") ;
d2.addElement(s1);
}
if(e.getSource()==b8) //remove l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Remove From List 2");
d2.removeElement(s1);
}
if(e.getSource()==b1) // <
{
String s1=(String)l2.getSelectedValue();
d1.addElement(s1);
d2.removeElement(s1);
//dat
String ss;int i=0;
while((ss=(String)d1.getElementAt(i++))!=null)
if(!ss.equals(s1))
d1.addElement(s1);
i=0;
//gh
}
if(e.getSource()==b2)// <<
{
Object s2[]=l2.getSelectedValues();
// int ii=0;
for(int i=0;i<s2.length;i++)
{
String s1=new String(s2[i].toString());
d1.addElement(s1);
d2.removeElement(s1);
}
}
if(e.getSource()==b3)// >
{
String s1=new String(l1.getSelectedValue().toString());
d2.addElement(s1);
d1.removeElement(s1);
}
if(e.getSource()==b4)// >>
{
Object s2[]=l1.getSelectedValues();
for(int i=0;i<s2.length;i++)
{
String s1=new String(s2[i].toString());
d2.addElement(s1);
d1.removeElement(s1);
}
}
}
public static void main(String arg[])
{
setb2 c =new setb2("My Transfer Screen");
c.setSize(400,200);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*********************************************************************************
******************************Que.:Ass:7 set:B Q.3***************************
Que)Write a menu driven program to perform the following operations on a set of integers. The Load operation should load the numbers from a file to an array and display the numbers in a panel. Use the file select dialog to choose the name of the file. The save operation should save the contents of the array to the same file. The Compute menu provides various menu options. The result should be displayed in a message box. The Search operation accepts a number from the user in an input dialog and displays the search result in a message dialog. The sort operation sorts the numbers and displays the sorted data in the panel.
***********************************Java Code***************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class setb3 extends JFrame implements ActionListener,ItemListener
{
private JMenu m1,m2,m3,m4;
private JMenuBar mb;
private JMenuItem m[];
private JRadioButtonMenuItem r1,r2;
private ButtonGroup b;
private JLabel l;
private JTextField t;
private JPanel p;
StringBuffer ss=new StringBuffer();
int sel,n;
int arr[]=new int [20];
double res;
public setb3(String s)
{
super(s);
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);
}
p.add(l);p.add(t);
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);
setLayout(new BorderLayout());
add(mb,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent ii)
{
if(r1.isSelected())
{
sortasc();
}
// t.setText(r1.getText());
else if(r2.isSelected())
{
sortdesc();
}
//t.setText(r2.getText());
}
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;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
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;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
t.setText(new String(s5));
}
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"))
{
//if(arr[0]!=0)
char ch;
String sss = t.getText();
try{
FileOutputStream br1 = new FileOutputStream("num.txt");
for(int i=0;i<sss.length();i++)
{
// String sss =new String(new Integer(arr[i]).toString());
ch=sss.charAt(i);
if(ch == ' ')
br1.write('\n');
else
br1.write(ch);
// br1.write(sss);
}
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)];
}
public static void main(String arg[])
{
setb3 c =new setb3("My Computation");
c.setSize(300,150);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
******************************************************************************
Que)Write a java program to select the name of a text file and display it
in the text field.Display the contents of the file in a text are when
the user clicks on View. (Use FileChooser)
***********************************Java Code***************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class seta1 extends JFrame implements ActionListener
{
private JLabel l1;
private JTextField t1;
private JButton b1,b2;
private JTextArea a1;
private JPanel p1;
private JFileChooser fc;
private Scrollbar sb;
File f1;
String nm=new String();
public seta1(String s)
{
super(s);
l1=new JLabel("File Name");
t1=new JTextField(20);
b1=new JButton("Browse");
b1.addActionListener(this);
b2=new JButton("View");
b2.addActionListener(this);
a1=new JTextArea(5,20);
p1=new JPanel();
sb=new Scrollbar(JScrollBar.VERTICAL);
p1.setLayout(new GridLayout(2,2));
p1.add(l1);p1.add(t1);
p1.add(b1);p1.add(b2);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(a1,BorderLayout.CENTER);
add(sb,BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
JFrame f =new JFrame("Choose File");
fc=new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.showOpenDialog(f);
f1=fc.getSelectedFile();
nm=f1.getPath();
t1.setText(f1.getName());
}
else if(e.getSource()==b2)
{
try
{
a1.setText(" ");
String s=" ";
StringBuffer s2=new StringBuffer();
BufferedReader br =new BufferedReader(new FileReader(f1));
while((s=br.readLine())!=null)
{
s2.append(s);
s2.append("\n");
}
a1.setText(new String(s2));
}
catch(Exception f)
{
a1.setText("Some Problem Opening File");
}
}
}
public static void main(String arg[])
{
seta1 c =new seta1("My File Displayer");
c.setSize(300,300);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*****************************************************************************************
******************************Que.:Ass:7 set:A Q.2***************************
Que)Write a java program to validate user login and password. If they do
not match,display appropriate message in a dialog box. The user is allowed maximum
3 chances.(Use the screen designed in Assignment 6- Set A , 1 )
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class seta2 extends JFrame implements ActionListener
{
private JTextField t1;
private JPasswordField t2;
private JButton b1,b2;
private JLabel l1,l2;
int n;
public seta2(String s)
{
super(s);
n=0;
t1=new JTextField(10);
t2=new JPasswordField(10);
b1=new JButton("Ok");
b1.addActionListener(this);
b2=new JButton("Cancel");
b2.addActionListener(this);
l1=new JLabel("Login Name");
l2=new JLabel("Password");
t2.setEchoChar('*');
setLayout(new GridLayout(3,2));
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
String s=t1.getText();
char c[]=new char[50];
c=t2.getPassword();
String s1=new String(c);
if(s.equals("root") && s1.equals("root"))
{
JOptionPane.showMessageDialog(null,"Success");
n=0;
}
else
{
JOptionPane.showMessageDialog(null,"Fail");
t1.setText(" ");
t2.setText(" ");
n++;
if(n==3)
System.exit(0);
}
}
else if(e.getSource()==b2)
{
System.exit(0);
}
}
public static void main(String arg[])
{
seta2 s=new seta2("Login");
s.setSize(400,150);
s.setVisible(true);
s.setLocation(200,200);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
********************************************************************************
******************************Que.:Ass:7 set:A Q.3***************************
Que)Write a java program to accept user name in a text box. Accept
the class of the user(FY/SY/TY) using radio buttons and the hobbies of the user using
check boxes. Display the selected options in a text box. (Use the screen designed in
Assignment 6 – Set A, 2 )
***********************************Java Code***************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class seta3 extends JFrame implements ActionListener
{
private JLabel l1,l2,l3;
private JButton b;
private JRadioButton r1,r2,r3;
private JCheckBox c1,c2,c3;
private JTextField t1,t2;
private ButtonGroup b1;
private JPanel p1,p2;
private StringBuffer s1=new StringBuffer();
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);
l1=new JLabel("Your Name");
l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20);
t2=new JTextField(30);
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);
}
}
*********************************************************************************
******************************Que.:Ass:7 set:B Q.1***************************
Que)Write a program to implement a simple calculator. Display appropriate
error messages in a dialog box. (Use the screen designed in Assignment 6 – Set B, 1)
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class setb1 extends JFrame implements ActionListener
{
private JPanel p1,p2;
private JTextField t1;
private JButton b[],b1;
StringBuffer s1 = new StringBuffer();
double n1,n2;
char ch;
public setb1(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
t1=new JTextField(20);
b1=new JButton("Reset");
String str[]={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
b=new JButton[str.length];
for(int i=0;i<str.length;i++)
b[i]=new JButton(str[i]);
p1.setLayout(new BorderLayout());
p1.add(t1,BorderLayout.NORTH);
p1.add(b1,BorderLayout.EAST);
p2.setLayout(new GridLayout(4,4));
b1.addActionListener(this);
for(int i=0;i<b.length;i++)
{
p2.add(b[i]);
b[i].addActionListener(this);
}
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
n1=n2=0;
ch=' ';
t1.setText(" ");
}
for(int i=0;i<b.length;i++)
if(e.getSource()==b[i])
{
String s=b[i].getActionCommand();
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
{
ch=s.charAt(0);
n1=Double.parseDouble(new String(s1));
s1.replace(0,s1.length()," ");
}
else if(s.equals("."))
{
s1.append(".");
String s22=new String(s1);
t1.setText(s22);
}
else if(s.equals("="))
{
double res=0;
n2=Double.parseDouble(new String(s1));
if(ch == '+')
res=n1+n2;
else if(ch == '-')
res=n1-n2;
else if(ch == '*')
res=n1*n2;
else if(ch == '/')
res=n1/n2;
t1.setText(new Double(res).toString());
s1.replace(0,s1.length()," ");
n1=res;
res=0;ch=' ';
}
else
{
for(int j=0;j<=b.length;j++)
if(s.equals(new Integer(j).toString()))
{
s1.append(new Integer(j).toString());
String s22=new String(s1);
t1.setText(s22);
}
}
}
}
public static void main(String arg[])
{
setb1 c =new setb1("My Calculator");
c.setSize(300,300);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
******************************************************************************
******************************Que.:Ass:7 set:B Q.2***************************
Que)Write a program to create two lists and transfer elements from one list to another.Multiple selection is allowed. The Add button allows an element to be added and the Remove button allows an element to be removed (Accepted in an input dialog). Do not add duplicate elements. (Use the screen designed in Assignment 6 – Set B, 2)
***********************************Java Code***************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class setb2 extends JFrame implements ActionListener
{
private JPanel p1,p2,p3;
private JButton b1,b2,b3,b4,b5,b6,b7,b8;
private JList l1,l2;
private DefaultListModel d1,d2;
public setb2(String s)
{
super(s);
d1=new DefaultListModel();
d2=new DefaultListModel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
b1=new JButton("<");
b1.addActionListener(this);
b2=new JButton("<<");
b2.addActionListener(this);
b3=new JButton(">");
b3.addActionListener(this);
b4=new JButton(">>");
b4.addActionListener(this);
b5=new JButton("Add");
b5.addActionListener(this);
b6=new JButton("Remove");
b6.addActionListener(this);
b7=new JButton("Add");
b7.addActionListener(this);
b8=new JButton("Remove");
b8.addActionListener(this);
l1=new JList(d1);
l2=new JList(d2);
p1.setLayout(new GridLayout(4,1));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p2.setLayout(new GridLayout(1,3));
p2.add(l1);
p2.add(p1);
p2.add(l2);
p3.setLayout(new GridLayout(1,4));
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
setLayout(new BorderLayout());
add(p2,BorderLayout.NORTH);
add(p3,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b5) //add l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Add in List 1");
d1.addElement(s1);
}
if(e.getSource()==b6) //remove l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Remove From List 1");
d1.removeElement(s1);
}
if(e.getSource()==b7) //add l2
{
String s1=JOptionPane.showInputDialog("Enter Element To Add in List 2") ;
d2.addElement(s1);
}
if(e.getSource()==b8) //remove l1
{
String s1=JOptionPane.showInputDialog("Enter Element To Remove From List 2");
d2.removeElement(s1);
}
if(e.getSource()==b1) // <
{
String s1=(String)l2.getSelectedValue();
d1.addElement(s1);
d2.removeElement(s1);
//dat
String ss;int i=0;
while((ss=(String)d1.getElementAt(i++))!=null)
if(!ss.equals(s1))
d1.addElement(s1);
i=0;
//gh
}
if(e.getSource()==b2)// <<
{
Object s2[]=l2.getSelectedValues();
// int ii=0;
for(int i=0;i<s2.length;i++)
{
String s1=new String(s2[i].toString());
d1.addElement(s1);
d2.removeElement(s1);
}
}
if(e.getSource()==b3)// >
{
String s1=new String(l1.getSelectedValue().toString());
d2.addElement(s1);
d1.removeElement(s1);
}
if(e.getSource()==b4)// >>
{
Object s2[]=l1.getSelectedValues();
for(int i=0;i<s2.length;i++)
{
String s1=new String(s2[i].toString());
d2.addElement(s1);
d1.removeElement(s1);
}
}
}
public static void main(String arg[])
{
setb2 c =new setb2("My Transfer Screen");
c.setSize(400,200);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*********************************************************************************
******************************Que.:Ass:7 set:B Q.3***************************
Que)Write a menu driven program to perform the following operations on a set of integers. The Load operation should load the numbers from a file to an array and display the numbers in a panel. Use the file select dialog to choose the name of the file. The save operation should save the contents of the array to the same file. The Compute menu provides various menu options. The result should be displayed in a message box. The Search operation accepts a number from the user in an input dialog and displays the search result in a message dialog. The sort operation sorts the numbers and displays the sorted data in the panel.
***********************************Java Code***************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class setb3 extends JFrame implements ActionListener,ItemListener
{
private JMenu m1,m2,m3,m4;
private JMenuBar mb;
private JMenuItem m[];
private JRadioButtonMenuItem r1,r2;
private ButtonGroup b;
private JLabel l;
private JTextField t;
private JPanel p;
StringBuffer ss=new StringBuffer();
int sel,n;
int arr[]=new int [20];
double res;
public setb3(String s)
{
super(s);
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);
}
p.add(l);p.add(t);
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);
setLayout(new BorderLayout());
add(mb,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent ii)
{
if(r1.isSelected())
{
sortasc();
}
// t.setText(r1.getText());
else if(r2.isSelected())
{
sortdesc();
}
//t.setText(r2.getText());
}
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;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
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;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
t.setText(new String(s5));
}
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"))
{
//if(arr[0]!=0)
char ch;
String sss = t.getText();
try{
FileOutputStream br1 = new FileOutputStream("num.txt");
for(int i=0;i<sss.length();i++)
{
// String sss =new String(new Integer(arr[i]).toString());
ch=sss.charAt(i);
if(ch == ' ')
br1.write('\n');
else
br1.write(ch);
// br1.write(sss);
}
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)];
}
public static void main(String arg[])
{
setb3 c =new setb3("My Computation");
c.setSize(300,150);
c.setVisible(true);
c.setLocation(500,200);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
******************************************************************************
No comments:
Post a Comment