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

Java Programming

Uploaded by

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

Java Programming

Uploaded by

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

ASSIGNMENT

Course : Java and Network Programming.

Course Code: ICE 3211

Submitted by : Prieom Majumder

St Id: 1710477133

Submitted to: Prof. Dr. Dipankar Das.

Problem 1: Write a GUI program to perform the following operations (Use separate button for each operations and
text fields for inputs and output): (a) addition (b) subtraction (b) multiplication (c) division

Answer:

import java.awt.*;

import java.awt.event.*;

public class JavaGUIApp1 extends Frame implements ActionListener {

private Label textfieldlabel_1, textfieldlabel_2;

private TextField textfield1, textfield2, resultField;

private Button addButton, subButton, mulButton, divButton;

JavaGUIApp1() {

textfieldlabel_1 = new Label("Number 1");

textfieldlabel_2 = new Label("Number 2");

textfield1 = new TextField(20);

textfield2 = new TextField(20);

resultField = new TextField(20);

resultField.setEditable(false);

addButton = new Button("Add");

subButton = new Button("Subtract");

mulButton = new Button("Multiply");

divButton = new Button("Division");

addWindowListener(new WindowAdapter() {
@Override

public void windowClosing(WindowEvent e) {

dispose();

});

addButton.addActionListener(this);

subButton.addActionListener(this);

mulButton.addActionListener(this);

divButton.addActionListener(this);

add(textfieldlabel_1);

add(textfield1);

add(textfieldlabel_2);

add(textfield2);

add(addButton);

add(subButton);

add(mulButton);

add(divButton);

add(resultField);

setTitle("My GUI App 1");

setLayout(new FlowLayout());

setSize(300, 400);

setResizable(false);

setVisible(true);

public static void main(String[] args) {

JavaGUIApp1 javaGUIApp1 = new JavaGUIApp1();

}
@Override

public void actionPerformed(ActionEvent event) {

double num1, num2;

String result = "";

try {

Object source = event.getSource();

num1 = Double.parseDouble(textfield1.getText());

num2 = Double.parseDouble(textfield2.getText());

if(source == addButton)

result = Double.toString(num1+num2);

else if(source == subButton)

result = Double.toString(num1-num2);

else if(source == mulButton)

result = Double.toString(num1*num2);

else if(source == divButton)

result = Double.toString(num1/num2);

} catch (Exception e) {

result = "Error!";

resultField.setText(result);

Problem 2: Write a GUI program to perform the Clear and Undo functions of a Text Field (Use separate
button for each operations and Text Fields for inputting text message).

Answer:

import java.awt.*;

import java.awt.event.*;
public class JavaGUIApp2 extends Frame implements ActionListener {

private final Label TextFieldLabel;

private final TextField textfield;

private final Button CLearButton, UndoButton;

private String temp = "";

JavaGUIApp2() {

TextFieldLabel = new Label("Enter Your Text");

textfield = new TextField(20);

CLearButton = new Button("Clear");

UndoButton = new Button("Undo Clear");

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

dispose();

});

CLearButton.addActionListener(this);

UndoButton.addActionListener(this);

add(TextFieldLabel);

add(textfield);

add(CLearButton);

add(UndoButton);

setTitle("My GUI App 2");

setLayout(new FlowLayout());

setSize(300, 400);

setResizable(false);

setVisible(true);
}

public static void main(String[] args) {

new JavaGUIApp2();

@Override

public void actionPerformed(ActionEvent event) {

String text;

try {

Object source = event.getSource();

text = textfield.getText();

if(source == CLearButton && !text.isBlank()) {

temp = text;

textfield.setText("");

} else if(source == UndoButton)

if(!temp.isBlank())

textfield.setText(temp);

} catch (Exception e) {

System.out.println(e);

You might also like