Adblock breaks this site

Quadratic formula solver in Java

Discussion in 'Programming General' started by Govind, Oct 27, 2008.

  1. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    Cheap imitation of SuF's version :p

    I don't know Java too well (but I do know C++), this is just my attempt to write something in Java using Microsoft Visual J++ 6.0, a 10-year old Java compiler that is long since deprecated and unsupported, but I have it, so I might as well use it a bit.

    Code:
    import java.lang.Math;
    public class Class1
    {
    	public static void main (String[] args)
    	{
    		double a = 0, b = 0, c = 0;
    		try{
    			a = Double.valueOf(args[0].trim()).doubleValue();
    			b = Double.valueOf(args[1].trim()).doubleValue();
    			c = Double.valueOf(args[2].trim()).doubleValue();
    		}
    		catch(NumberFormatException nfe)
    		{
    			System.out.println("NumberFormatException: " + nfe.getMessage());
    			return;
    		}
    		catch(ArrayIndexOutOfBoundsException oobe)
    		{
    			System.out.println("ArrayIndexOutOfBoundsException: " + oobe.getMessage());
    			return;
    		}
    		if(((b*b)-(4*a*c))<0)
    		{
    			System.out.println("Nonreal roots");
    		}
    		else
    		{
    			double x1 = (-b+Math.sqrt((b*b)-(4*a*c)))/(2*a);
    			double x2 = (-b-Math.sqrt((b*b)-(4*a*c)))/(2*a);
    			System.out.print("x[1] = ");
    			System.out.print(x1);
    			System.out.print("\n");
    			System.out.print("x[2] = ");
    			System.out.print(x2);
    			System.out.print("\n");
    		}
    	}
    }
     
  2. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quadratic formula solver in Java

    You code weird! D:
     
  3. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    Specifically what do you mean?

    This code is formatted the way most professional code is.
     
  4. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quadratic formula solver in Java

    Mainly because you used args, instead of asking for input later... :p
     
  5. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    That's what args[] is there for.
     
  6. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Quadratic formula solver in Java

    Well, if that's weird then obviously weird = more efficient.
     
  7. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quadratic formula solver in Java

    Faa, I like my way so I can loop easily... :/
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Quadratic formula solver in Java

    I like the UNIX terminal tbh.
    http://www.injunea.demon.co.uk/pages/page205.htm

    :D

    Well, depends on how user friendly you want to be as to which method you use. Personally I just use what's there, rather than making up my own silly methods (therefore I use args, which are easier to read).
     
  9. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    3-1

    You lose, SuF :p
     
  10. HyperSecret

    HyperSecret Active Member

    Joined:
    Jun 5, 2007
    Posts:
    123
    Referrals:
    0
    Sythe Gold:
    0
    Quadratic formula solver in Java

    Nice, ever thought of adding an input of some sort?

    Where a person can input an equation and you filter through and figure out the answer?

    I could input x^2+4x+2 and it filters through and finds what a, b and c is. ;)
    Could even add in a factoring method ;) that would be fun.

    That would make it pretty sweet.

    p.s. - have it figure out imaginaries.
     
  11. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quadratic formula solver in Java

    Ummmm, my vote counts as 4 since this is a "Cheap imitation of SuF's version :p"


    :D

    But what ever... New project, make a gui and lets argue about that! :D
     
  12. cars go fast

    cars go fast Active Member

    Joined:
    Dec 17, 2007
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0
    Quadratic formula solver in Java

    Well it's definately coded the way I am being taught how to.

    I am taking a basic Java course... it's not a basic class, but it is Advanced Computer Science with basic knowledge of Java.

    Help me out next semester? lol. Please? I was forced to take this class as part of an IB Diploma Program. =(
     
  13. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    Post questions in this forum and I'll try and get to you.
     
  14. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Quadratic formula solver in Java

    Too much effort, sorry.
     
< Loading The Runescape Client. | selling steam account for ijji account level 60 to 80 >


 
 
Adblock breaks this site