Tuesday 15 May 2012

Java program for solving quadratic equation using methods -



Java program for solving quadratic equation using methods -

i have java programme written solving quadratic equation assignment requires me have methods each of tasks: displaying equation, determining if equation has real solutions, calculating solution, , displaying solutions if exist. guess haven't quite learned or came total understanding of implementing methods, here code have far:

import java.util.scanner; public class quadraticformula { public static void main(string[] args) { //creating scanner , variables scanner s = new scanner(system.in); system.out.println("insert value a: "); double = double.parsedouble(s.nextline()); system.out.println("insert value b: "); double b = double.parsedouble(s.nextline()); system.out.println("insert value c: "); double c = double.parsedouble(s.nextline()); //display format 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 * * c))) / (2 * a); double answer2 = (-b - math.sqrt(math.pow(b, 2) - (4 * * c))) / (2 * a); //display results , check if solution 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); } }

your code seems broken enough, using methods handle different parts straight foward. you'll need create sure pass necessary parameters each method. example, determining if solution real, you'll need pass reply variables check them. furthermore, since you'll need utilize display solution, method should homecoming boolean, checked in separate method. seek maintain in mind each method need , may need retrun them. hope helps.

java

No comments:

Post a Comment