mamehapumpkin
Member
- Joined
- Feb 15, 2015
- Messages
- 35
- Gender
- Undisclosed
- HSC
- 2014
(If you don't know programming but know maths, please read regardless, I explain the code and it's fairly simple to look at so you can probably figure out what's going on anyway).
I really need help, this is due soon, and I'm having trouble with it. It's a programming assignment, and I know how to do the programming part. The issue is it is centred around a math question and math is not my strongest point. When I submit it to PASTA is doesn't pass all the tests and I have no idea what to do. :'(
Basically what the program does, is it asks the user for values a, b and c. Then it checks the discriminant to see if the roots are real or complex numbers. If complex, it prints "the roots are complex numbers" otherwise it prints the roots. Further down below my code, I posted a screenshot of the results I get in PASTA. It doesn't pass all the tests and I don't know how to fix it. I don't really find it fair, I mean obviously I know how to use the code and all that stuff. I'm only messing up on the math. (I know some people will say math is vital to programming, well I don't think I would need it to this extent in most cases unless I was coding stuff specifically for math related things). Please helpppp!!
I really need help, this is due soon, and I'm having trouble with it. It's a programming assignment, and I know how to do the programming part. The issue is it is centred around a math question and math is not my strongest point. When I submit it to PASTA is doesn't pass all the tests and I have no idea what to do. :'(
Basically what the program does, is it asks the user for values a, b and c. Then it checks the discriminant to see if the roots are real or complex numbers. If complex, it prints "the roots are complex numbers" otherwise it prints the roots. Further down below my code, I posted a screenshot of the results I get in PASTA. It doesn't pass all the tests and I don't know how to fix it. I don't really find it fair, I mean obviously I know how to use the code and all that stuff. I'm only messing up on the math. (I know some people will say math is vital to programming, well I don't think I would need it to this extent in most cases unless I was coding stuff specifically for math related things). Please helpppp!!
Code:
package quadratic;
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
int a, b, c;
Scanner input = new Scanner(System.in);
System.out.println("Enter the coefficient (a) of x^2:");
a = input.nextInt();
System.out.println("Enter the coefficient (b) of x:");
b = input.nextInt();
System.out.println("Enter the constant c:");
c = input.nextInt();
int discriminant = b*b - 4*a*c;
System.out.println(discriminant);
if(discriminant < 0) {
System.out.println("The roots of this equation are complex numbers.");
}
else if (discriminant == 0) {
System.out.println("There is 1 root: " + -b / (2*a) );
}
else if (discriminant > 0){
System.out.println("There are 2 roots: " + (-b + Math.sqrt(discriminant)) /(2*a) + " and " + (-b - Math.sqrt(discriminant)) /(2*a) );
}
}
}
Attachments
-
40.3 KB Views: 185