Week 4 Programming Assignment
Write a Java GUI application to calculate total resistance, current, and power in a circuit. The general appearance of you GUI should be as shown below:
Your program must meet the following requirements:
1. Do not use any of the GUI editing capabilities of Eclipse for this assignment (no setBounds). Do all the GUI layout work based on what you have learned from the lecture material and assigned book readings in the last 2 weeks.
2. You will only need to use the ActionListener interface for this lab. The only events that your program handles are action events from the 4 buttons.
3. This program is contained in 1 class that has a constructor, a main method, and a nested inner class for the event handler.
4. You must use inheritance to get a JFrame into your application
5. The GUI and event handling setup must be done in the constructor of your GUI class or in private methods called from the constructor. Hint: it would seem logical to have a private method to build the top, right, left, and bottom panels.
6. The GUI should be organized as follows:
• The top panel will contain 2 labels and 2 text fields. The text fields should be initialized with a parameter of 8. No special layout manager needed here.
• The bottom panel will contain 4 buttons. Use a grid layout here with horizontal and vertical gaps of 5.
• The right panel contains 3 labels and 3 text fields initialized with a parameter of 10. Use a grid layout here with horizontal and vertical gaps of 5.
• The left panel contains a scrollpane which contains a text area setup for 10 rows and 18 columns. No special layout manager needed here.
• Place the 4 panels into the frame in the appropriate regions of the frame’s border layout.
• You can create space around a panel by doing the following:
yourPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
How the Application Works
A user will enter one value into the voltage text field and one value into the resistance text field. Then the user will click either the Series Resistor or Parallel Resistor button. This action causes the program to store the voltage value and the resistance value and output the voltage and resistance values to the text area as shown in the screen shot above. The user can then place another value into the resistance text field and again click the Series Resistor or Parallel Resistor buttons. This action causes the resistance value to be reported in the text area and the resistance value is used to update the total resistance calculations internally. When the user is finished adding resistors, the user clicks the Calculate Totals button which causes the program to calculate and output the total resistance, current, and power into the appropriate text fields. Clicking the New Circuit button should clear the total resistance, current, and power text fields. It should clear the text area and place the initial instruction to “Enter volts, then resistance values” into the text area.
Event Handling Design
First of all you will need 3 member variables to hold values to be used over the course of the circuit entry. These three member variables are all doubles: one for voltage, one to accumulate total resistance, and one to accumulate resistance of the current parallel part of the circuit.
Second, you will need one nested inner class that implements the ActionListener interface. One object of this class is to be created and added to the 4 buttons as the event handler.
Inside the nested inner class you will have to use if statements to check to see which of the 4 buttons was clicked. The following describes what to do for each of the 4 buttons:
1. New Circuit button: Clear the three text fields for total resistance, power, and current. Set the text area to read “Enter volts, then resistance values.”. Set the 3 member variables mentioned above back to 0.
2. Series Resistor button: If the voltage member variable is 0, call a function that gets the voltage value from the voltage text field, reports the voltage value in the text area, and stores the numeric value of the voltage into the voltage member variable. (This function will be called from the next button also.). Get the value of the resistor from the resistor text field and report adding a series resistor in the text area. Convert the value of the resistance to a numeric value and add it to the member variable used to keep track of total resistance. In addition, if the member variable used for accumulating parallel resistance is non-zero, add that to the total resistance member variable, then set the parallel resistance variable to 0.
3. Parallel Resistor button: If the voltage member variable is 0, call a function that gets the voltage value from the voltage text field, reports the voltage value in the text area, and stores the numeric value of the voltage into the voltage member variable. (This function was called from the previous button also.). Get the value of the resistor from the resistor text field and report adding a parallel resistor in the text area. Convert the value of the resistance to a numeric value and if the member variable used to keep track of parallel resistance is 0, set the parallel resistance member variable to this resistance value. If the member variable is not zero, calculate the new parallel resistance value by using the following formula:
R_parallel = (R_parallel * new_resistance) / (R_parallel + new_resistance)
4. Calculate Totals button: If the parallel resistance member variable is not 0, add it to the total resistance member variable and set the parallel resistance variable to 0. Output the total resistance member variable to the total resistance text field with 3 digits after the decimal point. Use String.format! Output the current to the total current text field by calculating the current as voltage / total resistance. Also use String.format to set the output to 3 digits after the decimal point. Finally, output the power to the total power text field by calculating the power as (voltage * voltage) / total resistance. Also use String.format to set the output to 3 digits after the decimal point. The output should appear as shown in the screen shot above.
Also make sure that you add comments to your program!
Take screen shots of the program showing where you added at least 1 series resistor and 2 parallel resistors and show the calculated outputs. Paste the screen shot and the source code for your program into a Word document.