0

I'm supposed to create a program where a user enters two numbers that could be negative or positive and could contain decimal places or not. Theoretically, when you add say "256.78 + 78.6783" it is supposed to carry the one like a normal addition problem and finish the operation.

I have figured out how to add numbers of any length only when they are positive which took me FOREVER, but when I add negative numbers or even subtract the number I don't get the correct result. This is supposed to work with any set of two numbers that a user enters.

Here is my code so far, any suggestions?
P.S. I'm NOT allowed to convert these numbers to int's or double's before the operation so Parsing them is out of the question.

public class Number {
    static Scanner kbd = new Scanner (System.in);
    private String sign; 
    private String whole;
    private String decimal;
    private String fraction;
    private static double firstNumber;
    private static double secondNumber;

    public static void main(String[] args) {

        System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextDouble();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextDouble();

        Number x = new Number (firstNumber);
        Number y = new Number (secondNumber);
        Number sum = x.add(y);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("x + y = " + sum);
        Number subtract = x.subtract(y);
        System.out.println("x - y = " + subtract);
    }
    public Number() 
    {
        whole = "0";
        decimal = "0";
        sign = "+";
    }
    public String toString() 
    {
        return sign + whole + "." + decimal;
    }
    public Number (double n) 
    {
        whole = "0";
        decimal = "0";
        sign = "+";

        String numString = new Double(n).toString();
        if (numString.charAt(0) == '-') {
            sign ="-";
            numString = numString.substring(1);
        }
        int position = numString.indexOf(".");
        if (position == -1)
            whole = numString;
        else
        {
            whole = numString.substring(0,position);
            decimal = numString.substring(position+1);
            fraction = "";
        }
    }

    public Number add (Number RHS) {
        this.fixWhole (RHS);
        this.fixDecimal(RHS);
        return this.addNum (RHS);
    }
    public Number subtract (Number RHS) {
        this.fixWhole(RHS);
        this.fixDecimal(RHS);
        return this.subtractNum (RHS);
    }
    private void fixWhole (Number RHS) {
        int firstWholeNum = this.whole.length();
        int secondWholeNum = RHS.whole.length();
        int difference = firstWholeNum - secondWholeNum;
        if (difference > 0) {
            for (int i = 1; i <= difference; i++) 
                RHS.whole = "0" + RHS.whole;
        }
        else if (difference < 0 ) {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i++)
                this.whole = "0" + this.whole;
        }
    }

    private void fixDecimal  (Number RHS ) {
        int firstDecimalNum = this.decimal.length();
        int secondDecimalNum = RHS.decimal.length();
        int difference = firstDecimalNum - secondDecimalNum;

        if (difference > 0) {
            for (int i = 1; i <=  difference; i++)
                RHS.decimal = RHS.decimal + "0";
        }
        else if (difference < 0 ) 
        {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i ++)
                this.decimal = this.decimal + "0";
        }
    }

    private Number addNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) + (c2 - 48) + carry;
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit + 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) + (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }

    private Number subtractNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) - (c2 - 48) - carry; 
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit - 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) - (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }
}
4
  • Im not sure what you mean you cant parse them. You need to parse the string one way or the other, infact in your current approach you are already parsing the numbers. Commented Sep 6, 2014 at 11:42
  • @for3st: not entirely true. Ultimately, yes, at some point the string input needs to be converted to a 'number', if only because it needs to be added/subtracted. But the OP's assignment is clearly to do so one character at a time. That is not the case with wholesale conversion to floats. The purpose of this assignment seems to be to design BigNum functionality from scratch. Commented Sep 6, 2014 at 11:48
  • (char) (sumDigit - 48) + sum.decimal; will work better with a +: sumDigit + 48. (You can also do sumDigit + '0' for clarity). Also can you provide the algorithm of the substraction in english (not java)? There is clearly something wrong in your implementation of the substraction. Commented Sep 6, 2014 at 11:55
  • How did you do this in elementary school? Every + or - operation can be converted to a addition of 2 positive values or a substraction of 2 positive values, where the minuend is not smaller than the subtrahent, by changing signs. (If you change signs, you'll need to make sure the result has the right sign afterwards) Commented Sep 6, 2014 at 11:57

1 Answer 1

2

take both the numbers as strings and store the signs into the sign string into the corresponding Numbers objects and call your method like

System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextString();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextString();

        Number x = new Number (firstNumber.substring(1),firstNumber.charAt(0));
        Number y = new Number (secondNumber.substring(1),secondNumber.charAt(0));

/*convert the firstNumber.substring(1) and secondNumber.substring(1) to doubles using  Double.parseDouble()*/

public String doTheOperation(Number other){
 if(this.sign.equals(otherNumber.sign)){
  /*simply the double values and put the sign*/ in front of it and return it
 }

 else{
  do the simple double subtraction and by looking at your code i believe you can find out the bigger double among them
 }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, i'm not supposed to use parse to do the operations, my assignment is to do the arithmetic character by character. also, do you mean do input checking where if the user enters a negative number i call the method that does the operation for negative numbers?
can you find out which number is bigger and which is smaller if you cant i will tell you the logic but tell me first can you find out in that case it will be easy to tell you the answer if you can find out the bigger one so you only need to concentrate on the sign if signs are different subtract the smaller one from bigger one
actually, no i don't know how to do that. do you do something with loops maybe? i'm really stuck, any help would be greatly appreciated!
ok read the numbers as strings as i have shown in my answer so now to get the bigger number follow the steps 1.lets say you have two numbers as n1 and n2 so now check the numbers of digits before the point so so you will come to know which number is having the less digits will be smaller if the digits are same then check from the begining if the first digit is same look for second digit and so on.
When i do that i get an error saying i can't substring a double seeing as my numbers are in double.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.