-1

This is the task:

Implement a static-public method named "createDouble" in the class "Functionality.java". The method gets two integer values a and b as input and should transform them to a double value and return it as follows:

  • The first input value a, should be placed before the comma or dot.
  • The second input value b, should be after the comma or dot and superfluous zeros should be removed.

No imports may be used to solve this task. Also the use of the Math library or other libraries is prohibited. Implement an algorithm that contains at least one meaningful loop.

This was my idea:

public class Functionality {

    public static double createDouble(int a, int b) {
        double c = b;
        double d = 1;
        while (c >= 1) {
            c /= 10;
            d *= 10;
        }
        return a + b/d;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(createDouble(12, Integer.MAX_VALUE));
    }
}

The problem is I am using the Method Integer.MAX Value which I shouldn´t use. Is there another option to write this code ?

nkrivenko
  • 1,142
  • 3
  • 11
  • 19
  • I don't understand why your teacher wants you to use a loop. This project is pure bit-manipulation.You're converting two four-byte values into a single 8-byte value. – NomadMaker Nov 05 '20 at 15:38
  • I mean, probably because they are learning about loops and bit manipulation is further down the line, no? – Koenigsberg Nov 05 '20 at 15:40
  • There's a formula for converting a decimal into a fraction, two integers. It's iterative and suspect this is the inverse. – markspace Nov 05 '20 at 15:41
  • Markspace do yo mean the IEE754 ? – TheRealChickenWing Nov 05 '20 at 15:43
  • 1
    I do not believe the IEEE754 would be appropriate here. Dealing with offsets and implied bits is probably beyond the scope of this exercise. – WJS Nov 05 '20 at 15:45
  • @TheRealChickenWing no I mean a mathematical algorithm, not a specification. – markspace Nov 05 '20 at 15:45
  • It was the first lecture in Java Programming. The topics were loops, typcasting, variables datatyps and Methods (private, public) – TheRealChickenWing Nov 05 '20 at 15:46
  • I've tried to deal with floating point numbers as bits and it's harder than it looks. After each operation, the mantissa must be (properly) normalized. That is complex and if not done properly will result in a number that doesn't behave as expected. Short answer: don't reinvent the wheel. – markspace Nov 05 '20 at 15:47
  • @TheRealChickenWing Instead of Integer.MAX_VALUE you can define your own as max = (1<<31)-1; Noet that 1<<31 is Integer.MIN_VALUE. Not quite certain why you need them though. – WJS Nov 05 '20 at 15:47
  • Your solution doesn't use `Integer.MAX_VALUE` though, so what's the problem? – OrangeDog Nov 05 '20 at 16:06

2 Answers2

0

Your code looks sound, just a small tweek I would make. Your variable c will equal what you want b to be after it's done dividing so you can just use it directly. Otherwise, you don't really need to be using Integer.MAX_VALUE at all. Just use arbitrary values.

public class Functionality
{
  public static double createDouble(int a, int b) {
    double c = b;
    while(c >= 1)
      c /= 10;
    return a + c;
  }
  
  public static void main(String[] args) {
    System.out.println(createDouble(15, 351));
    System.out.println(createDouble(32, 8452));
  }
}

Output:

15.351
32.8452
Tim Hunter
  • 586
  • 2
  • 10
-1

Here my Implementation

public class Functionality {
    private static double logb10(double num){
        return (num > 1) ? 1 + logb10(num / 10) : 0;
    }
    public static double createOtherDouble(int a, int b) {
        double c = a;
        int len =(int) logb10(b);
        double d = b;
        for(int i = 0; i < len; i++){
            d /= 10;
        }
        return c + d;
    }
     public static void main(String []args){
        System.out.println(Integer.MAX_VALUE);
        System.out.println(createOtherDouble(12, Integer.MAX_VALUE));
     }
}