Question

In: Computer Science

Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String...

Using java:
Write a class called StringAlternateSorting

  what it does:

  1. ask user for one String (String1) (must be sorted)
  2. ask user for second String (String2) (must be sorted)

  3. Then prints all the characters of two previous Strings, in such a
  way that they are sorted (see examples below).

  4. consider the possibility that strings are of different sizes, one
  or both can be empty, etc.

  5. strings can contain any kind of characters as long as they are sorted

EXAMPLES

% java StringAlternateSorting.java
enter first string: 1357
enter second string: 2468
12345678
% java StringAlternateSorting.java
enter first string: 1
enter second string: 2345678
12345678
% java StringAlternateSorting.java
enter first string: 23456
enter second string: 1
123456
% java StringAlternateSorting.java
enter first string: 12345
enter second string: 9
123459
% java StringAlternateSorting.java
enter first string: 9
enter second string: 12345
123459
% java StringAlternateSorting.java
enter first string: 19
enter second string: 2345
123459
% java StringAlternateSorting.java
enter first string: 2345
enter second string: 19
123459
% java StringAlternateSorting.java
enter first string: 
enter second string: 12345
12345
% java StringAlternateSorting.java
enter first string: 12345
enter second string: 
12345
% java StringAlternateSorting.java
enter first string: acef
enter second string: bdghi
abcdefghi
% java StringAlternateSorting.java
enter first string: 12345
enter second string: 13579
1123345579
% 
  

Solutions

Expert Solution


ANSWER:-

CODE:-

import java.util.*;
class StringAlternateSorting{
   public static String alternateSort(String s1, String s2){
       String out = "";
       int i=0,j=0;
       // length of first string
       int len1 = s1.length();
       // length of secong string
       int len2 = s2.length();
       while (i<len1 && j<len2) {
           // characters of both strings
           char ch1 = s1.charAt(i);
           char ch2 = s2.charAt(j);
           // if character1 is less than
           // character2 add it to out string
           // and increase i
           // if character2 is less than
           // character1 add it to out string
           // and increase j
           if(ch1 <= ch2){
               out += ch1;
               i++;
           }else{
               out += ch2;
               j++;
           }
       }
       // This is for handling
       // different string length
       // add remaining characters in longer
       // length string to out string.
       while (i<len1) {
           out += s1.charAt(i++);
       }
       while (j<len2) {
           out += s2.charAt(j++);
       }
       // return out string
       return out;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       // Read strings from user.
       System.out.print("enter first string: ");
       String s1 = sc.nextLine();
       System.out.print("enter second string: ");
       String s2 = sc.nextLine();
       // call alternateSort method
       System.out.println(alternateSort(s1,s2));
   }
}

NOTE:- If you need any modifications in the code,please commene below.Please give positive rating.THUMBS UP.             

                 THANK YOU!!!!

OUTPUT:-

CODE SCREENSHOT:-


Related Solutions

Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation.
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Here is the code that I have so far I can't seem to get it...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation. This is the code I have from the...
ComputeAverage Write a class called ComputeAverage what it does: asks the user to enter three double...
ComputeAverage Write a class called ComputeAverage what it does: asks the user to enter three double numbers (see examples below), it uses Scanner class to read from standard input each one of the doubles entered by the user. it then prints the average of the three numbers. Suggested steps: 1. prompt the user to enter each of the three doubles by printing. 2. read each of the three doubles using a Scanner. Remember you need to declare a Scanner variable...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT