Search code examples
javaformulaequationquadratic

Java only returns 0 when equation is unfactorable


import java.util.Scanner;

public class QuadraticEquation
{
   private int a, b, c, d, s1, s2;

   public QuadraticEquation()
   {
      a = 1;
      b = 2;
      c = 1;
      d = 0;
      s1 = -1;
      s1 = -1;
   }

   public QuadraticEquation(int a, int b, int c)
   {
      this.a = a;
      this.b = b;
      this.c = c;
   }

   public int findDiscriminant(int a, int b, int c)
   {
      this.d = (int)Math.pow(b,2) - (4*a*c);
      return this.d;
   }

   public boolean equalRoots()
   {
      if (getSolution1() == getSolution2())
      {
         return true;
      } else {
         return false;
      }
   }

   public boolean noSolution()
   {
      if (Math.sqrt(this.d) == 0)
      {
         return false;
      } else {
         return true;
      }
   }

   public int getSolution1()
   {
      this.s1 = (int)(-b + Math.sqrt(this.d))/(2*a);
      return this.s1;
   }

   public int getSolution2()
   {
      this.s2 = (int)(-b - Math.sqrt(this.d))/(2*a);
      return this.s2;
   }

   public static void main (String args[])
   {
      Scanner input = new Scanner (System.in);
      for (int k = 1; k <= 3; k++)
      {
         System.out.print("Enter a, b, and c: ");
         int a = input.nextInt();
         int b = input.nextInt();
         int c = input.nextInt();

         QuadraticEquation q = new QuadraticEquation(a,b,c);

         if (q.noSolution() == true)
         {
            System.out.println("No real solution");
         } else if (q.equalRoots() == true) {
            System.out.println("The only solution is: " + q.getSolution1());
         } else {
            System.out.println("The two real solutions are: ");
            System.out.println(q.getSolution1());
            System.out.println(q.getSolution2());
         } //Else
      } //For
   } //Main
} //QuadraticEquations

I have this code, and I'm trying to get the factors of an equation. If the solutions aren't integers, then it returns "No real solution". If the factors are the same, then it returns the only factor. If there are two factors, then it should return two factors. It works when there's only 1 factor, (ex. when a=1, b=2, and c=1), but it doesn't work when the equation is unfactorable, and when there's two solutions, it only returns 1.

Here is current incorrect output:

 ----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 1 1
The only solution is: 0
Enter a, b, and c: 
 ----jGRASP: process ended by user.

 ----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 -5 6
The only solution is: 2
Enter a, b, and c: 
 ----jGRASP: process ended by user.

 ----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 2 1
The only solution is: -1
Enter a, b, and c: 
 ----jGRASP: process ended by user.

 ----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 -4 4
The only solution is: 2
Enter a, b, and c:

EDIT:

Thanks to bcsb1001, I've revised my code, and it works.

import java.util.Scanner;

public class QuadraticEquation
{
   private int a, b, c, d;

   public QuadraticEquation(int a, int b, int c)
   {
      this.a = a;
      this.b = b;
      this.c = c;
      this.d = findDiscriminant(a, b, c);
   }

   public int findDiscriminant(int a, int b, int c)
   {
      return (int)Math.pow(b,2) - (4*a*c);
   }

   public boolean equalRoots()
   {
      return this.d == 0;
   }

   public boolean noSolution()
   {
      return this.d < 0;
   }

   public int getSolution1()
   {
      return (int)(-b + Math.sqrt(this.d))/(2*a);
   }

   public int getSolution2()
   {
      return (int)(-b - Math.sqrt(this.d))/(2*a);
   }

   public static void main (String args[])
   {
      Scanner input = new Scanner (System.in);
      for (int k = 1; k <= 3; k++)
      {
         System.out.print("Enter a, b, and c: ");
         int a = input.nextInt();
         int b = input.nextInt();
         int c = input.nextInt();

         QuadraticEquation q = new QuadraticEquation(a,b,c);

         if (q.noSolution() == true)
         {
            System.out.println("No real solution");
         } else if (q.equalRoots() == true) {
            System.out.println("The only solution is: " + q.getSolution1());
         } else {
            System.out.println("The two real solutions are: ");
            System.out.println(q.getSolution1());
            System.out.println(q.getSolution2());
         } //Else
      } //For
   } //Main
} //QuadraticEquations

Btw, I was forced to do certain things like create "findDiscriminant" because the worksheet forced me to. It gave me the main method and I was supposed to figure out everything from there.


Solution

  • Your basic code logic is faulty.

    equalRoots() should be called hasOnlyOneSolution and should be exactly
    return this.d == 0.

    noSolution() should simply be return this.d < 0.

    And note that you never call findDiscriminant which causes d to remain 0.

    This is the basic mathematical distinction you make when working this problem out on paper. And the exact same logic has to be done in code.


    Final note: you are mixing up fields and local variables like crazy. Why does findDiscriminant return something AND set d. And why does it get passed a, b and c instead of simply accessing the fields of the same name with hopefully the same content?


    public class QuadraticEquation {
        private int a, b, d;
    
        public QuadraticEquation(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.d = b * b - (4 * a * c);
        }
    
        public boolean hasOnlyOneSolution() {
            return d == 0;
        }
    
        public boolean noSolution() {
            return d < 0;
        }
    
        public int getSolution1() {
            return (int) (-b + Math.sqrt(this.d)) / (2 * a);
        }
    
        public int getSolution2() {
            return (int) (-b - Math.sqrt(this.d)) / (2 * a);
        }
    
        // + your main method
    }