In this homework, you will create a calculator with 4 cores, i.e. the calculator can evaluate...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Answer rating: 100% (QA)
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... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Marcy has received a special order for 2,000 units of its product at a special price of $60. The product normally sells for $80 and has the following manufacturing costs: Per Unit Direct materials...
-
Evaluate dx with ordinary u-substitution technique. 1+x
-
In this assignment, you will create a program that allows theuser to draw in a window using brushes of different sizes andcolors. The user will be able to change the size and color of thebrush using...
-
The drawing shows a frictionless incline and pulley. The two blocks are connected by a wire (mass per unit length = 0.0250 kg/m) and remain stationary. A transverse wave on the wire has a speed of...
-
Evaluate: (a) C is the quarter circle from (0, -1) to (1, 0), centered at the origin. (b) C is the curve x = t, y = cos t, z = sin t, 0 ( t ( (/2? (1-v2) ds: xydx + z cos x dy +zdz;
-
1) Calculate the heat gained (inJ) during the heating of 20.0 g of H2O from 22 C to 77 C. The specific heat of H20 = 4.18J / (g) C 2) If a person can comfortably carry 20.0 kg of goldwhich has a...
-
What two major guides do businesses use to determine acceptable levels of performance?
-
1. Why did Nationwide need an enterprise-wide data warehouse? 2. How did integrated data drive the business value? 3. What forms of analytics are employed at Nationwide? 4. With integrated data...
-
Celeron Furniture Center had accounts receivable of $ 26 comma 000 at the beginning of the year and $ 58 comma 000 at year-end. Revenue for the year totaled $ 111 comma 000. How much cash did the...
-
Jackson's Vacuum company makes two types of vacuums: Basic and Advanced. The factory is set up into 3 working departments. Both vacuums utilize process #1 to assemble the basic components. Those...
-
On 1 July 2014 Marcus, (an adult Australian Resident) bought a property for $550,000. He used it as a rental property and collected rent throughout the ownership period He incurred the following...
-
You bought a 2.75% annual coupon bond for $800. The bond has 25 years to maturity. If 4 years from now the YTM on your bond has decreased and you sell your bond for $880, what will be your capital...
-
Half a liter of gas in an air conditioner is initially at a temperature of 25 degrees Celsius and 2.0 atm of pressure. The volume of the gas is rapidly expanded to 2 L and the pressure of the gas...
-
Describe the 'Big Five' factors as they relate to HRM (do not just list them). Which seem more related to managerial positions? Why?
-
The 1974-1975 period experienced a recession caused by a supply shock. In particular following the Arab-Israeli War of 1973, the Organization of the Petroleum Exporting Countries (OPEC) took actions...
-
Using the high-low method, please calculate the fixed costs and the variable cost of overhead at Riley's Human Fertilizer co-op. Show your work.sing the high-low method, please calculate the fixed...
-
What are the social consequences of environmental degradation and climate change, particularly in terms of resource distribution, displacement, and social conflict?
-
A consultant is beginning work on three projects. The expected profits from these projects are $50,000, $72,000, and $40,000. The associated standard deviations are $10,000, $12,000, and $9,000....
-
David and Darlene Jasper have one child, Sam, who is 6 years old. The Jaspers reside at 4639 Honeysuckle Lane, Los Angeles, CA 90248. David's Social Security number is 577-11-3311, Darlene's is...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-4. Ivan and Irene paid the following in 2012 (all by check or can otherwise be...
-
Ken paid the following amounts for interest during 2012: Qualified interest on home mortgage...........................................$4,700 Auto loan...
-
Using the following data for Evans \& Sons, calculate the cash paid for rent: Rent expense... $80,000 Prepaid rent, January 1 10,000 Prepaid rent, December 31 8,000
-
Using the following data for Evans \& Sons, calculate the cash received as interest: Interest income.. $30,000 Interest receivable, January 1 3,000 Interest receivable, December 31..... 3,700
-
Using the following data for Evans \& Sons, calculate the cash paid for merchandise purchased: Cost of goods sold. Inventory, January 1. Inventory, December 31 $128,000 19,000 22,000 Accounts...
Study smarter with the SolutionInn App