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. */ }