CSE 214 , Java, complex number help
This is a CSE homework, I was hoping there might be some friendly guys and gals out there that might take a quick look and see if it looks good to turn in, thanks Y'all.
Here are the instructions and the code I wrote,
-Kyle
write a ComplexNumber class with:
(1) a constructor which does not take any parameters (where the default value of a complex number should be 0 + 0i in this case.)
(2) another constructor that takes real and imaginary parts of type int as parameters
(3) an add method which takes another complex number, c2, as a parameter and adds c2 to the current complex number, which is this, and returns the resulting complex number. (4) a subtract method which takes another complex number, c2, as a parameter and subtracts c2 from the current complex number this, and returns the resulting complex number.
(5) a multiply method which takes another complex number, c2, as a parameter and multiplies c2 with the current complex number this, and returns the resulting complex number.
(6) a divide method which takes another complex number, c2, as a parameter and divides the current complex number this by c2, and returns the resulting complex number.
(7) a toString1 method that will print a String that is the current complex number in the form of a + bi, where a and b will be the values of the real and imaginary parts of the natural number.
/*
* Kyle Arthur Benzle
* CSE 214
* 10/13/9
* Tagore
*
* This program takes two int variables and performs
* four mathematical operations (+, -, *, /) to them before returning the result from a toString1 method.
*/
//our first class Complex#
public class ComplexNumber {
// two int variables real and imagine
int real;
int imagine;
// Constructor, no parameters, setting our complex number equal to o + oi
ComplexNumber() {
real = 0;
imagine = 0; }
// Constructor taking two int variables as parameters.
ComplexNumber(int rePart, int imaginePart) {
real = rePart;
imagine = imaginePart; }
// This is the add method, taking object c2 as parameter, and adding it to .this to return
public ComplexNumber add(ComplexNumber c2) {
return new ComplexNumber(this.real + c2.real, this.imagine + c2.imagine); }
// Now the subtract method, followed by the methods to multiply and divide according to hand-out rules.
public ComplexNumber substract(ComplexNumber c2) {
return new ComplexNumber(this.real - c2.real, this.imagine - c2.imagine); }
public ComplexNumber multiply(ComplexNumber c2) {
ComplexNumber c3 = new ComplexNumber();
c3.real = this.real * c2.real - this.imagine * c2.imagine;
c3.imagine = this.real * c2.imagine + this.imagine * c2.real;
return c3; }
public ComplexNumber divide(ComplexNumber c2) {
ComplexNumber c3 = new ComplexNumber();
c3.real = this.real / c2.real - this.imagine / c2.imagine;
c3.imagine = this.real / c2.imagine + this.imagine / c2.real;
return c3; }
// toString1 method to return "a+bi" as a String.
public String toString1() {
return this.real + " + " + this.imagine + "i";
}
/* And we are all done, except for this last little } right here. */ }
Is nobody going to point out that his division is off?
a + bi / x + yi
is not simply a/x + b/y + "i"
.
The correct form would be
(a*x + b*x) - (a*y - b*y*(-1)) / (X^2 + y^2)
.
please correct me if i am missing something.
Kyle,
Glad you're interested in verifying your code! Have you heard about test driven development? It's the process of writing unit tests before you write your code.
In general, testing helps verify that your code does what it's supposed to do...so even if you test after, you know your code does what it's supposed to do (for the most part).
What I suggest: write some j-unit tests (very quick and easy to implement) and actually test out solutions! Once you've implemented these tests, it's awesome because you can re-run these tests if code is ever changed...
and believe it or not, it makes you an amazing developer. Start doing this early! Many people in the industry don't test code and it has led to a lottttt of issues.
Your instance variables, real and imagine should probably be private.
For mulitply and divide you might construct your c3s directly, rather than use the zero-args constructor, just as you did for subtract and add. If you think that looks ugly, use temporary variables to hold the real and imgainary parts.
What will happen in the divide case if you pass in
new Complex()
as the argument? At the very least, if what does happen is what you intend, document that.
链接地址: http://www.djcxy.com/p/24372.html上一篇: OpenCL中的复杂数字支持
下一篇: CSE 214,Java,复数帮助