Monday, 3 November 2014

Create an applet which displays a message in the center of the screen. The message indicates the events taking place on the applet window. Handle events like mouse click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message should update each time an event occurs. The message should give details of the event such as which mouse button was pressed, which key is pressed etc.


/*

Name : Shreyal Mandot

Title : Assignment 8b1. Create an applet which displays a message in the center of the screen. The message indicates the events taking place on the applet window. Handle events like mouse click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message should update each time an event occurs. The message should give details of the event such as which mouse button was pressed, which key is pressed etc.

*/

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

/*
<applet code="ass8b1.class" width=400 height=200>

</applet>
*/

public class ass8b1 extends Applet implements MouseMotionListener,MouseListener,KeyListener
{

String msg="";

public void init()
{

setBackground(Color.cyan);
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);

}

public void paint(Graphics g)
{

g.drawString(msg,10,10);

}

public void mouseDragged(MouseEvent e)
{

msg="Mouse Dragged.";
repaint();

}

public void mouseMoved(MouseEvent e)
{

msg="Mouse Moved.";
repaint();

}

public void mouseClicked(MouseEvent e)
{

msg="Mouse Button "+e.getButton()+"clicked.";
repaint();

}

public void mousePressed(MouseEvent e)
{

msg="Mouse Button "+e.getButton()+"pressed.";
repaint();

}

public void mouseReleased(MouseEvent e)
{

msg="Mouse Button Released.";
repaint();

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public void keyTyped(KeyEvent e)
{

msg="Key Typed "+ e.getKeyChar();
repaint();

}

public void keyPressed(KeyEvent e)
{

msg="Key pressed "+ e.getKeyChar();
repaint();

}

public void keyReleased(KeyEvent e)
{

}

}

Create a conversion applet which accepts value in one unit and converts it to another. The input and output unit is selected from a list. Perform conversion to and from feet, inches, Centimeter, Meters and Kilometers.





Name : Shreyal Mandot

Title : Assignmeny 8a3. Create a conversion applet which accepts value in one unit and converts it to another. The input and output unit is selected from a list. Perform conversion to and from feet, inches, Centimeter, Meters and Kilometers.



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

/*
<applet code="ass8a3.class" width=800 height=800>

</applet>
*/

public class ass8a3 extends Applet implements ItemListener
{

int i,o;
double ip,op;
JComboBox j=new JComboBox();
JComboBox j1=new JComboBox();
JTextArea t=new JTextArea("0.0              ");
JTextArea t1=new JTextArea("0.0             ");

public void init()
{

j.addItem(new String("feet"));
j.addItem(new String("inch"));
j.addItem(new String("centimeter"));
j.addItem(new String("meter"));
j.addItem(new String("kilometer"));

j1.addItem(new String("feet"));
j1.addItem(new String("inch"));
j1.addItem(new String("centimeter"));
j1.addItem(new String("meter"));
j1.addItem(new String("kilometer"));

setLayout(new FlowLayout(FlowLayout.LEFT,10,10));

add(new Label(" Input "));add(t);

add(new Label(" Output  "));add(t1);

add(new Label(" Unit "));add(j);

add(new Label(" Unit "));add(j1);

j.addItemListener(this);
j1.addItemListener(this);

}

public void paint(Graphics g)
{



}
public void itemStateChanged(ItemEvent e)
{

i=j.getSelectedIndex();
o=j1.getSelectedIndex();
ip=Double.parseDouble(t.getText());
switch(i)
{

case 0: ip=ip*0.3048;break;
case 1:ip=(ip*2.54)/100;break;
case 2:ip=ip/100;break;
case 4:ip=ip*1000;

}
switch(o)
{

case 0:op=ip*3.2808;break;
case 1:op=ip*39.37;break;
case 2:op=ip*100;break;
case 3:op=ip;break;
case 4:op=ip/1000;

}
String tmp=" "+op;
t1.setText(tmp);

}

}

Create an applet to display coordinates of mouse movements in a text box.




Name : Shreyal Mandot

Title : Assignment 8a2. Create an applet to display coordinates of mouse movements in a text box.



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

/*
<applet code="ass8a2.class" width=400 height=200>

</applet>
*/

public class ass8a2 extends Applet implements MouseMotionListener
{

int x=0,y=0;

public void init()
{

setBackground(Color.orange);
addMouseMotionListener(this);

}

public void paint(Graphics g)
{

String msg="X: "+x+" Y: "+y;
g.drawString(msg,10,10);

}
public void mouseDragged(MouseEvent e)
{



}
public void mouseMoved(MouseEvent e)
{

x=e.getX();
y=e.getY();
repaint();

}

}

Create an applet to display a message at the center of the applet. The message is passed as a parameter to the applet.




Name : Shreyal Mandot

Title : Assignment 8a1. Create an applet to display a message at the center of the applet. The message is passed as a parameter to the applet.




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


/*
<applet code="ass8a1.class" width=400 height=200>
</applet>
*/

public class ass8a1 extends Applet
{

String msg;

public void init()
{

Font f=new Font("big",Font.BOLD|Font.ITALIC,24);
setFont(f);
setBackground(Color.orange);
msg=getParameter("msg");

if(msg == null)
{

}

msg="No One Msg Passed";

}

public void paint(Graphics g)
{

int x,y;
Dimension d;

d=this.getSize();

FontMetrics f=g.getFontMetrics();

x=(d.width-f.stringWidth(msg))/2;

y=(d.height-f.getHeight())/2;

g.drawString(msg,x,y);

}

}

Write a program to implement a simple calculator. Display appropriate error messages in a dialog box.(Use the screen designed in Assignment 6b1.)




Name : Shreyal Mandot

Title : Assignment 7b1. Write a program to implement a simple calculator. Display appropriate error messages in a dialog box.(Use the screen designed in Assignment 6b1.)



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

class ass7b1 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 ass7b1(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("/"))
                {
             
                try
                {
                                     
                    ch=s.charAt(0);
                        n1=Double.parseDouble(new String(s1));
                        s1.replace(0,s1.length()," ");
                                     
                    }
                                   
                    catch(NumberFormatException ae)
                    {
                                     
                    JOptionPane.showMessageDialog (null,"Invalid","ERROR",JOptionPane.ERROR_MESSAGE);
                                   
                    }
                             
}
                             
                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[])
        {
             
        ass7b1 c =new ass7b1("My Calculator");
            c.setSize(300,300);
            c.setVisible(true);
            c.setLocation(500,200);
            c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }

}

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 checkboxes. Display the selected options in a text box.(Use the screen designed in Assignment 6a2.)




Name : Shreyal Mandot

Title : assignment 7a3. 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 checkboxes. Display the selected options in a text box.(Use the screen designed in Assignment 6a2.)



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

class ass7a3 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 ass7a3(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(30);
        t2=new JTextField(45);

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

        String s2=new String(s1);
        t2.setText(s2);

        if(e.getSource()==b)
        {
     
        t2.setText(" ");
            t1.setText(" ");
             
        }

}

    public static void main(String arg[])
    {
             
    ass7a3 s=new ass7a3("Profile");
             
        s.setSize(600,200);
        s.setVisible(true);
        s.setLocation(400,400);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}


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 6a1.)





Name : Shreyal Mandot

Title : Assignment 7a2. 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 6a1.)




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

class ass7a2 extends JFrame implements ActionListener
{

private JTextField t1;
private JPasswordField t2;
private JButton b1,b2;
private JLabel l1,l2;

int n;

    public ass7a2(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("admin") && s1.equals("admin"))
            {
         
            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[])
    {
 
    ass7a2 s=new ass7a2("Login Please");
        s.setSize(400,150);
        s.setVisible(true);
        s.setLocation(200,200);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}

Write a java program to select the name of a text file and display it in the text field. Display the content of the file in a text area when the user clicks on View.(Use FileChooser).




Name : Shreyal Mandot

Title : Assignment 7a1. Write a java program to select the name of a text file and display it in the text field. Display the content of the file in a text area when the user clicks on View.(Use FileChooser).



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

class ass7a1 extends JFrame implements ActionListener
{
     
private JLabel l1;
private JButton b1,b2;
    private JTextField t1;
    private JTextArea a1;
    private JPanel p1;
    private JScrollBar s1;
private JFileChooser jc;

    public ass7a1(String s)
    {
 
    super(s);
             
        l1=new JLabel("File Name");
     
        b1=new JButton("Browse");
        b1.addActionListener(this);
     
        b2=new JButton("View");
        b2.addActionListener(this);
             
        jc=new JFileChooser();
     
        t1=new JTextField(20);
     
        a1=new JTextArea(10,20);
     
        p1=new JPanel();
     
        s1=new JScrollBar(JScrollBar.VERTICAL);

        p1.setLayout(new GridLayout(2,2));
        p1.add(l1);p1.add(t1);
            p1.add(b1);p1.add(b2);
            a1.add(s1);

        setLayout(new BorderLayout());
            add(p1,BorderLayout.NORTH);
            add(a1,BorderLayout.CENTER);
            add(s1,BorderLayout.EAST);
     
}

    public void actionPerformed(ActionEvent e)
    {
 
    if(e.getSource() == b1)
        {
     
        JFrame f=new JFrame();
                     
            jc.setCurrentDirectory(new File("."));
            jc.showOpenDialog(f);
                     
            File f1=jc.getSelectedFile();
                     
            String nm=f1.getPath();

t1.setText(f1.getName());
             
        }
             
        else if(e.getSource()== b2)
        {
             
        }
     
}

    public static void main(String arg[])
    {
             
    ass7a1 s=new ass7a1("File Descripter");

        s.setSize(400,200);
        s.setVisible(true);
        s.setLocation(400,400);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}

Write a java program to create the following GUI. Using appropriate layout managers.





Name : Shreyal Mandot

Title : Assignment 6b1. Write a java program to create the following GUI. Using appropriate layout managers.




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

public class ass6b1 extends JFrame
{
 
    private JTextField t1;
    private JButton b[];
    private JPanel p1,p2;
   
    public ass6b1()
{
   
    setTitle("Simple Calculator");
   
    t1=new JTextField(30);
    p1=new JPanel();
    p2=new JPanel();

p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(t1);
 
   b=new JButton[16];
   
p2.setLayout(new GridLayout(4,4,5,5));
String[] str={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
   
      for(int i=0;i<str.length;i++)
      {
     
        b[i]=new JButton(str[i]);
        p2.add(b[i]);
   
      }
   
      this.setLayout(new BorderLayout());
      this.add(p1,BorderLayout.NORTH);
      this.add(p2,BorderLayout.CENTER);
      this.setSize(300,300);
      setVisible(true);
   
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
}
 
public static void main(String[] args)
    {
 
new ass6b1();
 
    }

}

/*

Output-



*/


Write a praogram to create the following GUI screen using appropriate layout managers.





Name : Shreyal Mandot

Title : Assignment 6a3.Write a praogram to create the following GUI screen using appropriate layout managers.




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

public class ass6a3 extends Applet implements ActionListener
{

TextField t1;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
List l1,l2;
Panel p1,p2,p3,p4;

public void init()
{

b0=new Button(">");
b1=new Button(">>");
b2=new Button("<");
b3=new Button("<<");
b4=new Button("Add");
b5=new Button("Remove");
b6=new Button("Clear");
b7=new Button("Add");
b8=new Button("Remove");
b9=new Button("Clear");

t1=new TextField(20);
l1=new List(10);
l2=new List(10);

p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();

p3.add(b0);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p1.add(t1);
p2.add(l1);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p4.add(l2);
p4.add(b7);
p4.add(b8);
p4.add(b9);
add(p1);
add(p2);
add(p3);
add(p4);

b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{

if(ae.getSource()==b4)
{

l1.add(t1.getText());
t1.setText("");
t1.requestFocus();

}

else
if(ae.getSource()==b5)
{

int x[]=l1.getSelectedIndexes();

for(int i=x.length-1;i>=0;i--)
l1.remove(x[i]);

}
else
if(ae.getSource()==b6)
{

l1.clear();

}

if(ae.getSource()==b7)
{

l2.add(t1.getText());
t1.setText("");
t1.requestFocus();

}

else
if(ae.getSource()==b8)
{

int x[]=l2.getSelectedIndexes();

for(int i=x.length-1;i>=0;i--)
l2.remove(x[i]);

}

else
if(ae.getSource()==b9)
{

l2.clear();

}

else
if(ae.getSource()==b0)
{

String s[]=l1.getSelectedItems();

for(int i=0;i<s.length;i++)
{

l2.add(s[i]);

}

for(int i=s.length-1;i>=0;i--)
{

l1.remove(s[i]);

}

}

else
if(ae.getSource()==b1)
{

String s[]=l1.getItems();

for(int i=0;i<l1.getItemCount();i++)
l2.add(s[i]);

for(int i=l1.getItemCount()-1;i>=0;i--)
l1.remove(i);

}

if(ae.getSource()==b2)
{

String s[]=l2.getSelectedItems();

for(int i=0;i<s.length;i++)
{

l1.add(s[i]);

}

for(int i=s.length-1;i>=0;i--)
{

l2.remove(s[i]);

}

}

else
if(ae.getSource()==b3)
{

String s[]=l2.getItems();

for(int i=0;i<l2.getItemCount();i++)
l1.add(s[i]);

for(int i=(l2.getItemCount()-1);i>=0;i--)
l2.remove(i);

}

}

}

/*

<applet code=ass6a3.class height=400 width=300>
</applet>

*/





Name : Shreyal Mandot

Title : Write a program to create the following GUI screen using appropriate layout managers.



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

class ass6a2 extends JFrame
{

JFrame f;
JLabel l1,l2,l3;
JTextField t1,t2;
JRadioButton r1,r2,r3;
JCheckBox c1,c2,c3;

JPanel p1,p2,p3;

public ass6a2()
{

f=new JFrame("My Frame");

l1=new JLabel(" Your Name ");
l2=new JLabel(" Your Class ");
l3=new JLabel(" Your Hobbies ");

r1=new JRadioButton(" FY ");
r2=new JRadioButton(" SY ");
r3=new JRadioButton(" TY ");


c1=new JCheckBox(" Music ");
c2=new JCheckBox(" Dance ");
c3=new JCheckBox(" Sports ");

t1=new JTextField(20);
t2=new JTextField(40);
t2.setText("Name : ,Class : ,Hobbies : ");

p1=new JPanel();
p2=new JPanel();
p3=new JPanel();

p1.setLayout(new FlowLayout());
p1.add(l1);
p1.add(t1);

p2.setLayout(new GridLayout(4,2));
p2.add(l2);
p2.add(l3);
p2.add(r1);
p2.add(c1);
p2.add(r2);
p2.add(c2);
p2.add(r3);
p2.add(c3);

p3.setLayout(new FlowLayout());
p3.add(t2);

f.setLayout(new BorderLayout());
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);

f.setSize(500,600);
f.setLocation(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[])
{

new ass6a2();

}

}

/*

Output-

[prady@localhost setA]$ javac ass6a2.java
[prady@localhost setA]$ java ass6a2

*/




Name : Shreyal Mandot

Title : Assignment 6a1. Write a java program to create the following GUI. Use appropriate layout managers.




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

class ass6a1 extends JFrame
{

JFrame f;
JLabel l1,l2;
JTextField t1;
JPasswordField t2;
JButton b1,b2;
JPanel p1,p2;

public ass6a1()
{

f=new JFrame("Login Screen");

l1=new JLabel("Login");
l2=new JLabel("Password");

t1=new JTextField(20);
t2=new JPasswordField(20);

b1=new JButton("OK");
b2=new JButton("Cancel");

p1=new JPanel();
p2=new JPanel();

p1.setLayout(new GridLayout(2,2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);

p2.setLayout(new FlowLayout());
p2.add(b1);
p2.add(b2);

f.setLayout(new BorderLayout());
f.add(p1,BorderLayout.CENTER);
f.add(p2,BorderLayout.SOUTH);

f.setSize(300,150);
f.setLocation(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[])
{

new ass6a1();

}

}

/*

Output-

[prady@localhost setA]$ javac ass6a1.java
[prady@localhost setA]$ java ass6a1

*/

Write a programme to store item information (id,name,price,qty) in file "item.dat". Write a menu driven program to perform the following operations: 1:Search for a item by name 2:Find costliest item 3:Display all items and total cost




Name : Shreyal Mandot

Title : Assignment 5b1. Write a programme to store item information (id,name,price,qty) in file "item.dat". Write a menu driven program to perform the following operations:
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost



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

class item
{

String name,id;
int qty;
double price,total;

item(String i,String n,String q,String p)
{

name=n;
id=i;
qty=Integer.parseInt(q);
price=Double.parseDouble(p);
total=qty*price;

}

public String toString()
{

String s=name+" "+id+" "+qty+" "+price+" "+total;
return(s);

}

public static void search(item[] arr,int n)throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();

for(int i=0;i<n;i++)
{

if(arr[i].name.equals(s))
{

System.out.println(arr[i].toString());
return;

}

}

System.out.println("No Records Found");

}

public static void searchc(item[] arr,int n)
{

double max=0;int c=0;
     
        for(int i=0;i<n;i++)
        {
     
        if(arr[i].price > max)
        {
     
        c=i;
     
        }

}
System.out.println(arr[c].toString());

}

}

class ass5b1
{

public static void main(String[] args)throws IOException
{

String s,space=" ";
        int c,i;

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter no. of records");

int n=Integer.parseInt(b.readLine());

System.out.println("Enter Records:\n<id> <name> <price> <qty> :");

FileWriter f=new FileWriter("item.dat");

for(i=0;i<n;i++)
{

s=b.readLine()+"\n";
f.write(s);

}

f.close();

item it[]=new item[20];
FileReader fr=new FileReader("item.dat");
BufferedReader br=new BufferedReader(fr);

for(i=0;i<n;i++)
{

s=br.readLine();
StringTokenizer t=new StringTokenizer(s,space);
            String si=new String(t.nextToken());
            String sn=new String(t.nextToken());
            String sq=new String(t.nextToken());
            String sp=new String(t.nextToken());
it[i]=new item(si,sn,sq,sp);

}

do
{

System.out.println("Menu :\n"+"1:Search for a item by name\n"+"2:Find costliest item\n"+"3:Display all items and total cost\n4:Exit\n"+"Choice :" );

c=Integer.parseInt(b.readLine());

switch (c)
{

case 1:
System.out.println("Enter item name to be searched:");
item.search(it,n); break;

case 2:
System.out.println("Costliest Item :");item.searchc(it,n);break;

case 3:
for(i=0;i<n;i++)
    System.out.println(it[i].toString());
    break;

case 4:
break;
     
        default:
        System.out.println("Invalid Option");

}

}while(c!=4);

}

}

/*

Output-

[prady@localhost setB]$ javac ass5b1.java
[prady@localhost setB]$ java ass5b1
Enter no. of records
3
Enter Records:
<id> <name> <price> <qty> :
1001 Tyres 2000 4
1002 Leather 750 5
1003 Mirrors 190 2
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
1
Enter item name to be searched:
Tyres
Tyres 1001 2000 4.0 8000.0
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
2
Costliest Item :
Mirrors 1003 190 2.0 380.0
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
3
Tyres 1001 2000 4.0 8000.0
Leather 1002 750 5.0 3750.0
Mirrors 1003 190 2.0 380.0
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
4
[prady@localhost setB]$

*/

Write a programme to display contents of a file in reverese order




Name : .Shreyal Mandot

Title : Write a programme to display contents of a file in reverese order



import java.io.*;

class stack
{

char data[];
int top;

stack()
{

top=-1;
data=new char[100];

}

void push(char d)
{

top++;
data[top]=d;

}

char pop()
{

char d=data[top];
top--;
return (d);

}

}


class ass5a3
{

public static void main(String args[])throws IOException
{

File f1;
char ch;int i=0;
f1=new File(args[0]);

if(!f1.exists())
{

System.out.println(args[0]+" Does not Exist ! \n Programme Terminated......... ");
System.exit(0);

}

        FileInputStream f=new FileInputStream(args[0]);
     
stack s=new stack();
     
while(i != -1)
{
     
        i=f.read();
            ch=(char)i;
      s.push(ch);

  }

while(s.top != -1)
System.out.print(s.pop());

}

}

/*

Output-

[prady@localhost setA]$ javac ass5a3.java
[prady@localhost setA]$ java ass5_3 test.txt

0123456789
ZzZzZzZzZzZ
AAAAAAAAAAA
aaaaaaaaaaa
0987654321

[prady@localhost setA]$ java ass5_3 t.txt

t.txt Does not Exist !
Programme Terminated.........

*/