Question

In: Computer Science

Write a java program that lets the user to enter any two integers and weave them...

Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of x followed by the second-to-the-last digit of y. And so on.

You should write weaveNumber method that works as following examples weaveNumber( 271 ,389).should return 237819. If one of the numbers has more digits than the other, you should imagine that leading zeros are used to make the numbers of equal length. For example, weaveNumber(2384, 12) should return 20308142 (as if it were a call on weaveNumber(2384, 0012)). Similarly, weaveNumber(9, 318) should return 30198 (as if it were a call on weaveNumber(009, 318)).

Solutions

Expert Solution

Complete java program:

import java.util.Scanner;
public class WeaveNumberFinder {

   public static void main(String[] args) {
       //Scanner class object to read inputs
       Scanner scan = new Scanner(System.in);
      
       //asking inputs
       System.out.print("Enter first integer:");
       int num1 = scan.nextInt();
       System.out.print("Enter second integer:");
       int num2 = scan.nextInt();
      
       //calling weaveNumber() method
       int weaveNum = weaveNumber(num1,num2);
       System.out.println("Weave number:"+weaveNum);
   }
  
   //method implementation
   public static int weaveNumber(int num1,int num2) {
      
       //convert two numbers to two different strings
       String numStr1 = String.valueOf(num1);
       String numStr2 = String.valueOf(num2);
      
       StringBuilder sb1= new StringBuilder();
       StringBuilder sb2= new StringBuilder();
       String finalString = new String();
      
       int diffDigits=0;
       if(numStr1.length() > numStr2.length()) {
           //find out the difference of digits
           diffDigits = numStr1.length() - numStr2.length();
          
           //append diffDigits number of '0' to the sb2
           for(int i=1;i<=diffDigits;i++) {
               sb2.append("0");
           }
          
           //append all digits of num2 to sb2
           for(int i=0;i<numStr2.length();i++) {
               sb2.append(numStr2.charAt(i));
           }

           //preparing final string-concatenating both strings
           for(int i=0;i<numStr1.length();i++) {
               finalString = finalString +numStr1.charAt(i)+ sb2.charAt(i);
           }
       }else if(numStr2.length() > numStr1.length()) {
          
           //find out the difference of digits
           diffDigits = numStr2.length() - numStr1.length();
          
           //append diffDigits number of '0' to the sb1
           for(int i=1;i<=diffDigits;i++) {
               sb1.append("0");
           }
           //append all digits of num1 to sb1
           for(int i=0;i<numStr1.length();i++) {
               sb1.append(numStr1.charAt(i));
           }
          
           //preparing final string-concatenating both strings
           for(int i=0;i<numStr2.length();i++) {
               finalString = finalString+ sb1.charAt(i)+numStr2.charAt(i);
           }
       }else {
           //if both numbers have equal number of digits
          
           //preparing final string-concatenating both strings
           for(int i=0;i<numStr1.length();i++) {
               finalString = finalString+numStr1.charAt(i)+ numStr2.charAt(i);
           }
       }
      
       //converting string to int and return the same value to main
       return Integer.parseInt(finalString);
   }
}
Output:


Related Solutions

JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Write a program that allows the user to enter two integers and a character If the...
Write a program that allows the user to enter two integers and a character If the character is A, add the two integers If it is S, subtract the second integer from the first else multiply the integers Display the results of the arithmetic
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
Instructions Write a program that prompts the user to enter 50 integers and stores them in...
Instructions Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
JAVA Write a program that will compare two names. The program prompts the user to enter...
JAVA Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal.  Check also if there...
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT