Question

In: Computer Science

In this class add Comparable interface. In the driver program create a few objects and In...

 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));
        }


}

Solutions

Expert Solution

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);
        }



    }


}

====================================================================


Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT