简体   繁体   中英

Java program for solving quadratic equation using methods

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 guess I haven't quite learned or came to a full understanding of implementing methods, 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
    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
    double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);

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

You are almost done with your assignment. All you need is a good understanding of the modularity.

  1. displayEquation(double a, double b, double c);
  2. calculateSolution(double a, double b, double c);
  3. hasRealSolutions(double answer1, double answer2);
  4. displaySolutions(double answer1, double answer2);

Of course this may look like some redundant or unnecessary for this small program but modularity concepts come in handy when you write really big programs. Please look at this http://www.javawithus.com/tutorial/call-by-value-and-call-by-reference

I think this is a assignment for you, believe me its a easy one. Go through the link you will find the answer yourself. Good luck with your programming.

Your code seems to be broken up well enough, using methods to handle the different parts is fairly straight foward. You'll need to make sure that you pass the necessary parameters to each method. For example, for determining if the solution is real, you'll need to pass your answer variables to check them. Furthermore, since you'll need to use this to display the solution, your method should return a boolean, to be checked in a separate method. Try to keep in mind what each method will need and what you may need to retrun from them. I hope this helps.

I'll help you with making a method for this chunk of code (I'm not using all of it, because you'll learn more by applying it yourself)

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

To create a method, you need to first determine what it's return type is (if any), and what its parameters should be. Since you are working with doubles the return type for your "work the problem" method should be a double, and since you have three variables (a, b, c) those should be included in the methods parameters.

When making parameters, you have to first say what type they are, and then give them a name. If you have more than 1 parameters, you have to separate them with a comma.

This is what the method should look it.

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

Just for clarity, this chunk of code will be placed outside of the main method (under the second to last curly bracket)

In your main method, you should set the value of answer1 to

  double answer1 = workTheFirstPart(a, b, c);

Because the method returns a double, you can set answer1 equal to it.

Hopefully this will get you going in the right direction and hopefully I didn't make any stupid mistakes (I've been at work all day, my brain is a little fried).

Good luck!

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