Pergunta

I made an object, MyString. I can't figure out how to recreate valueOf(double d). I recreated valueOf for integers. Just to make it easier I limited the amount of decimal places to 8. How can I recreate valueOf(double d)?

public class MyString {

private char[] a;

public MyString(String s) {
    this.a = s.toCharArray();
}

public MyString(char[] a) {
    this.a = a;
}

public String toString() {
    return new String(a);
}

public int length() {
    return a.length;
}

public char charAt(int i) {
    return a[i];
}
    public static MyString valueOf(int i) {
    int digits = (int)(Math.log10(i)+1);
    char[] b = new char[digits];
    for (int j = 0; j < digits; j++) {
        b[j] = (char) (48 + i / 10);
        i = i % 10;
        if (i < 10) {
            b[j + 1] = (char)(48 + i);
            break;
        }
    }
    MyString ms = new MyString(b);
    return ms;
    }
public static MyString valueOf(double d) {
            char[] d1 = new char[digits];
            //Take each digit of the number and enter it into the array
            MyString ms = new MyString(d1);
    return ms;

}

public static void main(String[] args) {

}

}

Foi útil?

Solução

I assume you are doing this for fun... so here is the approach I took. You have valueOf(int i) already, so why not basically reuse that function. Just take the double and keep multiplying it by 10 until you have an int. Keep track of where the decimal place goes, then you basically call your valueOf(int i) but also include the decimal point.

I had trouble running your code so I re-did valueOf(int i) then created valueOf(int i, int decimalSpot), passing in -1 or 0 for the decimal spot then it's an integer value and don't use a decimal place.

Anyway, here is what I came up with. It's late, so probably not the cleanest code, but should give you a proof of concept.

public class MyString {

    private char[] a;

    public MyString(String s) {
        this.a = s.toCharArray();
    }

    public MyString(char[] a) {
       this.a = a;
    }

    public String toString() {
        return new String(a);
    }

    public int length() {
        return a.length;
    }

    public char charAt(int i) {
        return a[i];
    }

    public static MyString valueOf(int i) {
        return MyString.valueOf(i,-1);
    }

    public static MyString valueOf(double d) {
        int decimalPlace = 0;

        while (d != (int)d)
        {
            decimalPlace++;
            d = d*10;
        }

        return MyString.valueOf((int)d,decimalPlace);
    }

    public static MyString valueOf(int i, int decimalSpot) {
        int index=0;
        int digits = (int)(Math.log10(i)+1);
        int stringLength=digits;
        if (decimalSpot == 0) decimalSpot=-1; // Don't return 1234. - just return 1234
        if (decimalSpot != -1) 
        {
            // Include an extra spot for the decimal
            stringLength++;
        }
        char[] b = new char[stringLength];
        for (int j = digits-1; j >= 0; j--) {
            int power = (int) Math.pow(10,j);
            int singleDigit = (int) Math.floor(i/power);
            i = i - power*singleDigit;
            b[index++] = (char) (48 + singleDigit);

            if (decimalSpot==j)
            {
                b[index++] = '.';
            }
        }

        MyString ms = new MyString(b);
        return ms;
    }

    public static void main(String[] args) {        
        MyString ms = MyString.valueOf(12345);
        System.out.println(ms);

        ms = MyString.valueOf(12345.12313);
        System.out.println(ms);
    }

}

Outras dicas

Rather than trying to solve this problem for every possible source datatype you should be just concentrating on constructing your class from a String. Then all you have to do is delegate this method and all the others you haven't done yet to the corresponding String method in each case, take the resulting String, and construct your object with that String.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top