Hi all,
Im writing a program, that takes the a, b, and c from an equation, and uses them to find x using the formula:
http://www.purplemath.com/modules/quads/qform01.gif.

The problem im getting, is that when I plugin the equation 1x^2 +3x +4 I get x = -Infinity and x = infinity instead of x = 1 and x = -4.

Heres my code:

Class 1:

public class quadratictest
{
    public static void main(String args[])
    {
        DecimalFormat df = new DecimalFormat("#.###");
        System.out.println("--------------------------------------------------");
        System.out.println("               ~Quadratic Formula~");
        System.out.println("--------------------------------------------------");
        System.out.println("in a polynomial, there are 3 important numbers used");
        System.out.println("to figure out x. they are a, b, and c, shown below.\n");
        System.out.println("\t\t1x^2 +3x +4");
        System.out.println("\t\t^     ^   ^");
        System.out.println("\t\ta     b   c");
        Scanner input = new Scanner(System.in);
        System.out.print("\nPlease type a, b, and c here[a b c]: ");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        mathey quad = new quadsong(a,b,c);
        System.out.println("------------");
        System.out.println(quad.solveb());
        System.out.println(quad.solvea());
        //System.out.println("x =" +df.format(quad.solvea()));
        //System.out.println("x =" +df.format(quad.solveb()));
        System.out.println("------------");
    }
}

Class 2:

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
/**
 * Write a description of class quadsong here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mathey
{
    int a;int b;int c;
    double solution1;
    double solution2;
      public mathey(int aN, int bN, int cN)
    {
        int a  = aN;
        int b = bN;
        int c = cN;
        solvea();
        solveb();
    }
    public double solvea()
    {
     solution1 = ( (b*-1) + Math.sqrt((b^2)-(4*a*c)))/(a+a);
      if (solution1 == Math.floor(solution1))
     {
     return solution1;
    }
    else
    {
        return 0;
    }
    }
        public double solveb()
    {
     solution2 = ( (b*-1) - Math.sqrt((b^2)-(4*a*c)))/(2*a);
     if (solution2 == Math.floor(solution2))
     {
     return solution2;
    }
    else
    {
        return 0;
    }
    }
}

Whats going wrong? Thanks in advance!

Dani AI

Generated

Quick diagnosis: two bugs cause the Infinity results. The constructor in the solver redeclares local int a = aN; int b = bN; int c = cN;, which shadows the instance fields so the object’s a, b, c stay 0 — that makes the denominator 2*a zero and produces ±Infinity. Also ^ in Java is bitwise XOR, not exponentiation; use b*b or Math.pow(b,2) for the discriminant. As a side note, the polynomial 1x^2 + 3x + 4 has discriminant 9 - 16 = -7, so its roots are complex (the pair 1 and -4 would require c = -4).

A compact, corrected approach (use double for coefficients, assign to fields, handle a==0, and handle complex roots):

public class Quadratic {
  private double a, b, c;
  public Quadratic(double a, double b, double c) {
    this.a = a; this.b = b; this.c = c;
  }
  public String[] roots() {
    if (a == 0) return new String[] { String.valueOf(-c / b) }; // linear
    double disc = b * b - 4 * a * c;
    if (disc < 0) {
      double real = -b / (2 * a);
      double imag = Math.sqrt(-disc) / (2 * a);
      return new String[] { real + " + " + imag + "i", real + " - " + imag + "i" };
    }
    double s = Math.sqrt(disc);
    return new String[] { String.valueOf((-b + s) / (2 * a)), String.valueOf((-b - s) / (2 * a)) };
  }
}

Practical fixes and tips: replace the local declarations in the constructor with this.a = aN; this.b = bN; this.c = cN;; stop using ^ for squaring; remove the floor check that returns 0 for non-integer roots; avoid unnecessary calls to solve in the constructor (as noted); and make sure the instantiated class name matches the solver class (mathey vs quadsong). After those changes, supplying 1 3 -4 yields 1 and -4, while 1 3 4 yields complex conjugates.

What answers do you get exactly? Can you paste the output?
In your constructor of the class that solves your equation, why do you make it solve when you create it, but then later on solve it again?

 public mathey(int aN, int bN, int cN)
    {
        int a  = aN;
        int b = bN;
        int c = cN;
        //Why is this here?
        solvea();
        solveb();
    }
    //If you just print it out above?
     System.out.println(quad.solveb());
    System.out.println(quad.solvea());
    //Maybe print out solution1 & 2, or get rid of solvea() & b() in your constructor

Also, did you forget to provide some code? because the constructor you use isn't the class you provided.

mathey quad = new quadsong(a,b,c);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.