0

I am a new in java and programming in general. I am currently doing complex numbers. I know that there might be an answer for this online, but it will also reveal if I used the correct algorithm and so on, so I am trying to avoid other answers around here.

My main issue is that I am having trouble with the Divide class that I made, since in complex number division we are going to return a fraction as an answer, I can't figure out how to have the program return the 2 statements that it calculate, can someone advise what can be done? Here is the part of the division code, it works fine when I check both part 1 and then part 2, but how can i get it to return both of them when calling using that class.

I attached my full code that I made, I know it can be tweaked to have less coding, but that is not my current concern.

Thanks for your help in advance.

class complexN {

    int R, I;

    complexN(int R, int I) {
        this.R = R;
        this.I = I;
    }

    complexN AddCN(complexN A) {
        return new complexN(this.R + A.R, this.I + A.I);
    }

    complexN SubCN(complexN A) {
        int AnsR = this.R - A.R;
        int AnsI = this.I - A.I;
        complexN Sum = new complexN(AnsR, AnsI);
        return Sum;
    }

    complexN MulCN(complexN A) {
        int AnsI = (this.R * A.I) + (this.I * A.R);
        int AnsR = (this.R * A.R) - (this.I * A.I);

        complexN Sum = new complexN(AnsR, AnsI);
        return Sum;
    }

    complexN DivCN(complexN A) {
        complexN ComCon = new complexN(A.R, (A.I * -1));
        complexN part1 = new complexN(this.R, this.I).MulCN(ComCon);
        complexN part2 = A.MulCN(ComCon);

        return part1;

    }

    void print() {
        String i = (this.I == 1 ? "" : (this.I == -1 ? "-" : "" + this.I));
        if (this.R != 0 && this.I > 0) {
            System.out.println(this.R + "+" + i + "i");
        }
        if (this.R != 0 && this.I < 0) {
            System.out.println(this.R + i + "i");
        }
        if (this.R != 0 && this.I == 0) {
            System.out.println(this.R);
        }
        if (this.R == 0 && this.I != 0) {
            System.out.println(i + "i");
        }
        if (this.R == 0 && this.I == 0) {
            System.out.println("0");
        }
    }
}

class complex {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        complexN z1 = new complexN(5, 2);
        complexN z2 = new complexN(3, -4);

        System.out.print("z1 = ");
        z1.print();
        System.out.print("z2 = ");
        z2.print();
        System.out.println("---------");
        z1.DivCN(z2).print();
    }
}
pvg
  • 2,673
  • 4
  • 17
  • 31
Ryne Ignelzy
  • 137
  • 1
  • 2
  • 13
  • 3
    Your naming is all over the place. In Java classes are in `PascalCase` and methods, and variables, are in `camelCase`. Please update your code to conform to the correct naming conventions. This is currently illegible. – Boris the Spider Apr 27 '17 at 20:04
  • It's true that naming conventions are not being followed, but I refute that it is _illegible_. – Jordão Apr 27 '17 at 20:09
  • Possible duplicate of [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java) – pvg Apr 27 '17 at 20:12
  • I think that since your class only has integer precision, it is reasonable that the result of division is not an exact value. you could store R and I as doubles otherwise – lucasvw Apr 27 '17 at 20:16
  • 2
    Your R and I types need to be fractions in their own right. Or kludge them with a double depending on your requirements. And do please obey case conventions. – Bathsheba Apr 27 '17 at 20:19
  • One more naming issue: do not abbreviate! There is no penalty for long identifiers... – Timothy Truckle Apr 27 '17 at 20:33
  • not sure what you mean by saying it is illegible, it works fine. – Ryne Ignelzy Apr 27 '17 at 21:01
  • and not sure that any of the comments give me a clue on what i can do...guess will have to ask my teacher when he checks my work, and explain then – Ryne Ignelzy Apr 27 '17 at 21:02
  • @RyneIgnelzy people are telling you your code doesn't follow the standard Java naming and typographical conventions which makes it hard for more experienced users (whose help you're after) to read and thus hard for them to try and help you. The answer I linked discusses multiple options regarding your actual question, in great detail. – pvg Apr 27 '17 at 22:00

0 Answers0