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 [Link].*;
import [Link].*;
import [Link].*;
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
{
[Link]([Link]
temLookAndFeelClassName());
}
catch(Exception e)
{
[Link]([Link]());
}*/
calculator c = new calculator();
l = new JTextField(16);
[Link](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();
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](l);
[Link](ba);
[Link](bs);
[Link](bm);
[Link](bd);
[Link](be);
[Link](beq);
[Link](beq1);
[Link](b0);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](p);
[Link](200, 300);
[Link](JFrame.EXIT_ON
_CLOSE);
[Link](200, 220);
[Link]();
}
public void
actionPerformed(ActionEvent ae) {
String s = [Link]();
if ([Link](0) >= '0' &&
[Link](0) <= '9' || [Link](0) == '.')
{
if ()
s2 = s2 + s;
else
s0 = s0 + s;
[Link](s0 + s1 + s2);
} else if ([Link](0) == 'C') {
s0 = s1 = s2 = "";
[Link](s0 + s1 + s2);
} else if ([Link](0) == '=') {
double te;
if ([Link]("+"))
te =
([Link](s0) +
[Link](s2));
else if ([Link]("-"))
te =
([Link](s0) -
[Link](s2));
else if ([Link]("/"))
te =
([Link](s0) /
[Link](s2));
else
te =
([Link](s0) *
[Link](s2));
[Link](s0 + s1 + s2 + "="
+ te);
s0 = [Link](te);
s1 = s2 = "";
} else {
if ([Link]("") ||
[Link](""))
s1 = s;
else {
double te;
if ([Link]("+"))
te =
([Link](s0) +
[Link](s2));
else if ([Link]("-"))
te =
([Link](s0) -
[Link](s2));
else if ([Link]("/"))
te =
([Link](s0) /
[Link](s2));
else
te =
([Link](s0) *
[Link](s2));
s0 = [Link](te);
s1 = s;
s2 = "";
}
[Link](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 [Link].*;
import [Link].*;
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();
[Link](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);
[Link](l);
[Link](t);
[Link](l1);
[Link](t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new
MyMove());
addWindowListener(new
WindowAdapter() {
public void
windowClosing(WindowEvent we) {
[Link](0);
}
}
);
setSize(500, 500);
//setDefaultCloseOperation(Frame.EXIT_ON_
CLOSE);
setVisible(true);
}
class MyClick extends MouseAdapter {
public void
mouseClicked(MouseEvent me) {
x = [Link]();
y = [Link]();
[Link]("X=" + x + "Y=" +
y);
}
}
class MyMove extends
MouseMotionAdapter {
public void mouseMoved(MouseEvent
me) {
x = [Link]();
y = [Link]();
[Link]("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 [Link].*;
import [Link].*;
import [Link].*;
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");
[Link](this);
r1 = new JRadioButton("FY");
r2 = new JRadioButton("SY");
r3 = new JRadioButton("TY");
[Link](r1);
[Link](r2);
[Link](r3);
[Link](this);
[Link](this);
[Link](this);
c1 = new JCheckBox("Music");
c2 = new JCheckBox("Dance");
c3 = new JCheckBox("Sports");
[Link](this);
[Link](this);
[Link](this);
l1 = new JLabel("Your Name");
l2 = new JLabel("Your Class");
l3 = new JLabel("Your Hobbies");
t1 = new JTextField(20);
t2 = new JTextField(30);
[Link](new GridLayout(5,
2));
[Link](l1);
[Link](t1);
[Link](l2);
[Link](l3);
[Link](r1);
[Link](c1);
[Link](r2);
[Link](c2);
[Link](r3);
[Link](c3);
[Link](new FlowLayout());
[Link](b);
[Link](t2);
setLayout(new BorderLayout());
add(p1, [Link]);
add(p2, [Link]);
setSize(400, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_C
LOSE);
}
public void
actionPerformed(ActionEvent e) {
if ([Link]() == r1) {
cnt++;
if (cnt == 1) {
String s = [Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = FY");
} else if ([Link]() == r2) {
cnt++;
if (cnt == 1) {
String s = [Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = SY");
} else if ([Link]() == r3) {
cnt++;
if (cnt == 1) {
String s = [Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = TY");
}
else if ([Link]() == c1) {
[Link](" Hobbies =
Music");
} else if ([Link]() == c2) {
[Link](" Hobbies =
Dance");
} else if ([Link]() == c3) {
[Link](" Hobbies =
Sports");
}
[Link](new String(s1));
// [Link](s2);
if ([Link]() == b) {
[Link](" ");
[Link](" ");
}
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 [Link].*;
import [Link].*;
import [Link].*;
class InvalidPasswordException extends
Exception {
InvalidPasswordException() {
[Link]("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 : ",
[Link]);
upass = new Label("Password : ",
[Link]);
nametext = new TextField(20);
passtext = new TextField(20);
[Link](true);
[Link](c);
msg = new TextField(10);
[Link](false);
login = new Button("Login");
Clear = new Button("Clear");
[Link](this);
[Link](this);
addWindowListener(new
WindowAdapter() {
public void
windowClosing(WindowEvent we) {
[Link](0);
}
}
);
[Link](uname);
[Link](nametext);
[Link](upass);
[Link](passtext);
[Link](login);
[Link](Clear);
[Link](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)
([Link]());
if (attempt < 2) {
if (btn == Clear) {
[Link]("");
[Link]("");
}
if
(([Link]()).equals("Login")) {
try {
String user =
[Link]();
String upass =
[Link]();
if
([Link](upass) == 0) {
[Link]("Valid");
[Link]("Username is valid");
[Link](0);
} else {
throw new
InvalidPasswordException();
}
} catch (Exception e) {
[Link]("Error");
}
attempt++;
}
} else {
[Link]("Youm are
using 3 attempt");
[Link](0);
}
}
public static void main(String
args[]) {
q2 pd = new q2();
[Link]();
}
}