This is the source code I have so far. Im trying to get it to run. Please commen
ID: 3591587 • Letter: T
Question
This is the source code I have so far. Im trying to get it to run.
Please comment on what is doing what.
In Class Programming Assignment 9: Quadratic Equation C201 Fall 2017 Problem Description: 9.10 (Algebra: quadratic equations) Design a class named QuadraticEquation for a quadratic equation ax2+bx+x=0. The class contains: Private data fields a , b , and c that represent three coefficients. A constructor for the arguments for a , b , and c . Three getter methods for a , b , and c . A method named getDiscriminant() that returns the discriminant, which is b24ac. The methods named getRoot1() and getRoot2() for returning two roots of the equation These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative. Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display “The equation has no roots.” See Programming Exercise 3.1 for sample runs.
QuadraticEquation class
/*
* Programmer:
* Date: 10/12/2017
* Purpose: Implement solution to exercise 9.1
0 from the Liang textbook.
*/
package testquadraticequation;
/**
*
*
*/
public class QuadraticEquation {
private double a;
private double b;
private double c;
QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}//end constructor
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getDiscriminant() {
return Math.pow(b, 2.0)
-
4*a*c;
}
public double getRoot1() {
return ((
-
b)+Math.sqrt(this.getDiscriminant()))/(2*a);
}
public double getRoot2() {
return ((
-
b)
-
Math.sqrt
(this.getDiscriminant()))/(2*a);
}
}
In Class Programming Assignment 9: Quadratic Equation
TestQuadraticEquation class
/*
* Programmer:
* Date: 10/12/2017
* Purpose: Implement solution to exercise 9.10 from the Liang textbook.
*/
package testquadraticequation;
import java.util.Scanner;
/**
*
* @
*/
public class TestQuadraticEquation {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double a, b, c;
Scanner inp
ut = new Scanner(System.in);
System.out.println("This program solves the quadratic equation.");
System.out.print("Enter a value for a: ");
a = input.nextDouble();
System.out.print("Enter a value for b: ");
b = input.
nextDouble();
System.out.print("Enter a value for c: ");
c = input.nextDouble();
QuadraticEquation e1 = new QuadraticEquation(a,b,c);
if(e1.getDiscriminant() == 0)
System.out.printf("This equation has only one s
olution, which is
%3.2f
n", e1.getRoot1());
else if(e1.getDiscriminant() < 0)
System.out.println("This equation has no real solutions.
n");
else
System.out.printf("Here are the two roots: %3.2f and %3.2f
n",
e1.getRo
ot1(), e1.getRoot2());
System.out.printf("The discriminant is %3.2f
n",
e1.getDiscriminant());
}
}
output-TestQuadraticEquation (run) × ram solves the quadratie equation Enter a value for a: 1 Enter a value tor b: 2 Enter a value for c:1 This equation has only one solution, which is -1.00 The discriminant is 0.00 BUILD SUCCESSFUL (total time: 9 seconds)Explanation / Answer
QuadraticEquation.java
package org.students;
public class QuadraticEquation {
//Declaring instance variables
private double a;
private double b;
private double c;
//Parameterized constructor
QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
} // end constructor
//getters and setters
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
//This method will calculate the discriminant
public double getDiscriminant() {
return Math.pow(b, 2.0) - 4 * a * c;
}
//Calculating the roots
public double getRoot1() {
return ((-b) + Math.sqrt(this.getDiscriminant())) / (2 * a);
}
//Calculating the roots
public double getRoot2() {
return ((-b) + Math.sqrt(this.getDiscriminant())) / (2 * a);
}
}
__________________
TestQuadraticEquation.java
import java.util.Scanner;
public class TestQuadraticEquation {
public static void main(String[] args) {
//Declaring variables
double a, b, c;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner input = new Scanner(System.in);
//Getting the input entered by the user
System.out.println("This program solves the quadratic equation.");
System.out.print("Enter a value for a: ");
a = input.nextDouble();
System.out.print("Enter a value for b: ");
b = input.nextDouble();
System.out.print("Enter a value for c: ");
c = input.nextDouble();
//Creating an QuadraticEquation class object by passing the user entered inputs as arguments
QuadraticEquation e1 = new QuadraticEquation(a, b, c);
//Based on the discriminant display the corresponding message
if (e1.getDiscriminant() == 0)
System.out.printf("This equation has only one solution, which is %.2f ", e1.getRoot1());
else if (e1.getDiscriminant() < 0)
System.out.println("This equation has no real solutions. ");
else
System.out.printf("Here are the two roots: %.2f and %.2f ", e1.getRoot1(), e1.getRoot2());
//Displaying the Discriminant value
System.out.printf("The discriminant is %.2f ", e1.getDiscriminant());
}
}
__________________
Output#1:
This program solves the quadratic equation.
Enter a value for a: 1
Enter a value for b: 2
Enter a value for c: 1
This equation has only one solution, which is -1.00
The discriminant is 0.00
________________
Output#2:
This program solves the quadratic equation.
Enter a value for a: 3
Enter a value for b: 2
Enter a value for c: 5
This equation has no real solutions.
The discriminant is -56.00
___________________
Output#3:
This program solves the quadratic equation.
Enter a value for a: 2
Enter a value for b: 8
Enter a value for c: 8
This equation has only one solution, which is -2.00
The discriminant is 0.00
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.