Question

In: Computer Science

Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...

Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals.

Be sure to touch on these when solving for the solution:

  • Pad the right side
  • Pad the left side
  • Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points)
  • Make sure to remove trailing 0’s
  • Chunk out each step into it’s own function *NOT REQUIRED BUT CODE LOOKS CLEANER DOING SO*

Please do the solution in either JavaScript or Ruby so that I can understand better.

// let testCase_string1 = '3.92';

// let testCase_string2 = '0.09';

// let testCase2_string1 = '103.00000000909';

// let testCase2_string2 = '0.0000000019101';

// let testCase3_string1 = '1099.999999999990001';

//let testCase3_string2 = '10000000.000000000000000201000';

Solutions

Expert Solution

Please find the code below::

AddDecimalAsString.java

   package classes3;
  
   import java.util.Arrays;
  
   public class AddDecimalAsString {
  
       static String addString(String num1,String num2){
  
           String carry="";
           String result="";
  
           String number1[] = num1.split("\\.");
           String number2[] = num2.split("\\.");
  
  
           if(number1[0].length()>number2[0].length()){
               String zeros="";
               for(int i=0;i<number1[0].length()-number2[0].length();i++){
                   zeros +="0";
               }
               number2[0] = zeros+number2[0];
           }else if(number2[0].length()>number1[0].length()){
               String space="";
               for(int i=0;i<number2[0].length()-number1[0].length();i++){
                   space +="0";
               }
               number1[0] = space+number1[0];
           }
  
  
           if(number1[1].length()>number2[1].length()){
               String zeros="";
               for(int i=0;i<number1[1].length()-number2[1].length();i++){
                   zeros +="0";
               }
               number2[1] = number2[1]+zeros;
           }else if(number2[1].length()>number1[1].length()){
               String space="";
               for(int i=0;i<number2[1].length()-number1[1].length();i++){
                   space +="0";
               }
               number1[1] = number1[1]+space;
           }
  
  
           num1 = number1[0]+"."+number1[1];
           num2 = number2[0]+"."+number2[1];
  
  
           System.out.println("Number 1 : "+num1);
           System.out.println("Number 2 : "+num2);
  
           int d1,d2,res,car=0;
           for(int i=num1.length()-1;i>=0;i--){
               d1=0;
               d2=0;
               if(num1.charAt(i)=='.'){
                   result = "."+result;
                   continue;
               }
  
               d1 = Integer.parseInt(num1.charAt(i)+"");
               d2 = Integer.parseInt(num2.charAt(i)+"");
               res = d1+d2+car;
               car = 0;
               if(res>=10){
                   res = res-10;
                   car = 1;
               }
  
               if(car==1){
                   carry="1"+carry;
               }else{
                   carry=" "+carry;
               }
               result = res+result;
           }
  
           if(car!=0){
               result = car+result;
           }
  
  
           System.out.println();
           System.out.println(">> Output : ");
           System.out.printf("%20s%20s\n"," Carry:",carry+" ");
           System.out.printf("%20s%20s\n","AUGEND:",num1);
           System.out.printf("%20s%20s\n","ADDEND:",num2);
           System.out.printf("%20s%20s\n"," "," --------------------");
           System.out.printf("%20s%20s\n","SUM:",result);
  
  
  
           return result;
       }
       public static void main(String[] args) {
           System.out.println("\n\nReturned result is : "+addString("00.0000001","999999.9999999"));
       }
   }

output:


Related Solutions

Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT