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

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()
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...
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.
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...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
*****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...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT