• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

Urgent help: maths (quadratics) in programming (1 Viewer)

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!!


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

Drongoski

Well-Known Member
Joined
Feb 22, 2009
Messages
4,255
Gender
Male
HSC
N/A
A quadratic equation in the variable 'x' has the form: ax2 + bx + c = 0.

The a, b and c are what we may call the parameters (the variables) of the equation. So for each set of a, b and c, you have a quadratic equation. So if a=2, b= -5 and c= 1, you have one quadratic equation. If a= 3, b = 7 and c = -11, you have another quadratic equation. Your program is probably designed to read in the 3 values and print out the answer; i.e. the values of x (the roots) that make the equation true, i.e. the so-called solutions of the given equation.

There are 3 situations, viz:

i) eqn has 2 distinct (i.e. different) roots

ii) eqn has a repeated root, or sometimes called, 1 root

iii) eqn has 2 complex (i.e. not real numbers) roots

To determine which of these 3 cases an equation is, a quantity, called the discriminant = b2 - 4ac is calculated. If the discriminant is positive, eqn has 2 distinct roots, if 0, 2 identical roots and if negative, 2 complex roots.


To calculate the roots, your program will base its solution on the well-known quadratic equation formula:



Hope this helps.



Edit

For case of discriminant = negative, your answer will be a pair of complex numbers.

e.g. 2x2 - 3x + 5 = 0

Therefore the discriminant = (-3)2 - 4x2x5 = -31



or



depending on how your program is designed. Remember to take the square root of the negative of the discriminant for negative discriminants, as in this case, as you cannot find the square root of a negative number, in the usual sense.




I need your help to write Java programs.
 
Last edited:

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
On my phone, so I'll have a better look when I get home.

You should research integer division though.

I'd also suggest you start making using of debug printlns - print all of your inputs and after any calculations (e.g. print discriminant) to get a feel of what's happening. That's a very crude way of debugging if you don't want to learn how to use the tools in the IDE.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top