Question

QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce the GUI below. Use 3 panels to arrange the components. You must use an array when defining the numbered buttons (o-9. Do not declare 10 buttons individually. The textfield must be initialized with the text as shown below, and be able to accommodate up to 25 characters. The spacing between the numbered buttons is 10. (28) for caloula Add Equals 3.2 Rewrite any existing Java code of the class definition in 3.1 which needs to be changed, as well as any additional event-handling code, to display the appropriate numeric value in the textfield when the user cicks on the numbered buttons. If the user clicks on a combination of numbers then the entire multi-digit number is displayed in the textfield 3.3 Now write further Java statements to do the following Once the user has clicked a digit or combination of digits followed by the Add button then the single or multi-digit number must be stored before the textfield is cleared. The user can then click on more digits to make up a second number followed by the Equals button. Ater clicking on the Equais button the sum of the 2 selected numbersis (10) displayed in the textfield. 3.4 Write the Java statements for a class called RunCalculator, in which you declare a main method with Java statements to display the GUI. The dimensions of the frame are width 300 and height 300. Page 7 of 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Frame extends JFrame {

private double tempNumbers1 = 0;

private double tempNumbers2 = 0;

private byte function = -1;

private JTextField resultJText;

public Frame() {

JButton[] numberButtons = new JButton[10];

for ( int i = 9; i >= 0; i--) {

numberButtons[i] = new JButton(Integer.toString(i));

}

JButton enterButton = new JButton("Enter");

JButton cButton = new JButton("C");

JButton multiplyButton = new JButton("*");

JButton divideButton = new JButton("/");

JButton addButton = new JButton("+");

JButton substractButton = new JButton("-");

resultJText = new JTextField();

resultJText.setPreferredSize(new Dimension(160, 20));

resultJText.setBackground(Color.WHITE);

resultJText.setEnabled(false);

resultJText.setHorizontalAlignment(4);

resultJText.setDisabledTextColor(Color.BLACK);

JPanel motherPanel = new JPanel();

motherPanel.setLayout(new BoxLayout(motherPanel, BoxLayout.Y_AXIS));

JPanel textPanel = new JPanel();

textPanel.setPreferredSize(new Dimension(160, 20));

textPanel.add(resultJText);

JPanel numberButtonsPanel = new JPanel();

numberButtonsPanel.setPreferredSize(new Dimension(160, 100));

for(int i = 9; i>=0; i--) {

numberButtonsPanel.add(numberButtons[i]);

}

JPanel functionButtonPanel = new JPanel();

functionButtonPanel.setPreferredSize(new Dimension(160, 35));

functionButtonPanel.add(enterButton);

functionButtonPanel.add(cButton);

functionButtonPanel.add(multiplyButton);

functionButtonPanel.add(divideButton);

functionButtonPanel.add(addButton);

functionButtonPanel.add(substractButton);

numberButtonsAction[] numberButtonActions = new numberButtonsAction[10];

for ( int i = 0; i < 10; i++ ) {

numberButtonActions[i] = new numberButtonsAction(numberButtons[i]);

numberButtons[i].addActionListener(numberButtonActions[i]);

}

EnterButton enter = new EnterButton();

enterButton.addActionListener(enter);

CButton c = new CButton();

cButton.addActionListener(c);

MultiplyButton multiply = new MultiplyButton();

multiplyButton.addActionListener(multiply);

DivideButton divide = new DivideButton();

divideButton.addActionListener(divide);

AddButton add = new AddButton();

addButton.addActionListener(add);

SubtractButton subtract = new SubtractButton();

substractButton.addActionListener(subtract);

motherPanel.add(textPanel);

motherPanel.add(numberButtonsPanel);

motherPanel.add(functionButtonPanel);

add(motherPanel);

setTitle("ButtonTest");

setSize(180, 290);

setLocationByPlatform(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setVisible(true);

}

private class numberButtonsAction implements ActionListener {

private String c;

public numberButtonsAction(JButton a) {

this.c = a.getText();

}

public void actionPerformed(ActionEvent e) {

if (!resultJText.getText().equals("0.0")) {

resultJText.setText(resultJText.getText() + c);

} else {

resultJText.setText("");

actionPerformed(e);

}

}

}

private class EnterButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

tempNumbers2 = Double.parseDouble(resultJText.getText());

if (function == 0) {

resultJText.setText(Double.toString((Math.round((tempNumbers1 / tempNumbers2) * 100)) / 100));

} else if (function == 1) {

resultJText.setText(Double.toString(tempNumbers1 * tempNumbers2));

} else if (function == 2) {

resultJText.setText(Double.toString(tempNumbers2 + tempNumbers1));

} else if (function == 3) {

resultJText.setText(Double.toString(tempNumbers1 - tempNumbers2));

} else {

resultJText.setText(String.valueOf(tempNumbers1));

}

tempNumbers1 = Double.parseDouble(resultJText.getText());

}

}

private class CButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

resultJText.setText("");

tempNumbers1 = 0;

tempNumbers2 = 0;

function = -1;

}

}

private class DivideButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

if (tempNumbers1 == 0) {

tempNumbers1 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

} else {

tempNumbers2 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

}

function = 0;

}

}

private class MultiplyButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

if (tempNumbers1 == 0) {

tempNumbers1 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

} else {

tempNumbers2 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

}

function = 1;

}

}

private class AddButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

if (tempNumbers1 == 0) {

tempNumbers1 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

} else {

tempNumbers2 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

}

function = 2;

}

}

private class SubtractButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

if (tempNumbers1 == 0) {

tempNumbers1 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

} else {

tempNumbers2 = Double.parseDouble(resultJText.getText());

resultJText.setText("");

}

function = 3;

}

}

}

Add a comment
Know the answer?
Add Answer to:
QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • Create a simple calculator. Your interface should contain two input boxes in which the user can...

    Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two number and clicks on an operation button, the result should be displayed in the label. Can some please help my code that i came up with just displays a pop box with no words or nothing! I'm not the best at coding. Any tips or advice...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • (java)write a simple graphical calculator with four operations: addition,substractuon, multipication division, that must do the following:...

    (java)write a simple graphical calculator with four operations: addition,substractuon, multipication division, that must do the following: a)it must support real and whole numbers (3.5 and 3 for instance).. b)it must support negative numbers using a button "+/-" that allows changing between positivity and negativity of the number c)the graphical interface of the calculator will include a matrix of buttons that include the digits 0,...,9 and "+" "-""*" "/" "." "+/-" and "=". d)the calculator will include a text field that...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • User interfaces are a critical part of any system. In this lesson, a Graphical User Interface...

    User interfaces are a critical part of any system. In this lesson, a Graphical User Interface (GUI) is created. The code generated will link the components to the action. Assignment: Create a Graphical User Interface that has two buttons and a textArea (See Examples 12.3 and Example 12.4). figure 1 Create a String array to store the following messages (Enter your name where it says YourName). "Congratulations YourName!nYou completed the Java class. nYou learned to write Java programs with commonly...

  • please write code in java language and do not add any break, continue or goto statements...

    please write code in java language and do not add any break, continue or goto statements Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT