In: Computer Science
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public int constant = 0; /** * This a default constructor to create object with no parameter */ public Quadratic() { } /** * This a parameter constructor to create object with parameter * @param coeffX2 This is the coefficient of X^2 in quadratic equn * @param coeffX This is the coefficient of X in quadratic equn * @param constant This is the constant in quadratic equn */ public Quadratic(int coeffX2, int coeffX, int constant) { this.coeffX2 = coeffX2; this.coeffX = coeffX; this.constant = constant; } /** * This a getter method for variable coeffX2 * @return int This returns the coefficient of x^2 */ public int getCoeffX2() { return coeffX2; } /** * This a setter method for variable coeffX2 * @param coeffX2 This sets the coefficient of X^2 of quadratic equation */ public void setCoeffX2(int coeffX2) { this.coeffX2 = coeffX2; } /** * This a getter method for variable coeffX * @return int This returns the coefficient of x */ public int getCoeffX() { return coeffX; } /** * This a setter method for variable coeffX * @param coeffX This sets the coefficient of X of quadratic equation */ public void setCoeffX(int coeffX) { this.coeffX = coeffX; } /** * This a getter method for constant term * @return int This returns the constant term */ public int getConstant() { return constant; } /** * This a setter method for constant * @param constant This sets the constant of quadratic equation */ public void setConstant(int constant) { this.constant = constant; } /** * This a toString method * @return String This return a string showing equation in form ax^2+bx+c */ @Override public String toString() { return "Quadratic ["+coeffX2 + "X^2 +" + coeffX + "X +" + constant +"]"; } /** * This is a method to find slope at a given x * @param a This is the point where slope is to be calculated * @return int This returns the slope at point x */ public int findSlope(int a) { int slope = 2*coeffX2*a + coeffX; return slope; } }
DRIVER.java
package pack2; public class Driver { public static void main(String[] args) { Quadratic q1 = new Quadratic(5,7,10); System.out.println(q1); Quadratic q2 = new Quadratic(); q2.setCoeffX2(4); q2.setCoeffX(8); q2.setConstant(20); System.out.println("Coeff of X^2 : "+ q2.getCoeffX2()); System.out.println("Coeff of X : "+ q2.getCoeffX2()); System.out.println("Constant Term : "+ q2.getConstant()); System.out.println(q2); System.out.println("Slope at x = 5 is : "+q2.findSlope(2)); } }
Here are the 2 updated class, ======================================================================= package pack2;
/** * This is a program for defining a quadratic equation * * @author sonik */ //In this class add Comparable interface. public class Quadratic implements Comparable<Quadratic> { public int coeffX2 = 0; public int coeffX = 0; public int constant = 0; /** * This a default constructor to create object with no parameter */ public Quadratic() { } /** * This a parameter constructor to create object with parameter * * @param coeffX2 This is the coefficient of X^2 in quadratic equn * @param coeffX This is the coefficient of X in quadratic equn * @param constant This is the constant in quadratic equn */ public Quadratic(int coeffX2, int coeffX, int constant) { this.coeffX2 = coeffX2; this.coeffX = coeffX; this.constant = constant; } /** * This a getter method for variable coeffX2 * * @return int This returns the coefficient of x^2 */ public int getCoeffX2() { return coeffX2; } /** * This a setter method for variable coeffX2 * * @param coeffX2 This sets the coefficient of X^2 of quadratic equation */ public void setCoeffX2(int coeffX2) { this.coeffX2 = coeffX2; } /** * This a getter method for variable coeffX * * @return int This returns the coefficient of x */ public int getCoeffX() { return coeffX; } /** * This a setter method for variable coeffX * * @param coeffX This sets the coefficient of X of quadratic equation */ public void setCoeffX(int coeffX) { this.coeffX = coeffX; } /** * This a getter method for constant term * * @return int This returns the constant term */ public int getConstant() { return constant; } /** * This a setter method for constant * * @param constant This sets the constant of quadratic equation */ public void setConstant(int constant) { this.constant = constant; } /** * This a toString method * * @return String This return a string showing equation in form ax^2+bx+c */ @Override public String toString() { return "Quadratic [" + coeffX2 + "X^2 +" + coeffX + "X +" + constant + "]"; } /** * This is a method to find slope at a given x * * @param a This is the point where slope is to be calculated * @return int This returns the slope at point x */ public int findSlope(int a) { int slope = 2 * coeffX2 * a + coeffX; return slope; } @Override public int compareTo(Quadratic o) { if (coeffX2 == o.coeffX2) { if (coeffX == o.coeffX) { return constant - o.constant; } else { return coeffX - o.coeffX; } } else { return coeffX2 - o.coeffX2; } } }
====================================================================
package pack2;
import java.util.Arrays; public class Driver { public static void main(String[] args) { Quadratic q1 = new Quadratic(5, 7, 10); System.out.println(q1); Quadratic q2 = new Quadratic(); q2.setCoeffX2(4); q2.setCoeffX(8); q2.setConstant(20); System.out.println("Coeff of X^2 : " + q2.getCoeffX2()); System.out.println("Coeff of X : " + q2.getCoeffX2()); System.out.println("Constant Term : " + q2.getConstant()); System.out.println(q2); System.out.println("Slope at x = 5 is : " + q2.findSlope(2)); //In the driver program create a few objects and compare them Quadratic q3 = new Quadratic(4, 9, 7); Quadratic q4 = new Quadratic(12, 9, 7); Quadratic q5 = new Quadratic(1, 1, 1); System.out.println("q1.compareTo(q2) = " + q1.compareTo(q2)); System.out.println("q2.compareTo(q1) = " + q2.compareTo(q1)); System.out.println("q3.compareTo(q2) = " + q3.compareTo(q2)); //then create a list of those objects and sort them Quadratic [] quadratics = {q1,q2,q3,q4,q5}; System.out.println("\nBefore Sorting..."); for(Quadratic q: quadratics){ System.out.println(q); } System.out.println("Sorting the quadratics now ..."); Arrays.sort(quadratics); System.out.println("After Sorting..."); for(Quadratic q: quadratics){ System.out.println(q); } } }
====================================================================