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

Quadratic formula solver in Java

Govind

The One Musketeer
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
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");
		}
	}
}
 
Quadratic formula solver in Java
You code weird! D:
 
Quadratic formula solver in Java
Specifically what do you mean?

This code is formatted the way most professional code is.
 
Quadratic formula solver in Java
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
 
Quadratic formula solver in Java
That's what args[] is there for.
 
Quadratic formula solver in Java
Mainly because you used args, instead of asking for input later... :P
Well, if that's weird then obviously weird = more efficient.
 
Quadratic formula solver in Java
Quadratic formula solver in Java
Quadratic formula solver in Java
3-1

You lose, SuF :P
 
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.
 
Quadratic formula solver in Java
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
 
Quadratic formula solver in Java
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. =(
 
Quadratic formula solver in Java
Post questions in this forum and I'll try and get to you.
 
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.
Too much effort, sorry.
 
Runescape Gamblingrunescape gambling
Back
Top