Sunteți pe pagina 1din 5

package Chapter_09;

import ToolKit.QuadraticEquation;
import java.util.Scanner;
/**
* Chapter 9 Exercise 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
* The methods named getRoot1() and getRoot2() for returning two
roots of the equation
*
* These methods are useful only if the discriminant is non-negative. Let
these methods return 0 if the discriminant is negative.
* Draw the UML diagram for the class and then implement the class.
* 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.
*
* Created by Luiz Arantes Sa on 9/2/14.
*/
public class Exercise_10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
QuadraticEquation equation = new QuadraticEquation(a, b, c);
double discriminant = equation.getDiscriminant();
if (discriminant > 0) {
System.out.println("The roots are " + equation.getRoot1()
+ " and " + equation.getRoot2());
} else if (discriminant == 0) {
System.out.println("The root is " + equation.getRoot1());
} else {
System.out.println("The equation has no roots");
}
}
}

(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 *
* b2 - 4ac.
*
* The methods named getRoot1() and getRoot2() for returning two roots of
the *
* equation
*
* r1 = (-b + (b^2 - 4ac)) / 2a and r2 = (-b - (b2 - 4ac)) / 2a
*
*
*
* These methods are useful only if the discriminant is nonnegative. Let
these *
* methods return 0 if the discriminant is negative.
*
* Draw the UML diagram for the class and then implement the class. 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. *
***************************************************************************
******/
import java.util.Scanner;
public class Exercise_09_10 {
/** Main method */
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter values for a, b, and c
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
// Create a QuadraticEquation object
QuadraticEquation quadraticEquation = new
QuadraticEquation(a, b, c);
// Compute the real roots of the quadriatic equation if any.
System.out.print("The equation has ");
if (quadraticEquation.getDiscriminant() < 0)
System.out.println("no real roots");
else if (quadraticEquation.getDiscriminant() > 0) {
System.out.println("two roots " +
quadraticEquation.getRoot1() +
" and " + quadraticEquation.getRoot2());
}
else {
System.out.println("one root " +
(quadraticEquation.getRoot1() > 0 ?
quadraticEquation.getRoot1() :
quadraticEquation.getRoot2()));
}
}
}

import java.util.Scanner;
02
03 class QuadraticEquation {
04 private int a;
05 private int b;
06 private int c;
07
08 //Default constructor
09 QuadraticEquation() {
10 a = 0;
11 b = 0;
12 c = 0;
13 }
14 //user specifies coefficients
15 QuadraticEquation(int newA, int newB, int newC) {
16 a = newA;
17 b = newB;
18 c = newC;
19 }
20 //Get methods for coefficients
21 public int getA() {
22 return a;
23 }
24 public int getB() {
25 return b;
26 }
27 public int getC() {
28 return c;
29 }
30 //Return discriminant (b^2-4ac)
31 public double getDiscriminant() {
32 return (Math.pow(b, 2) - 4 * a * c);
33 }
34 //Return the roots
35 public double getRoot1() {
36 return (-b + Math.sqrt(getDiscriminant()) / (2 * a));
37
38 }
39 public double getRoot2() {
40 return (-b - Math.sqrt(getDiscriminant()) / (2 * a));
41 }
42 }
43
44 public class TestQuadraticEquation {
45 public static void main(String[] args) {
46 Scanner input = new Scanner(System.in);
47 System.out.println("Enter coefficients a, b, and c in order: ");
48 int a = input.nextInt();
49 int b = input.nextInt();
50 int c = input.nextInt();
51 QuadraticEquation qe = new QuadraticEquation(a, b, c);
52 if (qe.getDiscriminant() > 0) {
System.out.println("The roots of the equation are " +
53
qe.getRoot1() + " and " + qe.getRoot2());
54 }
55 else {
56 System.out.println("The equation has no roots.");
57 }
58 }
59 }

S-ar putea să vă placă și