0

I need some help on a program i'm supposed to create. i'm supposed to create a program that reads two strings of any length that are user inputted and it subtracts or adds them together. i'm NOT allowed to convert these strings into numbers before the operation. this is what I got so far.My teacher mentioned something like converting the strings into uni-code to add and subtract them but i have no idea how to do it as we haven't even learned uni-code. HERE IS MY CODE:

import java.util.Scanner;
public class Number {
    private char Sign;
    private String Whole;
    private String Fraction;

    public static void main(String[] args) {
        Scanner Keyboard = new Scanner (System.in);
        System.out.println("This program adds or subtracts numbers of any lengths, please add two numbers: ");
        String num1 = Keyboard.nextLine();
        System.out.println("Enter the second number: ");
        String num2 = Keyboard.nextLine();

        String sum = " ";
        int length = num1.length();
        int carry = 0;

        public Number Add(Number RHS) {

            for (int i = length -1 ; i >= 0; i--) {
                char c1 = num1.charAt(i);
                char c2 = num2.charAt(i);

                int tempSum = (c1 - 48) + (c2 - 48) + carry;
                carry = tempSum / 10;

                int sumDigit = tempSum % 10;
                sum = (char) (sumDigit + 48) + sum;

                if (carry == 1) {
                    sum = "1" + sum;
                }
            }
        }
    }
    public Number (double n) {
        Whole = " ";
        Fraction = " ";
        if (n >= 0) {
            Sign = '+';
        }
        else 
        {
            Sign = '-';
            n = Math.abs(n);
            String numString = new Double(n).toString();
            int position = numString.indexOf(".");
        }

    }
}
5
  • You may want to left-pad with zeros so they have the same length. Commented Sep 2, 2014 at 14:34
  • 1
    I honestly couldn't understand the question, however I felt the need to point out that variables by convention are written in camelCase and never start with capital letters unless they're static final constants Commented Sep 2, 2014 at 14:40
  • Forget about Unicode. A Java string is a sequence of characters. Java represents characters by their Unicode code points, but you won't need to know that to complete the assigment. All you'll need to know is that '0'-'0'==0, '1'-'0'==1, and so on. Commented Sep 2, 2014 at 14:41
  • this is what the assignment is and i'm stuck. "You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be carried out by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation". Commented Sep 4, 2014 at 3:06
  • "The program must use a "Number" class which includes at least the following methods": Number(); Number (double n); Number add(Number RHS); Number subtract(Number RHS); String toString(); Commented Sep 4, 2014 at 3:07

1 Answer 1

2
public static String add(String as, String bs){
    ArrayList<String> BigNum = new ArrayList<>();
    int m = as.length()-1;
    int n = bs.length()-1;
    int min = m > n ? n : m ;
    int max = 0;
    String s;
    if(n  > m){
        s = bs;
        max = n;
    }else{s = as ; max = m;}

    Integer carry = 0;
    while(true){
        int a = Integer.parseInt(Character.toString(as.charAt(m)));
        int b = Integer.parseInt(Character.toString(bs.charAt(n)));
        Integer c = a + b + carry;
        if(c > 9){
            carry = 1;
            c %=10;
        }else carry = 0;

        BigNum.add(c.toString());
        n--; m--; min--; max--;

        if ( min < 0   ) {
            if(carry !=0 && max < 0 )
                BigNum.add(carry.toString());
            break;
        }
    }
    Integer c = carry;
    while(max >= 0) {
        c += Integer.parseInt(Character.toString(s.charAt(max)));
        BigNum.add(c.toString());
        c = 0;
        max--;
    }
    String s2 = "";
    for (int i = BigNum.size()-1; i >= 0; i--) {
        s2 +=BigNum.get(i);
    }
    return s2;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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