Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume the existence of the following Java GUI application. There are two text f

ID: 3660921 • Letter: A

Question

Assume the existence of the following Java GUI application. There are two text fields for user input. The first JTextField variable is named voltage and is used to input a voltage value in volts. The second JTextField variable named resistance is used to input a resistance value in ohms. All input values and calculations use floating point values. The user clicks a JButton to perform a calculation. One JButton called current causes the program to calculate and display the current flowing through the resistor, I = V / R. Another JButton called power causes the program to calculate the power being dissipated by the resistor, P = V * V / R. The result of the selected calculation is displayed in an output text area variable named output. The output string should say something like this:

Explanation / Answer

GUITest.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class GUITest {

    /**
    * @param args
    */
    JFrame frame;
    JLabel voltageLabel;
    JLabel resistanceLabel;
    JTextField volatge;
    JTextField resistance;
    JButton currentButton;
    JButton powerButton;
    JTextArea output;
    public GUITest() {
        frame = new JFrame("Current / Power Calculator");
        voltageLabel = new JLabel("Voltage:");
        resistanceLabel = new JLabel("Resistance:");
        volatge = new JTextField(10);
        resistance = new JTextField(10);
        currentButton = new JButton("Calculate Current");
        powerButton = new JButton("Calculate Power");
        output = new JTextArea(5, 40);
       
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.add(voltageLabel);
        frame.add(volatge);
        frame.add(resistanceLabel);
        frame.add(resistance);
        frame.add(currentButton);
        frame.add(powerButton);
        frame.add(output);
        frame.setSize(400, 200);
        frame.setVisible(true);
       
        currentButton.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                double v = Double.parseDouble(volatge.getText());
                double r = Double.parseDouble(resistance.getText());
                output.setText(v + " volts dropped across " + r + " ohms creates current of " + v/r + " amps");
            }
        });
        powerButton.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                double v = Double.parseDouble(volatge.getText());
                double r = Double.parseDouble(resistance.getText());
                output.setText(v + " volts dropped across " + r + " ohms creates power of " + (v*v)/r + " watts");
            }
        });
       
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new GUITest();
    }

}

Output:


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote