Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class Ideone{
- public static void main(String[] args) {
- Complex c1 = new Complex(2, 2);
- Complex c2 = new Complex(4, 3);
- c1.print(); // 2+2i
- c2.print(); // 4+3i
- c1.add(c2);
- c2.mul(c1);
- c2.print();
- c2.sub(c1);
- c1.print(); // 6+5i
- c2.print(); // 3+33i
- }
- }
- class Complex{
- public int real;
- public int imaginary;
- public Complex(int r, int i){
- this.real = r;
- this.imaginary = i;
- }
- public void print(){
- System.out.print(this.real + "+" + this.imaginary + "i \n");
- }
- public void add(Complex c){
- this.real += c.real;
- this.imaginary += c.imaginary;
- }
- public void mul(Complex c){
- int oldreal = this.real;
- this.real = this.real * c.real - this.imaginary * c.imaginary;
- this.imaginary = oldreal * c.imaginary + this.imaginary * c.real;
- }
- public void sub(Complex c){
- this.real -= c.real;
- this.imaginary =this.imaginary- c.imaginary;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment