Java desktop application for computing the roots of a quadratic equation – Source code
The source code of a Java desktop application for solving a quadratic equation is shown below. The application is built using this Java code for computing the roots of the equation.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
public class QuadCalc implements ActionListener{
JFrame frame;
MyPanel panel;
JTextArea textArea;
JLabel aLabel, bLabel, cLabel;
JTextField aBox,bBox,cBox;
JButton[] functionButtons = new JButton[2];
JButton submitButton, clearButton;
JPanel labelPane;
Font myFont = new Font("Arial",Font.BOLD,18);
Font myFont2 = new Font("Arial",Font.PLAIN,14);
Color myColor = new Color(240,250,255);
double a=0,b=0,c=0,x=0,x1=0,x2=0;
String prnText = "";
String s;
QuadCalc(){
frame = new JFrame("Quadratic Equation Solver");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
panel = new MyPanel();
textArea = new JTextArea();
textArea.setBounds(50, 300, 300, 100);
textArea.setFont(myFont2);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
aLabel = new JLabel("a: ",JLabel.RIGHT);
aLabel.setFont(new Font("Verdana", Font.BOLD, 18));
aBox = new JTextField();
aBox.setBounds(100, 150, 50, 50);
aBox.setBackground(new Color(240,250,255));
aBox.setFont(myFont);
aBox.setEditable(true);
aBox.addActionListener(this);
bLabel = new JLabel("b: ",JLabel.RIGHT);
bLabel.setFont(new Font("Verdana", Font.BOLD, 18));
bBox = new JTextField();
bBox.setBounds(200, 150, 50, 50);
bBox.setBackground(myColor);
bBox.setFont(myFont);
bBox.setEditable(true);
bBox.addActionListener(this);
cLabel = new JLabel("c: ",JLabel.RIGHT);
cLabel.setFont(new Font("Verdana", Font.BOLD, 18));
cBox = new JTextField();
cBox.setBounds(300, 150, 50, 50);
cBox.setBackground(myColor);
cBox.setFont(myFont);
cBox.setEditable(true);
cBox.addActionListener(this);
//Lay out the labels and text boxes in a panel.
labelPane = new JPanel();
labelPane.setBounds(50,150,300,50);
labelPane.setLayout(new GridLayout(1,6));
labelPane.add(aLabel);
labelPane.add(aBox);
labelPane.add(bLabel);
labelPane.add(bBox);
labelPane.add(cLabel);
labelPane.add(cBox);
submitButton = new JButton("Submit");
submitButton.setBounds(50,225,100,50);
clearButton = new JButton("Clear");
clearButton.setBounds(50,430,100,50);
functionButtons[0] = submitButton;
functionButtons[1] = clearButton;
for(int i =0;i<2;i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}
frame.add(submitButton);
frame.add(clearButton);
frame.add(labelPane);
frame.add(textArea);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
QuadCalc calc = new QuadCalc();
}
class MyPanel extends JPanel{
MyPanel(){
this.setBounds(25,25,325,150);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
String prnText1 = "A quadratic equation has a form of";
String prnText2 = "aX2 + bX + c = 0";
String prnText3 = "To solve your equation, enter values";
String prnText4 = "of a, b, c into the boxes:";
// superscript
AttributedString supText = new AttributedString(prnText2);
supText.addAttribute(TextAttribute.SIZE, 18, 0, 16);
supText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 2, 3);
g2D.setPaint(Color.BLUE);
g2D.setFont(new Font("Dialog",Font.PLAIN,18));
g2D.drawString(prnText1,25,25);
g2D.drawString(supText.getIterator(), 25, 50);
g2D.drawString(prnText3,25,75);
g2D.drawString(prnText4,25,100);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==submitButton) {
a=Double.parseDouble(aBox.getText());
b=Double.parseDouble(bBox.getText());
c=Double.parseDouble(cBox.getText());
if (a == 0.0) {
if (b == 0.0) {
if (c != 0) {
prnText = "The equation has no solutions";
} else {
prnText = "The equation has infinite solutions";
}
} else {
x = -c/b;
s = String.format("%.2f",x);
prnText = "The equation is linear and has one root:\nX = ".concat(s);
}
} else {
double delta = b*b - 4*a*c;
if (delta == 0.0) {
x = -b/(2*a);
s = String.format("%.2f",x);
prnText="The equation has one unique root: \nX = ".concat(s);
} else if (delta > 0.0) {
double x1 = (-b - Math.sqrt(delta))/(2*a);
double x2 = (-b + Math.sqrt(delta))/(2*a);
s = String.format("%.2f",x1);
prnText="The equation has two roots: \n"+"X1 = ".concat(s);
prnText += ",\n"+ "X2 = "+String.format("%.2f",x2);
} else if (delta < 0.0) {
double realx = -b/(2*a);
double imx = (Math.sqrt(Math.abs(delta)))/(2*a);
prnText="The equation has two conjugate complex roots\n";
prnText += "X1 = "+String.format("%.2f",realx)+" - i("+String.format("%.2f",Math.abs(imx))+"), \n";
prnText += "X2 = "+String.format("%.2f",realx)+" + i("+String.format("%.2f",Math.abs(imx))+")";
}
}
textArea.setText(prnText);
}
if(e.getSource()==clearButton) {
textArea.setText("");
aBox.setText("");
bBox.setText("");
cBox.setText("");
}
}
}
