Java help [WARNING! Newbie inside ;)]

Soldato
Joined
24 Apr 2011
Posts
5,455
So at school, I started learning Java, and I have to do this program. Here is the question.

In the main method of a class Roots.java find the roots of the equation ax2+bx+c=0 such that
If a=0 and b=0 print a message that the equation is degenerate.
If a=0 and b<>0 there exists one root –c/b
If a<>0 and c=0 there exists two roots –b/a and 0
If b2-4ac < 0 there exist two complex roots
Else Use formula

I managed to do it all, without getting any errors, but the answers are wrong :@
If you put a=1, b=2 and c=1, you should get Root 1 = -1 and root 2 = -1 aswell yet my answers are -2 and -2.

This is what I did till now.

import java.util.*;
public class Question4
{
public static void main (String args []){
int a, b, c;
double temp, root1, root2;

Scanner s=new Scanner (System.in);

System.out.println ("Enter coefficient of x^2");
a = s.nextInt();

System.out.println ("Enter coefficient of x");
b = s.nextInt();

System.out.println ("Enter constant");
c = s.nextInt();

temp = ((b*b)-(4*(a)*(c)));

if (a==0 & b==0 )
System.out.println("Equation is degenerate");
else if (a==0 & b!=0)
System.out.println("there exists one root" + (-c/b));
else if (a!=0 & c==0)
System.out.println("There exists two roots" + (-b/a) + ("0"));
else if (temp < 0)
System.out.println ("There exists two complex roots");
else {
root1 = (-b + (Math.pow(temp , 0.5))/(2*a));
root2 = (-b - (Math.pow(temp , 0.5))/(2*a));

System.out.println ("Root 1 is " + root1);
System.out.println ("Root 2 is " + root2);
}
}
}

I am pretty sure the problem is here

else {
root1 = (-b + (Math.pow(temp , 0.5))/(2*a));
root2 = (-b - (Math.pow(temp , 0.5))/(2*a));

Reason is, if I remove the 2 2, and put it as (Math.pow(temp , 0.5))/(a) i get -2 still, o that makes me think it is not reading the 2, or just ignores it. I tried it out with the calculator, and if you do /1, the roots ome -2 and -2.

Please tell me, what am I doing wrong :S

P.S. I'm new, so don't be too harsh. I might have not even identified the mistake place.
 
You're right that was the error, it's a bracketing issue.

You had

Code:
root1 = (-b + (Math.pow(temp , 0.5))/(2*a));
root2 = (-b - (Math.pow(temp , 0.5))/(2*a));

Where as it should be

Code:
root1 = (-b + (Math.pow(temp , 0.5)))/(2*a);
root2 = (-b - (Math.pow(temp , 0.5)))/(2*a);

I've cleaned up a couple of bits of your code as well, for example in your conditions you're using the bitwise & operator whereas you should really be using the logical && and. Also although with if conditions if there is only one line to execute you can omit the { brackets, you really shouldn't it makes it less readable

Code:
import java.util.*;
public class Question4
{
    public static void main (String args []) {
        int a, b, c;
        double temp, root1, root2;

        Scanner s=new Scanner (System.in);

        System.out.println ("Enter coefficient of x^2");
        a = s.nextInt();

        System.out.println ("Enter coefficient of x");
        b = s.nextInt();

        System.out.println ("Enter constant");
        c = s.nextInt();

        temp = ((b*b)-(4*(a*c)));

        if ((a==0) && (b==0)) {
            System.out.println("Equation is degenerate");
        } else if ((a==0) && (b!=0)) {
            System.out.println("there exists one root" + (-c/b));
        } else if ((a!=0) && (c==0)) {
            System.out.println("There exists two roots" + (-b/a) + ("0"));
        } else if (temp < 0) {
            System.out.println ("There exists two complex roots");
        } else {
            root1 = (-b + (Math.pow(temp , 0.5)))/(2*a);
            root2 = (-b - (Math.pow(temp , 0.5)))/(2*a);
            System.out.println ("Root 1 is " + root1);
            System.out.println ("Root 2 is " + root2);
        } 
    }
}
 
Cheers! Finally got it right!

Cheers for cleaning up. About the curly brackets, yeah, my teacher is a piece of **** and didn't tell us we should do them in curly brackets. My friend told me after, but I didn't know exactly where when I got home.

And about the &&. It doesn't make a huge difference here though does it?
 
In this case yes you can use & but there is a difference. For example

Code:
int a = 1;
int b = 2;
if((a==1) | (b==1))

The if condition will evaluate to true, but with bitwise both conditions will be evaluated i.e. a's value is checked as well as b, even though the first condition has already resulted in true.

On the other hand with the logical equivalent.

Code:
if((a==1) || (b==1))

Evaluation stops at the first condition, no need to check the second condition.

Also with bitwise you can do things like this.

Code:
int a = 35;
int b = 40;
int result = a & b;
System.out.println(result);

This will print 32 because, bitwise compares each bit.

Code:
35 & 40

35 ==> 100011
40 ==> 101000
---------------
    ==> 100000 = 32
 
Back
Top Bottom