Creating a Visual Calculator with Java

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface and write a blog to record
MU STU ID and FZU STU ID21126496 832102220
The Link of Code of this assignment of GitHubhttps://github.com/Tyler-Linchenwei/second

Introduction

In this blog post, we will explore the step-by-step process of building a calculator application in Java. We’ll cover the problem-solving ideas, design and implementation process, code description, result display with screenshots, and a summary of the assignment.

Personal Software Process (PSP) Form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning3060
• Estimate3080
Development120180
• Analysis3020
• Design Spec2535
• Design Review3010
• Coding Standard3025
• Design4560
• Coding100130
• Code Review6050
• Test1010
Reporting100100
• Test Repor2020
• Size Measurement3025
• Postmortem & Process Improvement Plan4040
• Total700845

Ideas for solving problems

When starting this project, the first step was to think about how to approach the problem. The task was to create a visual calculator with a GUI that supports basic arithmetic operations. Here’s the thought process I followed:

1.Identify the programming language and libraries: I chose Java as the programming language and decided to use the Swing library for creating the GUI.

2.Design the GUI layout: I planned to use a JFrame as the main window and organize the components using JPanels and layout managers.

3.Handle user input: I needed to find a way to capture user input from buttons and perform the corresponding operations.

4.Implement the calculation logic: Based on the user input, the calculator should execute the appropriate arithmetic operation and display the result.

Design and Implementation Process

1.Created a class called Calculator that extends JFrame to serve as the main window.

2.Set the window title, size, layout, and close operation in the Calculator class.

3.Created two JPanels: jp_north for the input text field and clear button, and jp_center for the number and operator buttons.

4.Used the GridLayout to arrange the buttons in a 4x4 grid layout.

5.Added action listeners to the buttons to handle user input and perform operations.

6.Implemented the calculation logic in a separate method to execute the arithmetic operations.

7.Displayed the result in the input text field.

Code description

public void actionPerformed(ActionEvent e) {
    String clickStr = e.getActionCommand();
    
    // If the clicked button is a digit or a decimal point, append it to the input text field
    if (".o1234567890".indexOf(clickStr) != -1) {
        this.input_text.setText(input_text.getText() + clickStr);
        this.input_text.setHorizontalAlignment(JTextField.RIGHT);
        //JOptionPane.showMessageDialog(this, clickStr);
    } 
    // If the clicked button is an operator (+, -, *, /), store the operator and clear the input text field
    else if (clickStr.matches("[\\+\\-\\*/]{1}")) {
        operator = clickStr;
        firstInput = this.input_text.getText();
        this.input_text.setText("");
    } 
    // If the clicked button is the equals sign (=), perform the calculation
    else if (clickStr.equals("=")) {
        Double a = Double.valueOf(firstInput);
        Double b = Double.valueOf(this.input_text.getText());
        Double result = null;
        
        // Perform the corresponding arithmetic operation based on the stored operator
        switch (operator) {
            case "+":
                result = a + b;
                break;
            case "-":
                result = a - b;
                break;
            case "*":
                result = a * b;
                break;
            case "/":
                if (b != 0) {
                    result = a / b;
                }
                break;
        }
        
        // Display the calculated result in the input text field
        this.input_text.setText(result.toString());
    }
}

1.The method starts by getting the action command of the clicked button, which is stored in the clickStr variable.

2.If the clickStr is a digit (0-9) or a decimal point (.), it appends the clicked character to the input text field (input_text). It also sets the text alignment of the input field to right.

3.If the clickStr is an operator (+, -, *, /), it stores the operator in the operator variable and saves the current value of the input text field (input_text) as the firstInput. It then clears the input text field.

4.If the clickStr is the equals sign (=), it retrieves the values of firstInput and the current input text field (input_text) as a and b, respectively. It initializes the result variable as null.

5.The code uses a switch statement to perform the corresponding arithmetic operation based on the stored operator. It calculates the result and assigns it to the result variable.

6.Finally, it sets the input text field (input_text) to display the calculated result by converting it to a string using result.toString().

Displaying result functions

Displaying

Summary

In this assignment, I successfully created a visual calculator with a GUI using Java and the Swing library. The project involved designing the GUI layout, handling user input, and implementing the calculation logic. I encountered some challenges along the way, but with careful thinking and debugging, I was able to overcome them and complete the calculator.

This assignment provided a valuable learning experience, allowing me to enhance my Java programming skills and gain practical knowledge in GUI development. I hope this blog post serves as a helpful guide for creating a visual calculator and inspires you to explore more programming projects.

If you have any questions or feedback regarding this project, please feel free to leave a comment.

Thank you for reading this blog post ^ v ^ .

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值