Quadratic formula solver in Java

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

Quadratic formula solver in Java
  1. Unread #1 - Oct 27, 2008 at 8:49 PM
  2. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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");
    		}
    	}
    }
     
  3. Unread #2 - Oct 28, 2008 at 6:36 AM
  4. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    Quadratic formula solver in Java

    You code weird! D:
     
  5. Unread #3 - Oct 28, 2008 at 12:58 PM
  6. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in Java

    Specifically what do you mean?

    This code is formatted the way most professional code is.
     
  7. Unread #4 - Oct 28, 2008 at 7:53 PM
  8. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    Quadratic formula solver in Java

    Mainly because you used args, instead of asking for input later... :p
     
  9. Unread #5 - Oct 28, 2008 at 8:11 PM
  10. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in Java

    That's what args[] is there for.
     
  11. Unread #6 - Oct 28, 2008 at 10:10 PM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Quadratic formula solver in Java

    Well, if that's weird then obviously weird = more efficient.
     
  13. Unread #7 - Oct 29, 2008 at 6:43 AM
  14. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    Quadratic formula solver in Java

    Faa, I like my way so I can loop easily... :/
     
  15. Unread #8 - Oct 29, 2008 at 7:45 AM
  16. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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).
     
  17. Unread #9 - Oct 29, 2008 at 12:10 PM
  18. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in Java

    3-1

    You lose, SuF :p
     
  19. Unread #10 - Oct 29, 2008 at 12:21 PM
  20. HyperSecret
    Joined:
    Jun 5, 2007
    Posts:
    123
    Referrals:
    0
    Sythe Gold:
    0

    HyperSecret Active Member

    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.
     
  21. Unread #11 - Oct 29, 2008 at 6:23 PM
  22. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    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
     
  23. Unread #12 - Nov 2, 2008 at 1:23 AM
  24. cars go fast
    Joined:
    Dec 17, 2007
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0

    cars go fast Active Member

    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. =(
     
  25. Unread #13 - Nov 2, 2008 at 1:03 AM
  26. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in Java

    Post questions in this forum and I'll try and get to you.
     
  27. Unread #14 - Nov 2, 2008 at 1:04 AM
  28. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in Java

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

Users viewing this thread
1 guest


 
 
Adblock breaks this site