Results 1 to 7 of 7
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default add an If Else statement and......uh????

    the Task:
    Write a ConsoleProgram that accepts values for a, b, and c, and then calculates the two quadratic equation solutions.
    If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect.

    here is my code:
    Java Code:
    public void run(){
    	println("Enter coefficients for the quadratic equation: ");
    	int a = readInt(" Input a: ");
    	int b = readInt(" Input b: ");
    	int c = readInt(" Input c: ");
    	[COLOR="SeaGreen"]//if ((b*=b)-(4*a*c)<=0){
    		//println(" The formula has no real solutions");
    	//}else{[/COLOR]
    		println(" The first solution is: "+ quadraticPlus(a,b,c));
    		println(" The second solution is: "+ quadraticMinus(a,b,c));
    		 [COLOR="SeaGreen"] // } [/COLOR]
    	}
    
    	private double quadraticMinus(int a, int b, int c) {
    		double minusX = (-b - Math.sqrt((b*=b)-(4*a*c)))/2*a;
    		return minusX;
    	}
    
    	private double quadraticPlus(int a, int b, int c) {
    		double plusX = (-b + Math.sqrt((b*=b)-(4*a*c)))/2*a;
    		return plusX;
    	}
    
    }
    i wrote this without the If Else statement (in green) to make sure the methods worked, which they did
    and heres my output
    Java Code:
     Input a: 1
     Input b: -5
     Input c: 6
     The first solution is: 3.0
     The second solution is: 2.0
    this output is correct.

    but, when i add in the If Else statement i get this output
    Java Code:
    Input a: 1
     Input b: -5
     Input c: 6
     The first solution is: -0.24234932786873742
     The second solution is: -24.757650672131263
    and i dont understand why it has changed. :confused:
    if the method is passed the same values why should it return different results
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Take a look at your if statement... specifically this
    Java Code:
    (b*=b)
    What is this little bit of code doing?

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    it should be this
    (b*b) :o

    thanks a lot..:)
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #4
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    just one other thing
    when i put b*=b into a boolean test does it actually alter the variable int b, that is passed to the methods

    or should i say can you alter an integer value inside a boolean test.

    i thought that a boolean could only be true or false

    in the OP example it appears to alter the value of int b otherwise why would the result be different
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    That is correct, you can change an object's value inside a boolean test.

    Just because its altered doesn't mean that the statement doesn't return a true or false.

    Just to prove it run a SSCC example as such:
    Java Code:
    public class Test {
    	
    	public static void main(String[] args){
    		int i = 0; 
    		while (i < 100) {
    			i = i + 1;
    			System.out.println("1st Print: " + i);
    			if (i++ % 2 == 0) {
    				System.out.println("You'll never see me...");
    			}
    		}
    	}
    	
    }

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Just one other thing: if b*b-4*a*c == 0 there is one real root (-b/(2*a)). Only if that term is less than zero there are two complex roots; otherwise (that term is greater than zero) there are two real roots.

    kind regards,

    Jos

  7. #7
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    i see :D

    i had thought that the stuff in the boolean parenths was simply a test it hadn't occurred to me the program was still in fact a program within the boolean parenths and it seems kinda obvious now you pointed that out.


    and thanks Jos for pointing out the 0 bit

    i now have (b*b)-(4*a*c)<0) which works great
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 04:14 PM
  2. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 02:56 PM
  3. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  4. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •