Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this homework, you will create a calculator with 4 cores, i.e. the calculator can evaluate 4 mathematical expressions in parallel Problem 1: Implement
In this homework, you will create a calculator with 4 cores, i.e. the calculator can evaluate 4 mathematical expressions in parallel Problem 1: Implement class Calculator with the interface as follow: Calculator 12+8*4 cpu 1 cpu 2 cpu 3 cpu 4 0 3 6 9 Clear 1 7 Del 2 5 8 + 1 X Figure 1: Caption You will need some data members for your class, such as some arrays of JButtons, and JTextArea. The most needed data member is a string variable, named expression, to keep track of a mathe- matical expression. This variable is initialized as a string that contains "0" character. private String expression = "0"; You will need 4 JPanels to organize your calculator buttons panell: contains a JTextArea to display mathematical expressions, uses a default layout. panel2: contains 4 JButtons to compute mathematical expressions and 4 JTextAreas to display results. panel2 use a grid layout. These 5 JText Areas are set to be un-editable. panel3: use a border layout to contain panell and panel2. Put panell at the top, and panel2 at the center of panel3 panel4: uses a grid layout to display 10 digit buttons, dot button, 4 operation buttons (addition, subtraction, multiplication, and division), clear and delete buttons. set hgap and vgap to be 3 pixels in these grid layouts. set border layout to Calculator, then put panel3 and panel4 at the top and center of Calculator accordingly. You need the string variable expression to store and display mathematical expressions. The first JTextArea should work as follows At the beginning, the top JTextArea displays 0. As you push the digit, dot, or operation buttons, the JText Area changes accordingly. If you keep pushing 0 at the beginning, the display should not change, why? 0000 is still 0. Replace the operator if you choose a different one. For example, the current expression "8*9+7*" will change to "8*9+7/" if you push the "/" button. The expression is reset to 0 if the Clear button is pressed. The Del button will delete the last letter in the expression. If there is one letter left, the Del button should reset the expression to 0. To perform the above button actions, you need to write an inner class NumberListener to implement the interface NumberListener, i.e. implement its actionPerformed () method private class NumberListener implements ActionListener { @Override public void action Performed (Action Event e) {...} } The mathematical expression will not be evaluated until one of the CPU buttons is pressed. You can assume that each CPU button represents an individual CPU that can do computation inde- pendently. Specifically When a CPU button is pressed, the expression is sent to this CPU. Your program should eval- uation this mathematical expression by calling method evaluation provided in the template. Do not modify this code, please. private double evaluation (String expression); 2 The CPU button should be disabled for 5000 milliseconds right after it is pressed. You will need to call function setEnabled to set the Disability of JButtons. During this computational time, the user can press other CPU buttons, or reset and enter other mathematical expressions, etc. i.e. your program is not frozen while a task is performed. After 5000 milliseconds, the CPU button is enabled again and the next-sitting JTextArea will display the result. To carry out this task, you need to write inner class Calculator Thread to extend the Thread class private class CalculatorThread extends Thread { } //some data members // constructor: public Calculator Thread (String expression, JButton button, JTextArea textArea) { } //your implementation @Override public void run () { } // Your implementation: // 1. Disable the CPU button // 2. Evaluation the expression // 3. Put the thread to sleep for 5000 milliseconds // 4. Display the result in textArea // 5. Enable the CPU button } Your class provides a constructor that takes three parameters: a String variable, a JButton, and a JTextArea. These parameters are used to initialize its data member and will be used again in the run () method. You also need to write an inner class CPUListener to implement the interface ActionListener and add this to each CPU button: private class CPUListener implements ActionListener{ @Override public void action Performed (ActionEvent e) { // Your implementation: Check which CPU button is pressed, // then create and run a Calculator Thread Problem 2: Implement class CalculatorApp to test the Calculator Create a JFrame with title "Calculator" and a Calculator with 4 CPU. Add the panel to the JFrame. Make sure that the JFrame appears normally. You can call methods pack() or setSize(int, int) to set the size to the JFrame. 3
Step by Step Solution
★★★★★
3.47 Rating (160 Votes )
There are 3 Steps involved in it
Step: 1
Below is a basic implementation of the classes described in your problem statement Please note that the code may need further adjustments based on your specific requirements and the GUI library you ar...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started