Chapter 8 lab: Design a class called QuadraticEquation for a quadratic equation
ID: 3714689 • Letter: C
Question
Chapter 8 lab: Design a class called QuadraticEquation for a quadratic equation
Chapter 8 lab 1. Design a class called QuadraticEquation for a quadratic equation Here is the UML for the class QuadraticEquation -double a double b +QuadraticEquation(double a, double b, double c) +getA): double tgetB(): double +setA(double a): void +setB(double b): void +setC(double c):void getDiscriminant(): double// returns the discriminant, returns zero if it is negative +getRoot10: double//return zero if the discriminant is negative +getRoot20: double//return zero if the discriminant is negative. +equals(QuadraticEquation e):boolean tgetEquation): String. This method needs to return the equation in the form of: example: 3x2 +toStringl: Stri 4x 7 0 2. 3. Create a text file using notepad to include the coefficients a, b, and c for 10 different equations Create a driver class to do the following: a. Declare an array of QuadraticEquation with the size 10 b. Create a method called fillArray(QuadraticEquation equations). This method reads the info for each equation from a file, then creates an object of QuadraticEquation, then stores it in the array Write the method called getresult(QuadraticEquation equations) that for(int i =0 ; K equations. length, i++) c. i. Calls the method getEquation to display the equation for each object in the array ii. Call the method getDiscriminant for each equation in the array ii. Call the method getRoot1 for each equation in the array iv. Call the method getRoot2 for each equation in the array v. Display the result for each object in the arrayExplanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
QuadraticEquation.java
------------------------
public class QuadraticEquation {
private double a;
private double b;
private double c;
public QuadraticEquation(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public double getDiscriminant()
{
double d = b * b - 4 * a * c;
if(d < 0)
return 0;
else
return d;
}
public double getRoot1()
{
double d = b * b - 4 * a * c;
if(d < 0)
return 0;
else
return (-b + Math.sqrt(d)) / (2 * a);
}
public double getRoot2()
{
double d = b * b - 4 * a * c;
if(d < 0)
return 0;
else
return (-b - Math.sqrt(d)) / (2 * a);
}
public boolean equals(QuadraticEquation e)
{
if(a == e.a && b == e.b && c == e.c)
return true;
else
return false;
}
public String getEquation()
{
String s= "";
if(a != 0)
{
s += a +"x^2 ";
}
if(b != 0)
{
if(b == 1)
s += "+ x ";
else if(b == -1)
s += "- x ";
else
{
if(b < 0)
s += "- " + (-b) + "x ";
else
s += "+ " + b + "x ";
}
}
if(c != 0)
{
if(c < 0)
s += "- " + (-c);
else
s += "+ " + c;
}
s += " = 0";
return s;
}
public String toString()
{
return getEquation();
}
}
QuadraticDriver.java
---------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class QuadraticDriver {
public static void main(String[] args) {
QuadraticEquation[] equations = new QuadraticEquation[10];
try {
fillArray(equations);
getResult(equations);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void fillArray(QuadraticEquation[] equations) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
String filename;
System.out.print("Enter input filename: ");
filename = keyboard.next();
Scanner infile = new Scanner(new File(filename));
int i = 0;
while(infile.hasNextLine())
{
equations[i] = new QuadraticEquation(infile.nextDouble(), infile.nextDouble(), infile.nextDouble());
i++;
}
}
public static void getResult(QuadraticEquation[] equations)
{
System.out.printf("%-40s %-15s %-10s %-10s ", "Equation", "Discriminant", "Root1", "Root2");
for(int i = 0 ;i < equations.length; i++)
{
QuadraticEquation e = equations[i];
System.out.printf("%-40s %-15.2f %-10.2f %-10.2f ", e.getEquation(), e.getDiscriminant(), e.getRoot1(), e.getRoot2());
}
}
}
input file: equations.txt
------
3 4 7
1 1 -6
2 5 2
1 3 -4
1 2 -24
3 9 8
1 -6 6
1 7 12
4 12 9
1 0 25
output
-------
Enter input filename: equations.txt
Equation Discriminant Root1 Root2
3.0x^2 + 4.0x + 7.0 = 0 0.00 0.00 0.00
1.0x^2 + x - 6.0 = 0 25.00 2.00 -3.00
2.0x^2 + 5.0x + 2.0 = 0 9.00 -0.50 -2.00
1.0x^2 + 3.0x - 4.0 = 0 25.00 1.00 -4.00
1.0x^2 + 2.0x - 24.0 = 0 100.00 4.00 -6.00
3.0x^2 + 9.0x + 8.0 = 0 0.00 0.00 0.00
1.0x^2 - 6.0x + 6.0 = 0 12.00 4.73 1.27
1.0x^2 + 7.0x + 12.0 = 0 1.00 -3.00 -4.00
4.0x^2 + 12.0x + 9.0 = 0 0.00 -1.50 -1.50
1.0x^2 + 25.0 = 0 0.00 0.00 0.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.