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

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: ----------------------...
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 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!!
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,...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Write a program that lets the user enter the loan amount, number of years, and interest...
Write a program that lets the user enter the loan amount, number of years, and interest rate and displays the amortization schedule for the loan. Steps: 1) Create scanner 2) Prompt the user to enter loan amount and declare double variable and relate to scanner input 3) Prompt the user to enter number of years and declare integer years and relate to scanner input 4) Prompt the user to enter annual interest rate and declare double variable and relate to...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT