Search code examples
javacomplex-numbers

Java - complex numbers - problem with imaginary unit


I'm having some issues with "i" in imaginary unit. When I have "i" with number my program works. 4+4i it is ok. But when I have only "i" does not want to work. 4+i doesn't work.

I have no idea how to change the code to solve this error. I know that below lines make this problem.

        String x[] = str1.split("\\+|i|-");
        String y[] = str2.split("\\+|i|-");

It is program for calculating multiplication(*), division(/), addition(+) and subtraction(-) of complex numbers typed as a string.

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    private static DecimalFormat df = new DecimalFormat("0.0");
    public static String Addition(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r + b_r;
        double y = a_i + b_i;
        return df.format(x) + "+" + df.format(y) + "i";
    }
    public static String Subtraction(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r - b_r;
        double y = a_i - b_i;
        return df.format(x) + "-" + df.format(y) + "i";
    }
    public static String Multiplication(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r * b_r - a_i * b_i;
        double y = a_r * b_i + a_i * b_r;
        return df.format(x) + "+" + df.format(y) + "i";
    }
    public static String Division(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r*b_r + a_i*b_i / b_r + b_i;
        double y = a_r*b_i - a_i*b_r / b_r + b_i;
        return df.format(x) + "+" + df.format(y) + "i";
    }

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Num1");
        String str1 = scan.nextLine();

        System.out.println("Num2");
        String str2 = scan.nextLine();

        String x[] = str1.split("\\+|i|-");
        String y[] = str2.split("\\+|i|-");

        double a_real = Double.parseDouble(x[0]);
        double a_img = Double.parseDouble(x[1]);
        double b_real = Double.parseDouble(y[0]);
        double b_img = Double.parseDouble(y[1]);

        System.out.println(a_real);
        System.out.println(a_img);
        System.out.println(b_real);
        System.out.println(b_img);


       System.out.println(Addition(a_real, a_img, b_real, b_img));
       System.out.println(Subtraction(a_real, a_img, b_real, b_img));
       System.out.println(Multiplication(a_real, a_img, b_real, b_img));
       System.out.println(Division(a_real, a_img, b_real, b_img));
    }
}

Solution

  • Fix

    You use the length of the array as condition

    double a_img = x.length > 1 ? Double.parseDouble(x[1]) : 1;
    double b_img = y.length > 1 ? Double.parseDouble(y[1]) : 1;
    

    Improve

    For now you code doesn't handle negative numbers, as the dash is in the split pattern. You may use a regex to match the parts you need : (-?\\d+)\\+?(-?\\d*)\\+?i

    The parsing of the real part is easy, for the img you may check if the part is empty (case +i) and if the the part is just a dash (case -i)

    Matcher m = Pattern.compile("(-?\\d+)\\+?(-?\\d*)\\+?i").matcher(value);
    if (m.find()) {
        System.out.println(m.group(1) + "<>" + m.group(2) + " ==>" + m.groupCount());
        real = Double.parseDouble(m.group(1));
        img = m.group(2).isEmpty() ? 1 : m.group(2).equals("-") ? -1 : Double.parseDouble(m.group(2));
    } else {
       throw new InvalidParameterException(value);
    }
    

    Object-Oriented programation

    Designing a small class, the use could be like

    class Complex {
    
        private static final DecimalFormat df = new DecimalFormat("0.0");
        private final double real, img;
    
        public Complex(String value) {
            Matcher m = Pattern.compile("(-?\\d+)\\+?(-?\\d*)\\+?i").matcher(value);
            if (m.find()) {
                System.out.println(m.group(1) + "<>" + m.group(2) + " ==>" + m.groupCount());
                real = Double.parseDouble(m.group(1));
                img = m.group(2).isEmpty() ? 1 : m.group(2).equals("-") ? -1 : Double.parseDouble(m.group(2));
            } else {
                throw new InvalidParameterException(value);
            }
        }
    
        public Complex(double r, double i) {
            real = r;
            img = i;
        }    
        public Complex add(Complex other) {
            return new Complex(real + other.real, img + other.img);
        }    
        public Complex substract(Complex other) {
            return new Complex(real - other.real, img - other.img);
        }    
        public Complex multiply(Complex other) {
            return new Complex(real * other.real - img * other.img, real * other.img + img * other.real);
        }
    
        public Complex divide(Complex other) {
            double denominator = Math.pow(other.real, 2) + Math.pow(other.img, 2);
            return new Complex((real * other.real + img * other.img) / denominator,
                (real * other.img - img * other.real) / denominator);
        }    
        @Override
        public String toString() { return df.format(real) + "+" + df.format(img) + "i";}
    }
    

    use

    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        System.out.println("Input num1: ");
        String str1 = scan.nextLine();
    
        System.out.println("Input num2: ");
        String str2 = scan.nextLine();
    
        Complex c1 = new Complex(str1);
        Complex c2 = new Complex(str2);
        System.out.println(c1.add(c2));
        System.out.println(c1.substract(c2));
        System.out.println(c1.multiply(c2));
        System.out.println(c1.divide(c2));
    }