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;

public class AddDecimalAsString {
  
       static String addString(String num1,String num2){
  
           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;
               }
               result = res+result;
           }
  
           if(car!=0){
               result = car+result;
           }
  
  
           //removing trailing zeros
          
           String finalres ="";
           boolean num= false;
           for(int i=result.length()-1;i>=0;i--){
               if(result.charAt(i)=='0' && !num){
                   continue;
               }else{
                   finalres = result.charAt(i)+finalres;
                   num = true;
               }
           }
          
           return finalres;
       }
       public static void main(String[] args) {
           System.out.println("\n\nReturned result is : "+addString("1099.999999999990001","10000000.000000000000000201000"));
       }
   }

output:


Related Solutions

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...
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...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
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 method that takes four strings as parameter. The first string should be a pokemon...
Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type, the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. In this case you can use fire, water, and leaf. Example outputs should be like: Example: Pokemon X (Water) has the advantage over Pokemon Y (fire) Pokemon X (Fire) has...
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".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT