Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is the method we are supposed to create, my method fails to run properly an

ID: 3632666 • Letter: T

Question

this is the method we are supposed to create, my method fails to run properly and doesnt pass the j unit testing.

parseComplexNumber - this method takes one parameter (a String) and returns a ComplexNumber. The parameter is a String that represents a complex number, such as "5.9 + 73.44i" or "-2.35 + 6.5i". There could be any number of spaces in the beginning, at the end, before the 'i', and surrounding the '+' and '-' characters. For example, the following Strings could be passed to this method: " - 2.7 + 5.9 i", or possibly " 79.3 + 5 i" or even "-1.25+3.469i". The method will parse the String and return a ComplexNumber that represents the value described by the String. You may assume that the String being passed to this method is correctly formatted. In the strings that parseComplexNumber should accept, the imaginary part will never appear as a negative number. Instead, if the imaginary part is negative, a minus sign will appear between the two terms instead of a plus sign, and the imaginary part will appear as a positive term. If the real part is negative it will appear as a negative number. For example, one string that might be passed to the method could be "-2.35 - 6.5i" (and a string like "-2.35 + -6.5i" would not be passed in). Note that any string produced by toString should be able to be parsed by parseComplexNumber, and should return the exact same complex number that toString was called upon.

Note: your method is not expected to handle parameters like "3.2" or "-7.99i" - we would expect those to be passed to this method as something like "3.2 + 0i" or "0 - 7.99i", respectively. Also, your method is not expected to handle parameters like "+3.2+i" - we would expect something like "3.2+1i".

Hint: you will probably want to use some of the methods in the Java library String class, so you should probably review the online documentation for the Java String class. Also, you might find it useful to use the static method Double.parseDouble that was mentioned in class.

My CODE:
public static ComplexNumber parseComplexNumber(String s) {
s = s.replace(" ", "");
String realstring;
String imagstring;
if (s.indexOf('+') != -1) {
realstring = s.substring(0, s.indexOf("+"));
imagstring = s.substring(s.lastIndexOf("+") + 1, s.indexOf("i"));
}
else {
realstring = s.substring(0, s.lastIndexOf("-"));
imagstring = s.substring(s.lastIndexOf("-") - 1, s.indexOf("i"));
}
return new ComplexNumber(new MyDouble(Double.parseDouble(realstring)),
new MyDouble(Double.parseDouble(imagstring)));
}

Explanation / Answer

Dear,
public class ComplexNumber {
   
    private static double real;
    private static double imag;
    /** Creates a new instance of ComplexNumber */
    public ComplexNumber (double real,double imag) {
        this.real = real;
        this.imag = imag;
    }
    public String toString (){
        if(imag < 0)
            return real + " - " + Math.abs (imag) + "i";
        else
            return real + " + " + imag + "i";
    }
    public static ComplexNumber parseComplexNumber (String s){       
        int plusOrMinus;
        s = s.replaceAll (" ","");
        if(s.indexOf ('+') != -1)
            plusOrMinus = s.indexOf ('+');
        else
            plusOrMinus = s.lastIndexOf ('-');
        char operator = s.charAt (plusOrMinus);
        double real = Double.parseDouble (s.substring (0, plusOrMinus));
        double imag = Double.parseDouble (s.substring (plusOrMinus + 1, s.indexOf ('i')));
        if(operator == '-')
            imag *= -1;
        return new ComplexNumber(real, imag);
    }
    public static void main (String args[]){
        java.util.Scanner input = new java.util.Scanner (System.in);
        System.out.println ("Enter a complex number: ");
        String num = input.nextLine ();
        ComplexNumber cn = ComplexNumber.parseComplexNumber (num);
        System.out.println ("Given complex num: "+cn.toString ());
    }
}