0% found this document useful (0 votes)
25 views4 pages

OOP Lab Assignment Swing Calculator

Java GUI application study question

Uploaded by

cemmetesaritas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

OOP Lab Assignment Swing Calculator

Java GUI application study question

Uploaded by

cemmetesaritas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 343 Fall 2024 Lab Assignment 2

Building a Simple GUI Application using Swing

Objective
The goal of this lab assignment is to practice Object-Oriented Programming principles by
developing a Java GUI application using Swing components and event handling.

Figure 1: Sample appearance of your program.

Assignment Requirements

1. Create a GUI-Based Calculator Application


Your task is to create a simple calculator (a sample appearance given in Figure 1) that can
perform basic arithmetic operations (addition, subtraction, multiplication, and division)
with the following requirements:

GUI Components to Use:


• JFrame: For the main window.
• JPanel: To organize and arrange components in the window.
• JTextField: To display the input and output.
• JButton: For numbers (0–9), arithmetic operators (+, −, *, /), and special buttons (C for
clear, = for result).
• JLabel: For displaying messages or error hints.

Features of the Calculator:


• Input handling for two numbers and one operator.
• Display the result when the user clicks the “=” button.
• Clear the text fields when the user clicks the “C” button.
• Handle division by zero with an error message.

2. OOP Principles to Implement


• Encapsulation: Use private fields and public getter and setter methods where necessary.
• Classes and Objects: Organize your code by defining a Calculator class for the calculation
logic, and a CalculatorGUI class for the user interface.
• Event Handling: Use ActionListeners to respond to button clicks and process user inputs.

Implementation Steps

1. Create a Calculator Class


Define a Calculator class with methods for each arithmetic operation (add, subtract,
multiply, divide). Implement error handling within the divide method to handle division by
zero. This class should be independent of the GUI and should contain only the logic for
performing calculations.

2. Create a CalculatorGUI Class


In this class, create the main frame and add buttons for numbers, operators, and special
functions. Use a layout manager (such as GridLayout or FlowLayout) to arrange buttons and
text fields. Add event listeners to the buttons to capture user input and perform operations.

3. Implement Event Handlers


Add an ActionListener to each button in the CalculatorGUI class. In the ActionListener
methods, call appropriate methods from the Calculator class. Display the result in the text
field. Display an error message for invalid inputs, such as dividing by zero.

Bonus Tasks (Optional)


1. Error Handling:
- Add validation to ensure users cannot enter invalid characters.
- Display appropriate error messages if the user enters invalid input.

Example Code Snippet


Here’s a starter code snippet to help you get started with the structure:

Grading Criteria
• Correctness: The application performs calculations correctly.
• Usability: The GUI layout is intuitive, and buttons respond as expected.
• Code Organization: Code is modular, following OOP principles.
• Error Handling: Handles division by zero and invalid input.
• Bonus Features: Extra points for implementing the input error handling.

Here is the sample code that you can begin with:


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

public class Calculator {


public double add(double a, double b) {
return a + b;
}
public double subtract(double a, double b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
public double divide(double a, double b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}
}

class CalculatorGUI extends JFrame implements ActionListener {


private JTextField display;
private Calculator calculator;

public CalculatorGUI() {
calculator = new Calculator();
display = new JTextField();

setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();


buttonPanel.setLayout(new GridLayout(4, 4));

// Add buttons 0-9 and operations


String[] buttons = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"};

for (String text : buttons) {


JButton button = new JButton(text);
button.addActionListener(this);
buttonPanel.add(button);
}

add(buttonPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

// Handle operations and display updates here


}

public static void main(String[] args) {


new CalculatorGUI();
}
}

You might also like