简体   繁体   中英

Solving quadratic equation USING METHODS, java

I have a java program written for solving the quadratic equation but the assignment requires me to have methods for each of the tasks: displaying the equation, determining if the equation has real solutions, calculating a solution, and displaying the solutions if they exist. I have methods for everything except for checking if the solutions are real except my methods are saying that they are undefined. Can someone please help me format my methods, I don't quite know how to implement them? here is the code I have thus far:

import java.util.Scanner;
public class QuadraticFormula {

public static void main(String[] args)
{
    //Creating scanner and variables
    Scanner s = new Scanner(System.in);
    System.out.println("Insert value for a: ");
    double a = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for b: ");
    double b = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for c: ");
    double c = Double.parseDouble(s.nextLine());

    //Display format for negatives
    displayEquation(double a, double b, double c);{
    if (b > 0 && c > 0 ){
        System.out.println(a + "x^2 + " + b + "x + " + c + " =0");}
    if (b < 0 && c > 0 ){
        System.out.println(a + "x^2 " + b + "x + " + c + " =0");}
    if (b > 0 && c < 0 ){
        System.out.println(a + "x^2 + " + b + "x " + c + " =0");}
    if (b < 0 && c < 0 ){
        System.out.println(a + "x^2 " + b + "x " + c + " =0");}
    s.close();
    }
    //The work/formula
    private static double calculateSolution(double a, double b, double c); 
    {
    double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return answer1;
    }

    private static double calculateSolution(double a, double b, double c); 
    {
    double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return answer2;
    }
    //Display results and check if the solution is imaginary (real or not)
    private static void displaySolutions(double answer1, double answer2); {
    if (Double.isNaN(answer1) || Double.isNaN(answer2))
    {
        System.out.println("Answer contains imaginary numbers");
    } else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}

}

Your methods need to be defined outside your main method. You can then call them in your main method like this:

displayEquation(a, b, c);

You also should remove the semicolons after your method definitions. It should look like this:

public class QuadraticFormula {

public static void main(String[] args)
{
    //Creating scanner and variables
    Scanner s = new Scanner(System.in);
    System.out.println("Insert value for a: ");
    double a = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for b: ");
    double b = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for c: ");
    double c = Double.parseDouble(s.nextLine());
    s.close();
}
    //Display format for negatives
    public static void displayEquation(double a, double b, double c) {
        if (b > 0 && c > 0 ){
            System.out.println(a + "x^2 + " + b + "x + " + c + " =0");}
        if (b < 0 && c > 0 ){
            System.out.println(a + "x^2 " + b + "x + " + c + " =0");}
        if (b > 0 && c < 0 ){
            System.out.println(a + "x^2 + " + b + "x " + c + " =0");}
        if (b < 0 && c < 0 ){
            System.out.println(a + "x^2 " + b + "x " + c + " =0");}
    }

    private static double calculateSolution(double a, double b, double c) {
        double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
        return answer2;
    }

    //Display results and check if the solution is imaginary (real or not)
    private static void displaySolutions(double answer1, double answer2) {
        if (Double.isNaN(answer1) || Double.isNaN(answer2))
        {
            System.out.println("Answer contains imaginary numbers");
        }   
        else System.out.println("The values are: " + answer1 + ", " + answer2);
    }
}

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