Results 1 to 10 of 10
  1. #1
    beginner234 is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    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 ( );

    The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction . help

    Java Code:
    
    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 String toString()
        {
            return sign + whole + "." + decimal;
        }
    }
    Last edited by beginner234; 02-05-2014 at 10:59 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

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

    That's not how it works. You need to make a start, not just dump the homework and ask for help.

    You will probably first want to write out on a piece of paper what your code needs to do. It will probably help to use the addition code that is given to you as a source of inspiration. Which probably means that you first need to figure out how that code works and why it does what it does.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

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

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,030
    Rep Power
    4

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

    To the OP: when the first words of your post are "You are to design a Java application..." I *IMMEDIATELY* stop reading and stop caring. The "HOMEWORK ALERT" is triggered and it stops me from reading any further. If you can't handle the CS/Programming/Java class, then by all means drop it, but don't let us do your homework. At least pay me for it. Oh no, I won't do that either. I smell hamburgers and fries....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

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

    Quote Originally Posted by SurfMan View Post
    I smell hamburgers and fries....
    Damn you, now I smell them too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by gimbal2 View Post
    Damn you, now I smell them too.
    I do't sbell adythi'g because by dose is blocked ...

    ki'd regards,

    Jos *hatchoo!*
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

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

    Chili Con Carne with extra chili (or Chili Con Pollo, equally tasty). That will clear your nose up right away, guaranteed.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by gimbal2 View Post
    Chili Con Carne with extra chili (or Chili Con Pollo, equally tasty). That will clear your nose up right away, guaranteed.
    Ok, by dose is full of chilli beads; dow what?

    ki'd regards,

    Jos (<--- still full of it; maybe Dr. JosAHs jenever therapy will help)
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

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

    *sings* Mr booze, mr booze. Mister b double o z e that sure spells boohoooooooooze...

    Its Friday!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by gimbal2 View Post
    *sings* Mr booze, mr booze. Mister b double o z e that sure spells boohoooooooooze...

    Its Friday!
    Oh dear, the poor soul must've lost his mind; quick! finish him off!

    kindest regards,

    Jos (<--- switches on his chainsaw to perfom his noble deed)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 04-15-2013, 12:33 AM
  2. convert negative int to positive
    By aconti in forum New To Java
    Replies: 4
    Last Post: 05-30-2011, 10:43 AM
  3. Negative,Positive and Zero
    By dipowo in forum New To Java
    Replies: 2
    Last Post: 04-04-2011, 06:49 AM
  4. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 07:26 PM
  5. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 11:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •