Hello! so this is java language. i am working on gui java program. this program
ID: 3841997 • Letter: H
Question
Hello! so this is java language. i am working on gui java program. this program solves quadratic equations. what i have so far works but not for imageinary numbers. so i want to add codes which will solve imaginary numbers.
example will be if A=1, B=2, C=3. then solution is with imaginary number(giveing in the screenshot). this is what i have so far and solution for imaginary numbers need to looks like it is in the screenshot. (1+2, 12)
Thanks.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
class QuadraticFrame extends JFrame
{
private JLabel lblTitle;
private JLabel lblFirst;
private JLabel lblSecond;
private JLabel lblThird;
private JTextField txtFirst;
private JTextField txtSecond;
private JTextField txtThird;
private JTextField txtAnswer1;
private JTextField txtAnswer2;
private JButton btnSolve;
private JButton btnClear;
private final int WINDOW_WIDTH = 700;
private final int WINDOW_HEIGHT = 700;
private Font font = new Font("Serif", Font.BOLD, 30);
public QuadraticFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Find the roots");
buildPanels();
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}
private void addComponentsAndSetFont()
{
JComponent components[] = {
lblTitle, lblFirst, lblSecond, lblThird, txtFirst, txtSecond, txtThird,
txtAnswer1, txtAnswer2, btnSolve, btnClear
};
for(int i=0; i<components.length; i++)
{
components[i].setFont(font);
add(components[i]);
}
}
private void buildPanels()
{
setLayout(null);
lblTitle = new JLabel("Find the roots of ax^2 + bx + c");
lblFirst = new JLabel("Enter The First Coefficient");
lblSecond = new JLabel("Enter The Second Coefficient");
lblThird = new JLabel("Enter The Third Coefficient");
txtFirst = new JTextField(10);
txtSecond = new JTextField(10);
txtThird = new JTextField(10);
txtAnswer1= new JTextField(10);
txtAnswer2= new JTextField(10);
btnSolve = new JButton("Solve");
btnClear = new JButton("Clear");
ButtonListener listener = new ButtonListener();
btnSolve.addActionListener(listener);
btnClear.addActionListener(listener);
lblTitle.setBounds(20,10, 400, 50);
lblFirst.setBounds(20,70, 400, 50);
lblSecond.setBounds(20,130, 400, 50);
lblThird.setBounds(20, 190, 400,50);
txtFirst.setBounds(500,70, 400, 50);
txtSecond.setBounds(500, 130, 400, 50);
txtThird.setBounds(500, 190, 400, 50);
btnSolve.setBounds(290, 290, 200, 50);
btnClear.setBounds(510, 290, 200, 50);
txtAnswer1.setBounds(30,360, 400, 50);
txtAnswer2.setBounds(30,420, 400, 50);
addComponentsAndSetFont();
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Solve"))
{
DecimalFormat myFor = new DecimalFormat("0.00");
double a = Double.parseDouble(txtFirst.getText());
double b = Double.parseDouble(txtSecond.getText());
double c = Double.parseDouble(txtThird.getText());
double answer1 = (-b + Math.sqrt(b*b - 4*a*c))/(2*a);
double answer2 = (-b - Math.sqrt(b*b - 4*a*c))/(2*a);
txtAnswer1.setText(String.valueOf(myFor.format(answer1)));
txtAnswer2.setText(String.valueOf(myFor.format(answer2)));
}
else if (actionCommand.equals("Clear"))
{
txtFirst.setText("");
txtSecond.setText("");
txtThird.setText("");
txtAnswer1.setText("");
txtAnswer2.setText("");
}
}
}
}
public class QuadraticApp
{
public static void main (String args[])
{
new QuadraticFrame();
}
}
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
class QuadraticFrame extends JFrame
{
private JLabel lblTitle;
private JLabel lblFirst;
private JLabel lblSecond;
private JLabel lblThird;
private JTextField txtFirst;
private JTextField txtSecond;
private JTextField txtThird;
private JTextField txtAnswer1;
private JTextField txtAnswer2;
private JButton btnSolve;
private JButton btnClear;
private final int WINDOW_WIDTH = 700;
private final int WINDOW_HEIGHT = 700;
private Font font = new Font("Serif", Font.BOLD, 30);
public QuadraticFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Find the roots");
buildPanels();
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}
private void addComponentsAndSetFont()
{
JComponent components[] = {
lblTitle, lblFirst, lblSecond, lblThird, txtFirst, txtSecond, txtThird,
txtAnswer1, txtAnswer2, btnSolve, btnClear
};
for(int i=0; i<components.length; i++)
{
components[i].setFont(font);
add(components[i]);
}
}
private void buildPanels()
{
setLayout(null);
lblTitle = new JLabel("Find the roots of ax^2 + bx + c");
lblFirst = new JLabel("Enter The First Coefficient");
lblSecond = new JLabel("Enter The Second Coefficient");
lblThird = new JLabel("Enter The Third Coefficient");
txtFirst = new JTextField(10);
txtSecond = new JTextField(10);
txtThird = new JTextField(10);
txtAnswer1= new JTextField(10);
txtAnswer2= new JTextField(10);
btnSolve = new JButton("Solve");
btnClear = new JButton("Clear");
ButtonListener listener = new ButtonListener();
btnSolve.addActionListener(listener);
btnClear.addActionListener(listener);
lblTitle.setBounds(20,10, 400, 50);
lblFirst.setBounds(20,70, 400, 50);
lblSecond.setBounds(20,130, 400, 50);
lblThird.setBounds(20, 190, 400,50);
txtFirst.setBounds(500,70, 400, 50);
txtSecond.setBounds(500, 130, 400, 50);
txtThird.setBounds(500, 190, 400, 50);
btnSolve.setBounds(290, 290, 200, 50);
btnClear.setBounds(510, 290, 200, 50);
txtAnswer1.setBounds(30,360, 400, 50);
txtAnswer2.setBounds(30,420, 400, 50);
addComponentsAndSetFont();
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Solve"))
{
DecimalFormat myFor = new DecimalFormat("0.00");
double a = Double.parseDouble(txtFirst.getText());
double b = Double.parseDouble(txtSecond.getText());
double c = Double.parseDouble(txtThird.getText());
double answer1 = (-b + Math.sqrt(b*b - 4*a*c))/(2*a);
double answer2 = (-b - Math.sqrt(b*b - 4*a*c))/(2*a);
txtAnswer1.setText(String.valueOf(myFor.format(answer1)));
txtAnswer2.setText(String.valueOf(myFor.format(answer2)));
}
else if (actionCommand.equals("Clear"))
{
txtFirst.setText("");
txtSecond.setText("");
txtThird.setText("");
txtAnswer1.setText("");
txtAnswer2.setText("");
}
}
}
}
public class QuadraticApp
{
public static void main (String args[])
{
new QuadraticFrame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.