Anda di halaman 1dari 3

Assignment 1

Due Sunday 10/4/2018 by 5:30 PM


No delay is accepted

Do not delete anything from this file. Copy and paste your code under Answer at the
bottom.

Design a GUI with a frame, three text fields, four radio buttons, and a label (for the equal sign) as follows.
Write a program such that if the user enters two Integers on the text fields and click on any radio buttons
the result of the operation appears in the third text field. Note that there is no command button here (after
entering integer data on text field one and field two, click on the radio button for ‘+’, sum will appear on
text field three. And so for the other operations.)

For the following cases you must display a dialog box with an appropriate message:

1. One or both text fields are empty or contain non numerical strings, and the user clicks on one of the radio
buttons.
2. When division by zero happens.

Hint: You need to convert the content of both text fields to integer by using Integer class. It is at the above
cases an exception must happen (need try-catch block) and your program must issues an error message by a
dialog box.

Answer:

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

public class Calculations extends JFrame implements ActionListener{


JTextField input1 = new JTextField();
JTextField input2 = new JTextField();
JTextField output = new JTextField();

public static void main(String[] args){


Calculations cl = new Calculations();
cl.display();
}

private void display(){


setSize(new Dimension(600,300));
Container c = getContentPane();
c.setLayout(null);

//RadioButtons for additions, subtractions, multiplications and divisions


JRadioButton addition = new JRadioButton("+");
addition.setBounds(150,50,50,30);
addition.addActionListener(this::actionPerformed);

JRadioButton subtraction = new JRadioButton("-");


subtraction.setBounds(150,100,50,30);
subtraction.addActionListener(this::actionPerformed);

JRadioButton multiplication = new JRadioButton("*");


multiplication.setBounds(150,150,50,30);
multiplication.addActionListener(this::actionPerformed);

JRadioButton division = new JRadioButton("/");


division.setBounds(150,200,50,30);
division.addActionListener(this::actionPerformed);

//Grouping All RadioButtons


ButtonGroup group = new ButtonGroup();
group.add(addition);
group.add(subtraction);
group.add(multiplication);
group.add(division);

//Equal sign lable


JLabel equalLabel = new JLabel("=");
equalLabel.setBounds(350, 100,50,50);

//textFields for inputs and outputs


input1.setBounds(10, 100, 120, 50);
input2.setBounds(200, 100, 120, 50);
output.setBounds(400, 100, 120, 50);

//Adding RadioButtons into container


c.add(addition);
c.add(subtraction);
c.add(multiplication);
c.add(division);

//Adding textFields and label into container


c.add(input1);
c.add(input2);
c.add(output);
c.add(equalLabel);

setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent evt){

try {
String a = input1.getText(), b = input2.getText();
float i = Float.parseFloat(a), j = Float.parseFloat(b);

if (evt.getActionCommand().equals("+")) {
output.setText(String.valueOf(i + j));
} else if (evt.getActionCommand().equals("-")) {
output.setText(String.valueOf(i - j));
} else if (evt.getActionCommand().equals("*")) {
output.setText(String.valueOf(i * j));
} else if (evt.getActionCommand().equals("/")) {

if(input2.getText().equals("0")){
JOptionPane.showMessageDialog(null, "Division by zero is not possible");
}
else
output.setText(String.valueOf(i/j));
}
} catch (Exception e){
JOptionPane.showMessageDialog(null, "Enter the values in textfiled first");
}
}
}

Anda mungkin juga menyukai