{ /** * * **/ public static void main (String [] args) { charchoice; // Repeat p
ID: 3616374 • Letter: #
Question
{
/**
*
*
**/
public static void main (String [] args)
{
charchoice; // Repeat program
String str =""; // Inputsentence
StringinputStr; // repeatinput
String sAnagram =null; //Tmp array w/ repeats
Scanner scan = newScanner(System.in);
do
{
System.out.print("Enter your words to (case incensitive) sort:");
inputStr =scan.nextLine(); //Read message
ascendSort(inputStr); //Ascending sorted words
//System.err.println("The input string is:"+inputStr);
for(int i= 0; i< str.length(); ++i)
System.out.print("Print ascending order: " + inputStr);
descendSort(inputStr); //Descending sorted words
for(int j =0; j <str.length(); j++)
System.out.println("Print in descending order: " + inputStr);
System.out.print(" Want more sorting (y/n)? ");
str =scan.next(); // Read and assign to String
choice =str.charAt(0); // Assign to character
if(scan.hasNextLine()) //<Enter> character
str = scan.nextLine(); // Readline, discard
} while(choice != 'n' &&choice != 'N'); //Repeat until n or N input
scan.close(); //close this scanner
System.exit(0);
}
/**
*
*
**/
public static void ascendSort (Stringwords)
{ //Bubble Sort words
String tmpStrg = " ";
String [] a = words.split(""); // Array Strings delimit space
String [] orig = words.split(""); // Original array
//Display original words
for (int i = 0; i < a.length-1;++i)
{
for (int j =a.length-1; i<j; --j)
{
if(a[j-1].compareTo(a[j])<0)
{
tmpStrg = a[j-1];
a[j-1]=a[j];
a[j] = tmpStrg;
}
}
}
}
/**
*
*
**/
public static void descendSort(String words)
{ /** Bubble sort words in descending order */
String tmpStrg = " ";
String [] a = words.split(""); // Array Strings delimit space
String [] orig = words.split(""); // Original array
//Display original words
for (int i = 0; i < a.length;i++)
{
for (int j = i; j< a.length; j++)
{
if(a[i].compareTo(a[j])<0)
{
tmpStrg = a[j];
a[i]=a[j];
a[j] = tmpStrg;
}
}
}
}
}
Explanation / Answer
import java.io.*; import java.lang.*; import java.util.*; public class Sort { /** * * **/ /** * * **/ public static void ascendSort (Stringwords) { //Bubble Sort words String tmpStrg = " "; String [] a = words.split(""); // Array Strings delimit space String [] orig = words.split(""); // Original array //Display original words for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.