• Log in or sign up for an account here: https://www.sythe.org/login

Quadratic formula solver in Java

Govind

The One Musketeer

Highly Respected
Mudkips
Retired Administrator
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
You code weird! D:
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Specifically what do you mean?

This code is formatted the way most professional code is.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Specifically what do you mean?

This code is formatted the way most professional code is.

Mainly because you used args, instead of asking for input later... :P
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
That's what args[] is there for.
 
Mainly because you used args, instead of asking for input later... :P
Well, if that's weird then obviously weird = more efficient.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Member of the Month Winner
Sythe's 10th Anniversary
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
3-1

You lose, SuF :P
 
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
3-1

You lose, SuF :P

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
 
Specifically what do you mean?

This code is formatted the way most professional code is.

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. =(
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Post questions in this forum and I'll try and get to you.
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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.
Too much effort, sorry.
 
Runescape Gamblingrunescape gambling
Back
Top