2016-11-03 1 views
0

Ich bin ein neuer Java-Programmierer und ich versuche, ein Programm zu schreiben, das die Wurzeln einer quadratischen Gleichung findet, indem Sie die roots() -Methode in dieser Klasse implementieren.Implementierung von Methoden mit Parametern in Java

Ich glaube, ich habe herausgefunden, wie die Gleichung zu implementieren, aber die return-Anweisung sagt: Error-Typenkonflikt: kann nicht von Doppel konvertiert Set

Wie würde ich diesen Fehler beheben?

Vielen Dank!

package warmup; 

import java.util.Set; 

public class Quadratic { 

/** 
* Find the integer roots of a quadratic equation, ax^2 + bx + c = 0. 
* @param a coefficient of x^2 
* @param b coefficient of x 
* @param c constant term. Requires that a, b, and c are not ALL zero. 
* @return all integers x such that ax^2 + bx + c = 0. 
*/ 
public static Set<Integer> roots(int a, int b, int c) { 

    //my code so far 
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a); 
    return q; 
} 


/** 
* Main function of program. 
* @param args command-line arguments 
*/ 
public static void main(String[] args) { 
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:"); 
    Set<Integer> result = roots(1, -4, 3); 
    System.out.println(result); 
    } 


} 
+0

Der Fehler ist selbsterklärend: Sie erklären, dass die Methode ' zurückgibt' in der Signatur des Verfahrens, aber Sie kehren ein ' doppeltes ". – alfasin

+0

Es ist erforderlich, Set zu verwenden? – Thrasher

+0

Ich nehme an, Sie haben den Teil weggelassen, in dem Sie auch '-b - ...' berechnen und beide Werte gleichzeitig ausgeben. Das sollte wirklich ein 'Set ' sein. – Grexis

Antwort

0

Warum verwenden Sie Set<Integer> als Rückgabetyp, sondern können Double verwenden. Ersetzen Sie Set<Integer> durch Double und Ihr Problem ist gelöst.

Hinweis: - Sie versuchen doppelt zurück, aber Ihre Methode Rückgabetyp ist ....

+1

Während das stimmt - beantwortet es die Frage nicht und sollte es sein als Kommentar gepostet. – alfasin

0
public class Quadratic { 

public static double roots(int a, int b, int c) { 

    //my code so far 
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a); 
    return q; 
} 
public static void main(String[] args) { 
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:"); 
    double result = roots(1, -4, 3); 
    System.out.println(result); 
    } 

}

+0

Dies gibt nur ein Ergebnis zurück, aber wenn Sie Root berechnen, erhalten Sie möglicherweise mehr als ... – alfasin

0

Sie Rückkehr eine doppelte Variable anstelle einer Set Objekt in der Wurzeln Funktion. Daher die Typabweichung. Ändern Sie es wie folgt:

public static double roots(int a, int b, int c) { 

double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a); 
return q; 
} 

Stellen Sie sicher, das Ergebnis dieser Funktion auch als ein Doppel zu erhalten. dies ändern:

Set<Integer> result = roots(1, -4, 3); 

zu:

double result = roots(1, -4, 3); 

Das sollte funktionieren.

+0

Dies gibt nur ein Ergebnis zurück, aber wenn Sie Root berechnen, erhalten Sie möglicherweise mehr als ... – alfasin

2

Der folgende Code enthält einige Korrekturen an den Code in der Frage, die Kommentare im Code für weitere Erläuterungen siehe:

public static Set<Double> roots(int a, int b, int c) { 
    Double x = Math.sqrt(Math.pow(b, 2) - 4 * a * c); 
    Set<Double> result = new HashSet<>(); // return a set that contains the results 
    result.add((-b + x)/ 2 * a); // -b should be divided by 2a as well 
    result.add((-b - x)/ 2 * a); // -b should be divided by 2a as well 
    return result; 
} 

public static void main(String[] args) { 
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:"); 
    Set<Double> result = roots(1, -4, 3); // the returned type is Set<Double> not Set<Integer> 
    System.out.println(result); 
} 

OUTPUT

For the equation x^2 - 4x + 3 = 0, the possible solutions are: 
[1.0, 3.0] 
+0

Eine Antwort, die auf das Problem kommt anstatt einfach den Rückgabetyp zu verdoppeln! Die Eingabe sollte wahrscheinlich validiert werden, um sicherzustellen, dass "a" (unter anderem) nicht null ist. Ich bin mir nicht sicher, wie Java die Quadratwurzel negativer Zahlen behandelt, um ehrlich zu sein. – Grexis

+0

wie angegeben, es könnte kein Ergebnis sein, sollte man überprüfen Math.pow (b, 2)> = 4 * a * c' – Turo

+0

@Grexis es wird eine Ausnahme werfen, wie Sie es in solchen Fällen erwarten würde. Der API-Vertrag zwischen Ihnen und dem Benutzer könnte bestimmt sein, diese Ausnahmen zu "schlucken" und zum Beispiel ein leeres Set zurückzugeben, aber ich mag den Ansatz "fail fast" und erlaubt dem Benutzer zu entscheiden, was er mit der Ausnahme machen möchte. Beachten Sie, dass die meisten Java-Bibliotheken den gleichen Ansatz haben! – alfasin

1

Der Rückgabetyp Ihres Methode ist Set<Integer>. Die Rückgabe eines Set bedeutet, dass Sie null, eins oder mehrere Elemente zurückgeben können. Dies ist angebracht, da eine quadratische Gleichung im allgemeinen Fall mehrere Wurzeln hat.

<Integer> bedeutet, dass die Elemente der Menge, die Sie zurückgeben, Ganzzahlen sein müssen, was seltsam ist, da die Wurzeln einer quadratischen Gleichung oft reelle Zahlen sind und manchmal komplexe Zahlen sind. Ich würde raten, was Sie wirklich wollen, ist Set<Double>.

Wenn Sie wirklich ein einzelnes Element zurückgeben möchten, müssen Sie dies tun, indem Sie einen Satz zurückgeben, dem Sie den betreffenden Stamm hinzugefügt haben. Rufen Sie Collections.singleton() ist eine bequeme Möglichkeit, dies zu tun.

lesenswert:

https://docs.oracle.com/javase/6/docs/api/java/util/Set.html

https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#singleton(T)

0
import java.util.Set; 
    import java.util.HashSet; 

    public class Quadratic { 

/** 
* Find the integer roots of a quadratic equation, ax^2 + bx + c = 0. 
* @param a coefficient of x^2 
* @param b coefficient of x 
* @param c constant term. Requires that a, b, and c are not ALL zero. 
* @return all integers x such that ax^2 + bx + c = 0. 
*/ 
    public static Set<Double> roots(int a, int b, int c) { 
    Set<Double> result=new HashSet<>(); 
    //my code so far 
    double p = (-b - (Math.sqrt(Math.pow(b, 2)-4*a*c)))/2*a; 
    result.add(p); 
    double q = (-b + (Math.sqrt(Math.pow(b, 2)-4*a*c)))/2*a; 
    result.add(q); 
    return result; 
} 


/** 
* Main function of program. 
* @param args command-line arguments 
*/ 
public static void main(String[] args) { 
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:"); 
    Set<Double> result = roots(1, -4, 3); 
    System.out.println(result); 
    } 


} 
Verwandte Themen