Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adding and Subtracting positive or negative two numbers of any length with strings

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding and Subtracting positive or negative two numbers of any length with strings

    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 done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
    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 ( );

    This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method



    import java.util.Scanner;
     
    public class Number{
        private String whole;
        private String decimal;
        private String sign;
     
     
        public static void main (String[] args){
     
    		System.out.println("Enter two numbers");
    		Scanner keyboard = new Scanner(System.in);
    		Double firstNumber = keyboard.nextDouble();
    		Double secondNumber = keyboard.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 Number (double n){      
            whole = "0";
            decimal = "0";
            sign = "+";
            String temp = new Double(n).toString();
            if (temp.charAt(0) == '-'){
                sign = "-";
                temp = temp.substring(1); 
            }
            int pos = temp.indexOf(".");
            if (pos == -1)
                whole = temp;
            else
            {
                whole = temp.substring (0,pos);
                decimal = temp.substring (pos+1);
            }
        }
     
        public Number add (Number RHS){ 
            this.alignWhole (RHS);
            this.alignDecimal (RHS);
            return this.addNum (RHS);
        }
     
        private void alignWhole (Number RHS){
            int firstWholeL = this.whole.length();
            int secondWholeL = RHS.whole.length();
            int dif = firstWholeL - secondWholeL;
            if (dif > 0){      
                for (int i = 1; i <= dif; i++)
                    RHS.whole = "0" + RHS.whole;    
            }
            else if (dif < 0)
            {
                dif = Math.abs (dif);
                for (int i = 1; i <= dif; i++)
                    this.whole = "0" + this.whole;    
            }       
        }
     
        private void alignDecimal (Number RHS) {
            int firstDecimalL = this.decimal.length ( );
            int secondDecimalL = RHS.decimal.length ( );
            int dif = firstDecimalL - secondDecimalL;
     
            if (dif > 0)
            {
                for (int i = 1; i <= dif; i++)
                    RHS.decimal = RHS.decimal + "0";
            }
     
            else if (dif < 0)
            {
                dif = Math.abs (dif);
                for (int i = 1; i <= dif; i++)
                    this.decimal = this.decimal + "0";
            }
     
        }
     
     
     
        private Number addNum (Number RHS){
          Number sum = new Number();
            sum.decimal = "";     
            int carry = 0;
            int decimalL = this.decimal.length();
            for (int i = decimalL -1; i >= 0; i--)
            {
                char firstC = this.decimal.charAt(i);
                char secondC = RHS.decimal.charAt(i);
                int sumX = (firstC - 48) + (secondC - 48) + carry;
                char sumC = (char)((sumX % 10) + 48);
                carry = sumX / 10;
                sum.decimal = sumC + sum.decimal;   
            }   
     
          sum.whole = "";     
          int wholeL = this.whole.length();   
            for (int i = wholeL -1; i >= 0; i--)
            {
                char firstC = this.whole.charAt (i);
                char secondC = RHS.whole.charAt (i);
                int sumX = (firstC - 48) + (secondC - 48) + carry;
                char sumC = (char)((sumX % 10) + 48);
                carry = sumX / 10;
                sum.whole = sumC + sum.whole;
            }
     
            if (carry != 0)
                sum.whole = "1" + sum.whole;
     
               return sum;
        }
     
     
        public Number Subtract (Number RHS){ 
            this.SubtractalignWhole (RHS);
            this.SubtractalignDecimal (RHS);
            return this.subNum (RHS);
        }
     
        private void SubtractalignWhole (Number RHS){
            int firstWholeL = this.whole.length();
            int secondWholeL = RHS.whole.length();
            int dif = firstWholeL - secondWholeL;
            if (dif > 0){
                for (int i = 1; i <= dif; i++)
                    RHS.whole = "0" + RHS.whole;
            }
            else if (dif < 0)
            {
                dif = Math.abs (dif);
                for (int i = 1; i <= dif; i++)
                    this.whole = "0" + this.whole;
            }       
        }
     
        private void SubtractalignDecimal (Number RHS) {
            int firstDecimalL = this.decimal.length ( );
            int secondDecimalL = RHS.decimal.length ( );
            int dif = firstDecimalL - secondDecimalL;
     
            if (dif > 0)
            {
                for (int i = 1; i <= dif; i++)
                    RHS.decimal = RHS.decimal + "0";
            }
     
            else if (dif < 0)
            {
                dif = Math.abs (dif);
                for (int i = 1; i <= dif; i++)
                    this.decimal = this.decimal + "0";
            }
     
        }
     
     
        private Number subNum (Number RHS){
            Number subtract = new Number();
     
              subtract.decimal = "";     
              int carry = 0;
              int decimalL = this.decimal.length();
              for (int i = decimalL -1; i >= 0; i--)
              {
                  char firstC = this.decimal.charAt(i);
                  char secondC = RHS.decimal.charAt(i);
                  int sumX = (firstC - 48) - (secondC - 48) + carry; 
                  char sumC = (char)((sumX % 10) + 48);
                  carry = sumX / 10;
                  subtract.decimal = sumC + subtract.decimal;   
              }   
     
            subtract.whole = "";     
            int wholeL = this.whole.length();   
              for (int i = wholeL -1; i >= 0; i--)
              {
                  char firstC = this.whole.charAt (i);
                  char secondC = RHS.whole.charAt (i);
                  int sumX = (firstC - 48) - (secondC - 48) + carry; 
                  char sumC = (char)((sumX % 10) + 48);
                  carry = sumX / 10;
                  subtract.whole = sumC + subtract.whole;
              }
     
              if (carry != 0)
                  subtract.whole = "1" + subtract.whole;
     
                 return subtract;
     
          }
     
     
        public String toString()
        {
            return sign + whole + "." + decimal;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,077
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    Do you have any specific questions?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    My question is how to write the code to add and subtract positive and negative numbers. For example: -29.896 + 3.54

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,077
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    how to write the code to ...
    That's not a specific question. Can you break it down into something more specific.
    Take one of the problems: how to add two numbers.
    What are the steps the code must do for that?
    At what step are you having problems?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    Quote Originally Posted by Norm View Post
    That's not a specific question. Can you break it down into something more specific.
    Take one of the problems: how to add two numbers.
    What are the steps the code must do for that?
    At what step are you having problems?

    How to add any two numbers? ex: -10+4
    What are the steps?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,077
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    Write an expression and assign it to a variable:
    int value = -10+4;
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,077
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Adding and Subtracting positive or negative two numbers of any length with strings

    Also posted at: Adding and Subtracting positive or negative two numbers of any length with strings
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. (Java) Help with alternating negative and positive numbers.
    By fcknDan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 15th, 2013, 11:48 PM
  2. catch negative numbers
    By garry in forum Exceptions
    Replies: 6
    Last Post: November 8th, 2013, 09:05 PM
  3. Have trouble dealing with negative numbers.
    By Arick Cheng in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 29th, 2012, 07:41 AM
  4. This java program is adding up in a negative way....
    By seaofFire in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 04:58 PM
  5. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM