0% found this document useful (0 votes)
10 views

assign 5 java

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

assign 5 java

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Set A)

a) Write a java program that works as a simple calculator.


Use a grid layout to arrange buttons for the digits and for
the +, -, *, % operations. Add a text field to display the
result.

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

class calculator extends JFrame


implements ActionListener {
static JFrame f;
static JTextField l;
String s0, s1, s2;
calculator() {
s0 = s1 = s2 = "";
}
public static void main(String
args[]) {
f = new JFrame("Calculator");
/*try
{

UIManager.setLookAndFeel(UIManager.getSys
temLookAndFeelClassName());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}*/
calculator c = new calculator();
l = new JTextField(16);
l.setEditable(false);
JButton b0, b1, b2, b3, b4, b5,
b6, b7, b8, b9, ba, bs, bm, bd, be, beq,
beq1;

b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");

beq1 = new JButton("=");


beq = new JButton("C");
be = new JButton(".");
ba = new JButton("+");
bs = new JButton("-");
bm = new JButton("*");
bd = new JButton("/");

JPanel p = new JPanel();

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

ba.addActionListener(c);
bs.addActionListener(c);
bm.addActionListener(c);
bd.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);

p.add(l);
p.add(ba);
p.add(bs);
p.add(bm);
p.add(bd);
p.add(be);
p.add(beq);
p.add(beq1);

p.add(b0);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);

f.add(p);
f.setLocation(200, 300);

f.setDefaultCloseOperation(JFrame.EXIT_ON
_CLOSE);
f.setSize(200, 220);
f.show();
}

public void
actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();

if (s.charAt(0) >= '0' &&


s.charAt(0) <= '9' || s.charAt(0) == '.')
{
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

l.setText(s0 + s1 + s2);
} else if (s.charAt(0) == 'C') {
s0 = s1 = s2 = "";
l.setText(s0 + s1 + s2);
} else if (s.charAt(0) == '=') {
double te;

if (s1.equals("+"))
te =
(Double.parseDouble(s0) +
Double.parseDouble(s2));
else if (s1.equals("-"))
te =
(Double.parseDouble(s0) -
Double.parseDouble(s2));
else if (s1.equals("/"))
te =
(Double.parseDouble(s0) /
Double.parseDouble(s2));
else
te =
(Double.parseDouble(s0) *
Double.parseDouble(s2));

l.setText(s0 + s1 + s2 + "="
+ te);

s0 = Double.toString(te);
s1 = s2 = "";
} else {
if (s1.equals("") ||
s2.equals(""))
s1 = s;
else {
double te;

if (s1.equals("+"))
te =
(Double.parseDouble(s0) +
Double.parseDouble(s2));
else if (s1.equals("-"))
te =
(Double.parseDouble(s0) -
Double.parseDouble(s2));
else if (s1.equals("/"))
te =
(Double.parseDouble(s0) /
Double.parseDouble(s2));
else
te =
(Double.parseDouble(s0) *
Double.parseDouble(s2));

s0 = Double.toString(te);
s1 = s;
s2 = "";
}
l.setText(s0 + s1 + s2);
}
}
}

b) Design a screen to handle the Mouse Events such as


MOUSE_MOVED and
MOUSE_CLICK and display the position of the Mouse_Click
in a TextField.

import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame {
TextField t, t1;
Label l, l1;
int x, y;
Panel p;
MyFrame(String title) {
super(title);
setLayout(new FlowLayout());
p = new Panel();
p.setLayout(new GridLayout(2, 2,
5, 5));
t = new TextField(20);
l = new Label("Co-ordinates of
clicking");
l1 = new Label("Co-ordinates of
movement");
t1 = new TextField(20);
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new
MyMove());
addWindowListener(new
WindowAdapter() {
public void
windowClosing(WindowEvent we) {
System.exit(0);
}
}
);
setSize(500, 500);

//setDefaultCloseOperation(Frame.EXIT_ON_
CLOSE);
setVisible(true);
}

class MyClick extends MouseAdapter {


public void
mouseClicked(MouseEvent me) {
x = me.getX();
y = me.getY();
t.setText("X=" + x + "Y=" +
y);
}
}

class MyMove extends


MouseMotionAdapter {
public void mouseMoved(MouseEvent
me) {
x = me.getX();
y = me.getY();
t1.setText("X=" + x + "Y=" +
y);
}
}
}

class main {
public static void main(String
args[]) {
MyFrame f = new MyFrame("Mouse
Events");
}
}

Set B)
a) Create the following GUI screen using appropriate layout
managers. Accept the name,class , hobbies of the user and
apply the changes and display the selected options in a text
box.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class q1 extends JFrame implements


ActionListener {
JLabel l1, l2, l3;
JButton b;
JRadioButton r1, r2, r3;
JCheckBox c1, c2, c3;
JTextField t1, t2;
ButtonGroup b1;
JPanel p1, p2;
static int cnt;
private StringBuffer s1 = new
StringBuffer();
q1() {

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

setSize(400, 200);
setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_C
LOSE);
}

public void
actionPerformed(ActionEvent e) {

if (e.getSource() == r1) {
cnt++;
if (cnt == 1) {
String s = t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = FY");
} else if (e.getSource() == r2) {
cnt++;
if (cnt == 1) {
String s = t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = SY");
} else if (e.getSource() == r3) {
cnt++;
if (cnt == 1) {
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[])


{
q1 s = new q1();

}
}

b) Write a Java program to design a screen using Awt that


will take a user name and password. If the user name and
password are not same, raise an Exception with appropriate
message. User can have 3 login chances only. Use clear
button to clear the TextFields.

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

class InvalidPasswordException extends


Exception {
InvalidPasswordException() {
System.out.println("Username and
password is not same");
}
}

class q2 extends Frame implements


ActionListener {
Label uname, upass;
TextField nametext;
TextField passtext, msg;
Button login, Clear;
Panel p;
int attempt = 0;
char c = '*';

public void login() {


p = new Panel();
uname = new Label("Username : ",
Label.CENTER);
upass = new Label("Password : ",
Label.RIGHT);
nametext = new TextField(20);
passtext = new TextField(20);
passtext.setEditable(true);
passtext.setEchoChar(c);
msg = new TextField(10);
msg.setEditable(false);
login = new Button("Login");
Clear = new Button("Clear");
login.addActionListener(this);
Clear.addActionListener(this);
addWindowListener(new
WindowAdapter() {
public void
windowClosing(WindowEvent we) {
System.exit(0);
}
}
);
p.add(uname);
p.add(nametext);
p.add(upass);
p.add(passtext);
p.add(login);
p.add(Clear);
p.add(msg);
add(p);

setTitle("Login");
setSize(290, 200);
setVisible(true);
setTitle("Password");
setSize(290, 200);
setVisible(true);
}

public void
actionPerformed(ActionEvent ae) {
Button btn = (Button)
(ae.getSource());
if (attempt < 2) {
if (btn == Clear) {
nametext.setText("");
passtext.setText("");
}
if
((btn.getLabel()).equals("Login")) {
try {
String user =
nametext.getText();
String upass =
passtext.getText();

if
(user.compareTo(upass) == 0) {

msg.setText("Valid");

System.out.println("Username is valid");
System.exit(0);
} else {
throw new
InvalidPasswordException();
}
} catch (Exception e) {
msg.setText("Error");
}
attempt++;
}
} else {
System.out.println("Youm are
using 3 attempt");
System.exit(0);
}
}

public static void main(String


args[]) {
q2 pd = new q2();
pd.login();
}
}

You might also like