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

*q3: Write a public class named OrganListener that implements the ActionListener

ID: 3702890 • Letter: #

Question

*q3: Write a public class named OrganListener that implements the ActionListener interface *This class wi1l have a public constructor that takes a JTextField and a JLabel as * parameters and stores these in instance variables. Override the actionPerformed method to * interpret the text on the JTextField as an x-value (as a double) and display the corresponding y-value on the 3Label for the line with slope -2.53 and y-intercept 6.92 Note that creating classes that implement listener interfaces we can add the same example, if we want 3 buttons that will compute y-values for this line with x-values from it *functionality to multiple components by creating multiple instances of this class. For * different text fields we can instantiate this class 3 times and attach each instance to a * different button

Explanation / Answer

import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class OrganListener implements ActionListener { private JTextField textField; private JLabel label; public OrganListener(JTextField textField, JLabel label) { this.textField = textField; this.label = label; } @Override public void actionPerformed(ActionEvent e) { double x = Double.parseDouble(textField.getText()); label.setText(Double.toString((-2.53 * x) + 6.92)); } }