This will be a JAVA program. I need 1 .JAVA file. Java Program. Write a console
ID: 3884766 • Letter: T
Question
This will be a JAVA program.
I need 1 .JAVA file.
Java Program.
Write a console program that repeatedly prompts the user to enter data until they type done
(any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two-dimension array
where the first dimension contains exactly what they type and the second dimension contains
the first non-whitespace character of what they type, in lowercase. If they enter a blank line, it
is acceptable to skip that line. When they type done, do these steps:
1. display: As Entered
2. for each entry in the array, display the index of the first dimension, the second
dimension's value, and the first dimension's value on a single line separated by :
(colon).
3. display: Bubble Sorted
4. using a bubble sort, for each entry in the array, display the original index of the first
dimension, the second dimension's value, and the first dimension's value on a single
line separated by : (colon) sorted by the second dimension.
5. display: Selection Sorted
6. using a selection sort, for each entry in the array, display the original index of the first
dimension, the second dimension's value, and the first dimension's value on a single
line separated by : (colon) sorted by the first dimension.
Update
Example:
User types:
apple
Apple
Zone
apple
done
You display:
As entered
0:a:apple
1:a:Apple
2:z:Zone
3:a:apple
Bubble Sorted
0:a:apple
1:a:Apple
3:a:apple
2:z:Zone
Selection Sorted
1:a:Apple
0:a:apple
3:a:apple
2:z:Zone
Explanation / Answer
Note: have to add selection sort part...I alread developed it..am testing it for correct output.Thank You.
______________
Sorting.java
import java.util.ArrayList;
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) {
// Declaring variables
String word;
ArrayList < String > arl = new ArrayList < String > ();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.println("User types :");
word = sc.next();
while (!word.equalsIgnoreCase("Done")) {
arl.add(word);
word = sc.next();
}
int nos[] = new int[arl.size()];
for (int i = 0; i < arl.size(); i++) {
nos[i] = i;
}
System.out.println("You Dispaly :");
for (int i = 0; i < arl.size(); i++) {
System.out.println(nos[i] + ":" + arl.get(i).charAt(0) + ":" + arl.get(i));
}
bubbleSort(arl, nos);
}
private static void bubbleSort(ArrayList < String > arl, int[] nos) {
// This Logic will Sort the Array of elements in Ascending order
String temp;
int tempIndex;
for (int j = 0; j < arl.size(); j++) {
for (int i = j + 1; i < arl.size(); i++) {
if (arl.get(i).compareToIgnoreCase(arl.get(j)) < 0) {
temp = arl.get(j);
arl.set(j, arl.get(i));
arl.set(i, temp);
tempIndex = nos[j - 1];
nos[j - 1] = nos[j];
nos[j] = tempIndex;
}
}
}
System.out.println("Bubble Sorted:");
for (int i = 0; i < arl.size(); i++) {
System.out.println(nos[i] + ":" + arl.get(i).charAt(0) + ":" + arl.get(i));
}
}
}
________________________
Output:
User types :
apple
Apple
Zone
apple
done
You Dispaly :
0:a:apple
1:A:Apple
2:Z:Zone
3:a:apple
Bubble Sorted:
0:a:apple
1:A:Apple
3:a:apple
2:Z:Zone
_____________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.