Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //QuadraticsWithMethods by Coby Boley
- import java.text.DecimalFormat;
- import javax.swing.JOptionPane;
- public class QuadraticsWithMethods {
- public static void main(String[] args) {
- JOptionPane.showMessageDialog(null, "QuadraticsWithMethods by Coby Boley");
- double a = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient A");
- double b = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient B");
- double c = inHandler("For the equation, Ax^2+Bx+C = 0, please enter the coefficient C");
- JOptionPane.showMessageDialog(null, "a: "+a+ "\nb: "+b+"\nc: "+c);
- }
- public static double inHandler(String prompt){
- double returnValue = 0;
- boolean tryAgain = false;
- do {
- String s = JOptionPane.showInputDialog(prompt);
- try {
- returnValue = Double.parseDouble(s);
- tryAgain = false;
- } catch (NumberFormatException e){
- returnValue = Double.NaN;
- if (Double.isNaN(returnValue)){
- JOptionPane.showMessageDialog(null, "you have entered something I can't understand, please try again.");
- tryAgain = true;
- }
- }
- } while (tryAgain);
- return returnValue;
- }
- public static double discriminant (double a, double b, double c) {
- double disc;
- disc = b*b - 4*a*c;
- return disc;
- }
- public static String comp(double disc, double b, double a) {
- String rtn;
- double comp = Math.sqrt(Math.abs(disc));
- double compA = comp/2*a;
- double compB = -b/2*a;
- compA = Double.parseDouble(new DecimalFormat("##.###").format(compA));
- compB = Double.parseDouble(new DecimalFormat("##.###").format(compB));
- if (-b == 0) {
- rtn = "\nThe complex roots are: " + compA + "i and -" + compA + "i";
- } else {
- rtn = "\nThe complex roots are: " + compB + "+" + compA + "i and" + compB + "-" + compA + "i";
- }
- return rtn;
- }
- public static void root(double a,double b,double c,double disc)
- {
- //declaring roots as double
- double root1;
- double root2;
- if (disc > 0)
- {
- //real roots if two roots show
- root1 = (-b + Math.sqrt(disc)) / (2 * a);
- DecimalFormat df1 = new DecimalFormat("#.###");
- root1 = Double.valueOf(df1.format(root1));
- root2 = (-b - Math.sqrt(disc)) / (2 * a);
- DecimalFormat df2 = new DecimalFormat("#.###");
- root2 = Double.valueOf(df2.format(root2));
- JOptionPane.showMessageDialog(null,"The roots are: " +root1+" and "+root2);
- //JOptionPane.showMessageDialog(null,"The roots of the Quadratic Equation \"Ax^2 + Bx +C = 0\" are "+root1+" and "+root2);
- }
- else if (disc==0)
- {
- //real roots if roots are the same
- root1 = (-b + Math.sqrt(disc)) / (2 * a);
- JOptionPane.showMessageDialog(null,"Roots is:"+ root1);
- //System.out.println("Root is: " +root1);
- }
- }
- //string to receive test values
- public static double TestNum(String prompt)
- {
- double returnVal = 0;
- String s = JOptionPane.showInputDialog(prompt);
- returnVal = Double.parseDouble(s);
- return returnVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement