美文网首页stanford CS106a assignment
CS106A assignment2 --problem1 Qu

CS106A assignment2 --problem1 Qu

作者: EarsternRain | 来源:发表于2016-06-30 23:47 被阅读36次

    /* TODO: A program that the roots of the quadratic equation
    /
    import acm.program.
    ;

    public class QuadraticFormula extends ConsoleProgram {
        public int a,b,c;
        public double discriminant;
        public double root1,root2;
        public void run() {
            getNumber();
            getRoots();
        }
    
        private void getNumber() {
            println("This program needs three intengers:");
            a = readInt("Enter first int a:");
            b = readInt("Enter second int b:");
            c = readInt("Enter third int c:");
        }
    
        private void getRoots() {
            getDiscriminant();
            showRoots();
        }
    
        private double getDiscriminant() {
            discriminant = b*b - 4*a*c;
            return discriminant;
        }
    
        private void showRoots() {
            if(discriminant < 0){
                println("There are no real roots");
            }else if(discriminant ==0 ){
                root1 = (-b)/(2*a);
                println("there is one root:"+ root1);
            }else {
                root1 = (-b+ Math.sqrt(discriminant))/(2*a);
                root2 = (-b- Math.sqrt(discriminant))/(2*a);
                println("one root is:"+root1);
                println("another root is:"+root2);
            }
        }
    }

    相关文章

      网友评论

        本文标题:CS106A assignment2 --problem1 Qu

        本文链接:https://www.haomeiwen.com/subject/ndefjttx.html