Write a Java application that displays a JFrame containing a JPanel. The panel w
ID: 3834789 • Letter: W
Question
Write a Java application that displays a JFrame containing a JPanel. The panel will contain three text fields labeled: A, B, C. There will also be another JLabel that shows the result and a button labeled Solve. When the Solve button is pushed, the program solves the quadratic equation ax^2 + bx + c = 0 given values A, B, and C in the text fields. If the coefficient A is zero, the equation is linear and has one root at -C/B. If both A and B are zero write out an error message. Otherwise, the equation is quadratic, so use the quadratic equation. X = -b plusminus squareroot b^2 - 4ac/2a Dispaly the result by changing a JLabel. It will either display both roots, display one root, or display an error message. The ideas you need are in the push counter program and in the Fahrenheit program. All you need is a listener for Action Events from the button. That listener can get text from the text fields and convert them to numbers. You don't need to listen to events from the text fields. Start the program with a documentation header that says what it is, who did it, and when. Also, have some documentation that says what is going on. The Java source files for your application. You should have two files: Quadratic. java and Quadratic Panel .java Don't turn in the entire BlueJ project.Explanation / Answer
import javax.swing.JFrame;
public class Quadratic {
public static void main(String args[]) {
JFrame frame = new JFrame("Quadratic");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
QuadraticPanel panel = new QuadraticPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
public class QuadraticPanel extends JPanel {
private static final long serialVersionUID = 7289726775842563057L;
private JLabel answerLabel;
private JLabel labelA;
private JLabel labelB;
private JLabel labelC;
private JButton solveButton;
private JTextField textFieldA;
private JTextField textFieldB;
private JTextField textFieldC;
/**
* Creates new form QuadraticPanel
*/
public QuadraticPanel() {
initComponents();
}
/**
* Listener/Event Handler for Solve Button
*/
private void solveButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
double A = Double.parseDouble(textFieldA.getText());
double B = Double.parseDouble(textFieldB.getText());
double C = Double.parseDouble(textFieldC.getText());
if (A == 0 && B == 0) {
answerLabel.setText("Error, A and B are both zeroes!");
}
else if (A == 0) {
double root = -C/B;
answerLabel.setText("The root is " + (float) root);
}
else {
double deter = B*B - 4*A*C;
if (deter < 0) {
double realPart = (-B) / (2*A);
double imaginary1 = (-deter) / (2*A);
double imaginary2 = (deter) / (2*A);
answerLabel.setText("The roots are (" + (float)realPart+ "+" + (float)imaginary1+ "i) and (" + (float)realPart+ "+" + (float)imaginary2+ "i)");
}
else {
double root1 = (-B + deter) / (2*A);
double root2 = (-B - deter) / (2*A);
answerLabel.setText("The roots are " + (float)root1 + " and " + (float)root2);
}
}
} catch (Exception e) {
answerLabel.setText("Error, invalid values!");
}
}
private void initComponents() {
textFieldA = new JTextField();
textFieldB = new JTextField();
textFieldC = new JTextField();
labelA = new JLabel("A");
labelB = new JLabel("B");
labelC = new JLabel("C");
answerLabel = new JLabel(" ");
solveButton = new JButton("Solve");
labelA.setFont(new java.awt.Font("Ubuntu", 1, 15));
labelB.setFont(new java.awt.Font("Ubuntu", 1, 15));
labelC.setFont(new java.awt.Font("Ubuntu", 1, 15));
answerLabel.setFont(new java.awt.Font("Ubuntu", 1, 15));
answerLabel.setHorizontalAlignment(SwingConstants.CENTER);
solveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
solveButtonActionPerformed(evt);
}
});
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(146, 146, 146)
.addComponent(solveButton, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(labelA)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFieldA, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addComponent(labelB)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFieldB, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addComponent(labelC)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFieldC, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(answerLabel, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(textFieldA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(labelA)
.addComponent(textFieldB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(labelB)
.addComponent(textFieldC, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(labelC))
.addGap(34, 34, 34)
.addComponent(solveButton)
.addGap(28, 28, 28)
.addComponent(answerLabel)
.addContainerGap(88, Short.MAX_VALUE))
);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.