JAVA作业一份 超高分+高附加分

来源:百度知道 编辑:UC知道 时间:2024/05/31 08:50:17
In this homework we consider any quadratic polynomial that may or may not have roots that are
double numbers (i.e. b2 - 4ac is negative), hence its root should not be computed.
Write a class, called QuadraticPolynomialTUI, which uses the class above, and in a command
(similar to the RectangleViewer on page 153 of the book) displays the polynomial followed by its
roots if it had any or by a message that the polynomial has no real roots. If the roots of the polynomial
are equal it should display only one root. You do this for all polynomials in the test plan. The
test plan should include polynomials which have roots as well as no real roots. And there should
be polynomials where a > 1, or a < -1, as well as polynomial which have only one root.
About the display of the polynomial. It should be written so that it indeed looks like a polymomial.
Example: given a = -1; b = 1; c = -5. The display should be of the form :
- X^2 + x - 5

你的题目提到的参考文件和测试资料都没有,不太清楚具体要弄成什么样。主要的功能给你实现了:

import java.util.Scanner;

public class QuadraticPolynomialTUI {
public double[] findRoots(double a, double b, double c) {
double rootSquare = b*b - 4*a*c;
if(rootSquare<0) return null;

if(rootSquare==0)
return new double[] {-b/2/a};
else
return new double[] {(-b+Math.sqrt(rootSquare))/2/a, (-b-Math.sqrt(rootSquare))/2/a};
}

public String formatPolynomial(double a, double b, double c) {
return (a<0?"- "+(-a):""+a) + "X^2" + (b<0?" - "+(-b):" + "+b) + "X" + (c<0?" - "+(-c):" + "+c);
}
}

class PolynomialStart {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

QuadraticPolynomialTUI qptui = new QuadraticPolynomialTUI();

do {
System.out.print("I