0

I need to be able to check if variables a, b, and c from one constructor are equal to a, b, and c from another constructor (Quadratic q). I get the feeling that my copy constructor might be wrong and this could potentially be the problem as well. I'm trying to accomplish this through the last method. Ignore what I have in there now, I'm sure it's completely wrong. I'd appreciate any help I can get. This is what I have so far:

import java.util.Scanner;

public class Quadratic
{
    // instance variables - replace the example below with your own
    private double a;
    private double b;
    private double c;

    /**
     * Assignment constructor
     */
    public Quadratic(double aIn, double bIn, double cIn)
    {
        a=aIn; b=bIn; c=cIn;
    }

    /**
     * Copy constructor of class Quadratic for objects 
     */
    public Quadratic(Quadratic q)
    {
        a=q.a;b=q.b;c=q.c;
    }

    /**
     * Default constructor - uses Scanner class.
     */
    public Quadratic()
    {
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a: ");
       double a=in.nextDouble();
       System.out.println("Enter b: ");
       double b= in.nextDouble();
       System.out.println("Enter c: ");
       double c= in.nextDouble();
    }

    /**
     * Returns an expression for the quadratic, i.e., 1.0x^2 + 3.0x + 2.0
     */
    public String toString()
    {
        return a+"x^2 + "+b+"x + " + c;
    }

    /**
     * Returns true if a, b, and c all match for this & q; false otherwise.
     */
    public boolean equals(Quadratic q)
    {

        if (Quadratic(q).equalsQuadratic(a,b,c))
            return true;
        else
            return false;
    }

}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
J.Cole
  • 29
  • 6

1 Answers1

1

Hint. If you use IntelliJ IDEA, then right click and Generate... -> equals() and hashCode();

public final class Quadratic {

    private final double a;
    private final double b;
    private final double c;

    public Quadratic(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public Quadratic(Quadratic quadratic) {
        this(quadratic.a, quadratic.b, quadratic.c);
    }

    @Override
    public String toString() {
        return a + "x^2 + " + b + "x + " + c;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof Quadratic))
            return false;
        Quadratic quadratic = (Quadratic)obj;
        return Double.compare(quadratic.a, a) == 0 &&
                Double.compare(quadratic.b, b) == 0 &&
                Double.compare(quadratic.c, c) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b, c);
    }
}
  1. Use final class variable if it is possible.
  2. Use this to access class variable, but not mehtod parameter
  3. Scanner should be closed
  4. Do not use Scanner in constructor, this is not belong to the class. Use it in the client code and then call constructor
  5. double values should be compared with Double.compare
  6. equals() + hashCode() - this is must have (well, hashCode() could be useless if you not plan to use this class as a key in hash map, but still - this should be automatic)
  7. When implement equals() + hashCode() manually, do pay attention on specification.
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35