Teacher Gave me An assignment I dont understand
I dont remember anything from algebra and my teacher wants us to do this
create four classes that consist of this
Complex, which encapsulates a complex number
oComplexPair, which encapsulates a pair of complex numbers
oQuadratic, which encapsulates a quadratic equation and solves and returns a complexPair object.
oSolveEquation, which contains the main method
I pretty much have it where I enter an equation into the SolveEquation class and it was pass it to the quadratic class and seperate it down to a,b, and c... Im pretty confused.
[582 byte] By [
zito358a] at [2007-11-26 16:22:28]

Great! So what's your problem?
> ComplexPair, which encapsulates a pair of complex numbersNot just an two complex numbers. A complex pair is of the form a ± bi
I really dont understand what that means what I would do. Should I pass a and b from the quadratic equation? I figure out what x equals both times then what. Can you walk me through it real slow, im kind of slow.
Post some code and ask a specific question.
Whats in bold is what I need help with, I need to send that to the complexPair class which I have not written nor have I written the complex class. Im not sure where to start at all. Like I know im supposed to send x= b + and x= b- but how am I supposed to send both results and then put it in the form u said a +- bi?
import java.util.*;
public class quadratic
{
private String equation;
private String quadratic;
private String linear;
private String constant;
private String newProblem;
private String finalProblem;
private int a;
private int b;
private int c;
public void quadratic (String x)
{
equation=x;
}
public int getQuadratic ()
{
int firstX = equation.indexOf('x');
quadratic=equation.substring( 0, firstX );
a=Integer.parseInt(quadratic);
return a;
}
public int getLinear ()
{
int firstX = equation.indexOf('x');
newProblem=equation.substring(firstX+1,equation.length());
int newX = newProblem.indexOf('x');
int value=newProblem.indexOf('+');
if (value==1)
linear=newProblem.substring(2,newX);
else
linear=newProblem.substring(1,newX);
b=Integer.parseInt(linear);
return b;
}
public int getConstant()
{
int firstX = newProblem.indexOf('x');
finalProblem=newProblem.substring(firstX,newProblem.length());
int value=finalProblem.indexOf('+');
if (value==1)
constant=finalProblem.substring(2,finalProblem.length());
else
constant=finalProblem.substring(1,finalProblem.length());
c=Integer.parseInt(constant);
return c;
}
[b]public complexPair solve ()
{
complexPair x= new complexPair();
int x=b + Math.sqrt(b^2-4ac/2a);
return x;
}[/b]
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public String toString ()
{
String x=" " + this.getQuadratic() + this.getLinear() + this.getConstant();
return x;
}
public boolean equals (quadratic x)
{
if (this.getQuadratic()==(x.getQuadratic()) && this.getLinear()==(x.getLinear()) && this.getConstant()==(x.getConstant()))
return true;
else
return false;
}
}
> I dont remember anything from algebra
How long ago was that? Last year?
> and my teacher wants us to do this
>
> create four classes that consist of this
> Complex, which encapsulates a complex number
Right: z = x + iy
> ComplexPair, which encapsulates a pair of complex numbers
Easy enough. Java can only return one object, and your teacher wants you to be able to return two Complex numbers at once.
>
> oQuadratic, which encapsulates a quadratic
> equation and solves and returns a complexPair
> object.
Remember the formula? Two roots, sometimes - wait for it - complex?
> oSolveEquation, which contains the main method
> I pretty much have it where I enter an equation into
> the SolveEquation class and it was pass it to the
> quadratic class and seperate it down to a,b, and c...
I won't believe that until you post some code. Are you the person who was getting the number parsing exception this week because you couldn't figure out how to parse a String with a quadratic equation?
I'm betting you've done nothing at all that's worth keeping.
> Im pretty confused.
Post some code. Let's see just how bad off you are.
%
[code]
int x=b + Math.sqrt(b^2-4ac/2a);
[code]
This isn't legal Java. It won't compile at all. Or, if it does, it's not going to give you what you need.
As I thought - you've done nothing worth keeping.
%
Hint: The "^" character does NOT mean "raise to a power" in Java.
Another hint: The method should NOT return an int. It should return - wait for it - a Complex number.
Still another hint :That's the wrong formula. Utterly wrong. You'll NEVER get the right answer if you can't even Google for the proper formula.
I'm concluding that you're so lazy, so incompetent, so helpless that it's a waste of time to help you. Drop the class now.
I already posted one the quadratic class. The other class is just scanning in the the formula, the first past of my code breaks down the formula and seperates it in a, b, and c. I am trying to pass it back to the complexpair class. No, I would not try and parse something a string with letters in it.
How do I encapsulate a complex Number?
> How do I encapsulate a complex Number?
I have a great deal of sympathy for you considering one of my severest flaws is that I haven't progressed very far in mathematics. However, I've yet to take trig and I didn't have much trouble figuring it out by just Googling "complex number" (the quadratic formula I learned in high school, so you really SHOULD know that).
your equals method is quite wrong.
there's little about what you've posted that's correct.
You tell me: What would a Complex number class have?
Remember, you weren't asked to write a fully functional complex number. You don't have to add or multiply them. What's different about a complex number? How is it different from a real number? (You were told twice earlier in this thread.)
%
coding wise. not the formula you write down on a paper. a +- bi. I understand that. I understand you solve ax^2+bx+c with b+-square root of b^2-4ac/2a. Thats not the problem. Putting it into code is.
so the result just needs to be like say
4x^2+2x-9
it would be
4+or -2i=0... (I dont know the symbol for the plus minus)
in the form a +- bi?
Instead of bashing me, maybe you could help man. The reason im here is to ask for help. If I knew it all I wouldnt be here.
Dvoiwnik ;)
Complex { double x;double y;}
ComplexPair{ Complex first; Complex second;}
Quadratic { int a; int b; int c;
double getD(){ return b*b - 4*a*c;}
ComplexPair solve() { ComplexPair cp = new ComplexPair(); if ( getD() < 0 )
{ cp.first.x = cp.second.x = -b/2a; cp.first.y = Math.sqrt(-getD); cp.second.y = - cp.first.y;}
else { cp.first.x = ( -b + Math.sqrt(getD) ) /(2*a);cp.second.x = ( -b - Math.sqrt(getD) ) /(2*a);} return cp; } }
I guess You can put correct modifiers in correct place ;)
> coding wise. not the formula you write down on a
> paper. a +- bi.
What's the difference?
> I understand that.
No, you don't. If you REALLY understood it, you wouldn't have to ask how to express it in code. Hence my question.
>I understand you
> solve ax^2+bx+c with b+-square root of b^2-4ac/2a.
> Thats not the problem. Putting it into code is.
That is the problem. The fact that you really don't understand it makes it impossble for you to put it into code.
Could you do a hand calculation to get the roots for the following case?
x^2 + 4x + 4 = 0
What would the ComplexPair be?
%
> so the result just needs to be like say
>
> 4x^2+2x-9
>
> it would be
>
> 4+or -2i=0... (I dont know the symbol for the plus
> minus)
> in the form a +- bi?
Where did you get this answer?
>
> Instead of bashing me, maybe you could help man. The
> reason im here is to ask for help. If I knew it all I
> wouldnt be here.
Nobody's bashing you. We're asking you to learn something.
I've already written the result, but you won't learn anything if I just give it to you.
%
the real problem is im just having trouble PASSING the double down to classes.
Here is what my code looks like now... all four classes.
Client Class
import java.util.*;
public class solveEquation
{
public static void main (String [] args)
{
quadratic one=new quadratic();
quadratic two=new quadratic();
Scanner scan=new Scanner(System.in);
System.out.print("A Quadratic Equation is an equation that is in this fourm Ax^2+Bx+C=0. Please Enter the following to determine");
System.out.println(" the roots, and the results. Thank you!");
System.out.print("Please Enter A:");
int a=scan.nextInt();
System.out.print("Please Enter B: ");
int b=scan.nextInt();
System.out.print("Please Enter C: ");
int c=scan.nextInt();
one.quadratic(a,b,c);
}
}
Quadratic Class
import java.util.*;
public class quadratic
{
private int a;
private int b;
private int c;
public void quadratic (int x, int y, int z)
{
a=x;
b=y;
c=z;
}
double getMath()
{
return b*b - 4*a*c;
}
public complexPair solve()
{
complexPair quadratic = new complexPair();
complex one = new complex();
complex two = new complex();
one.quadratic =(double) ( -b + Math.sqrt(getMath()) ) /(2*a);
two.quadratic =(double) ( -b - Math.sqrt(getMath()) ) /(2*a);
return cp;
}
}
ComplexPair Class
import java.util.*;
public class complexPair
{
complex numberOne=new complex();
complex numberTwo=new complex();
public void complexPair(complex x, complex y)
{
}
}
Complex Class
import java.util.*;
public class complex
{
public double x;
public void complex (double x)
{
this.x=x;
}
}
I started over. How does it look? Any suggestion? I have only been doing java for 4 months. We just started classes and arrays.
> I have only been doing java for 4 months. We just started classes and arrays.? What was your class doing for 4 months if you've only started classes and arrays!
> > I have only been doing java for 4 months. We just
> started classes and arrays.
>
> ? What was your class doing for 4 months if you've
> only started classes and arrays!
Trust me, these classes go sloooooooooooow. S/he is probably in the introductory computer science class.
basics, if statements, loops, all that good stuff. We are soon getting into multidimensional arrays. I'm in the second class now. The whole point of these assignments are for us to think in abstraction. They are not easy.
This is the complete Assignment....
Write a program that solves a quadratic equation in all cases, including when both roots are complex numbers. For this, you need to set up the following classes:
oComplex, which encapsulates a complex number
oComplexPair, which encapsulates a pair of complex numbers
oQuadratic, which encapsulates a quadratic equation
oSolveEquation, which contains the main method
Along with the usual constructors, accessors, and mutators, you will need to code additional methods:
oIn the Complex class, a method that determines whether a complex object is real
oIn the ComplexPair class, a method that determines whether both complex numbers are identical
oIn the Quadratic class, a method to solve the quadratic equation and return a ComplexPair object
oAdditionally, you need to include code in the main method to solve several examples of quadratic equations input from the keyboard.
Your output should make comments as to what type of roots we get (double real root, distinct real roots, distinct real roots, distinct complex roots). You should check that your code works in all four basic cases:
1.The quadratic equation is actually a linear equation
2.Both roots are complex
3.There is a double real root
4.There are two distinct real roots.
You will need to use javadoc comments in all the classes to generate the appropriate documentation. Make sure in your main program that I can tell what test cases you are running. Your program should be neat, easy to read, correctly indented, with appropriate comments. One last thing, you may work with a partner if you want, but you need to document who does what.
Review:
To solve the quadratic equation ax2 + bx + c, use the following formula:
Use the value inside the square root (b2 4ac), called the discriminant, to determine the types of roots.
discriminant > 0 : 2 real solutions
discriminant = 0 : 1 repeated real solution
discriminant < 0 : 2 complex solutions
I would write one class at a time, starting with the lowest level: Complex. Examine your Complex class. It has a basic fault. What is the basic structure of a complex number? Have you captured it in you class?
should it be a string or should I somehow break the number up when I calculate it in the quadratic class? because I dont understand how u can encapsulate something like a plus or minus bi. Haha, Im so sorry guys. Thanks guys, I really appreciate this.
> I started over. How does it look? Any suggestion? I have only been doing java for 4 months. We just started classes and arrays.
Pay attantion to my post on the first page. Complex should have couple of doubles!
And solution should provide couple of Complex!!!
D meand Discreemenant.
Don't mix quadrantic and complex.
All other not bed ;)
This one is really funny flame :-)))
look at your complex class.you keep telling us that you understand complex numbers, yet your complex class has only one private member.how can that possibly represent a complex number?%
ok this is what I changed it to , Dima, your right . So if it lets it pass two doubles. it will accept the first part of the complex number in double x, and the second part in double y?
import java.util.*;
public class complex
{
public double x;
public double y;
public void complex (double x, double y)
{
this.x=x;
this.y=y;
}
}
ok, i think i've got what ur saying... now another question.... how would i go about seperating the complex number into real number and the imaginary number in the quadratic class?
x is real part of comlex and y is imagenaryas far as You don't realy need any operation on complex You just to remember if D < 0 engage y otherwise use only x