简体   繁体   中英

calling mutator from within a method of another class

Im trying understand what am I doing wrong here

import java.lang.Math

public class QuadraticEquation {
        public static Roots findRoots(double a, double b, double c) {
            double d = b*b-4*a*c;
            double x1 = (-b + Math.sqrt(d))/2*a;
            double x2 = (-b - Math.sqrt(d))/2*a;
            Roots( x1, x2);

        }

        public static void main(String[] args) {
            Roots roots = QuadraticEquation.findRoots(2, 10, 8);
            System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
        }
    }

    class Roots {
        public final double x1, x2;

        public Roots(double x1, double x2) {         
            this.x1 = x1;
            this.x2 = x2;
        }
    }

Obviously it gives me a error: cannot find symbol on last line of public static Roots findRoots but I don't understand what other way of calling the mutator is there

What's wrong with replacing

Roots(x1, x2);

with

return new Roots(x1, x2);

?

Also, I don't know what your understanding of a "mutator" is, but the keyword you might want to look up in your Java beginners guide is "constructor".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM