
Respuesta :
Answer:
Required code and output is given below:
Explanation:
import java.util.Scanner;
public class HomworkAssignment3_1
{
 public static void main(String[] args)
 {
   String str;
   Scanner console=new Scanner(System.in);
   System.out.printf("Input: ");
   //read input from user
   str=console.nextLine(); Â
   //check if length of the string is above 50
   if(str.length()>50)
   {
     System.out.println("Input string size must be less than 50.");
   }
   //check if string is null or empty
   else if(str==null || str.equals(""))
   {
     System.out.println("Input string should not be null or empty.");
   }
   //call method isvalid that takes string,str is not true
   else if(!isvalid(str))
   {
     System.out.println("Input string must contains only a and b letters");
   }
   else
   {
     //create an instance of Solution
     Solution solution=new Solution();
     //call the method,separateLetters
     System.out.println("Output: "+solution.separateLetters(str));
   }   Â
 }
 /*The method, isvalid that takes string and
 * return true if the str is valid otherwise
 * returns false.*/
 public static boolean isvalid(String str)
 {
   boolean valid=true;
   for (int i = 0; i < str.length() && valid; i++) {
     if(str.charAt(i)!='a' && str.charAt(i)!='b')
       valid=false;
   }
   return valid;
 } //end of the method isvalid
} //end of the class
------------------------------------------------------------------------------------------------------------------------
//Solution.java
public class Solution
{
 /*The method that sorts the */
 public String separateLetters(String input)
 {
   //convert the string to character array
   char ch[]=input.toCharArray();
   //apply sorting algorithm
   for (int outer = 0; outer < input.length(); outer++)
   {
     for (int inner = 0; inner < input.length()-outer-1; inner++)
     {
       if(ch[inner]>ch[inner+1])
       {
         char temp=ch[inner];
         ch[inner]=ch[inner+1];
         ch[inner+1]=temp;
       }
     } Â
   }
   //returns the sorted string
   return new String(ch);
 }
}
------------------------------------------------------------------------------------------------------------------------
Sample output:
Run1:
Input: bababa
Output: aaabbb
Run2:
Input: abbbbbbbbbaaaaaaaa
Output: aaaaaaaaabbbbbbbbb