Certainly! Below is a Java program that fulfills the requirements of Activity #6 – Bubble Sort Using Arrays. This program uses only while loops for control structures, handles user input for ten integers, sorts them using the Bubble Sort algorithm, and allows the user to run the program multiple times based on their input.
import java.util.Scanner;
public class BubbleSortProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char choice = 'Y';
while (choice == 'Y' || choice == 'y') {
int[] numbers = new int[10];
int i = 0;
// Input ten integers
while (i < 10) {
System.out.print("Enter Number " + (i + 1) + " : ");
if (scanner.hasNextInt()) {
numbers[i] = scanner.nextInt();
i++;
} else {
System.out.println("Invalid input. Please enter an integer.");
scanner.next(); // Clear the invalid input
}
}
// Bubble Sort using while loops
int n = numbers.length;
boolean swapped;
int pass = 0;
do {
swapped = false;
int j = 0;
while (j < n - 1 - pass) {
if (numbers[j] > numbers[j + 1]) {
// Swap numbers[j] and numbers[j + 1]
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
swapped = true;
}
j++;
}
pass++;
} while (swapped);
// Display sorted integers
System.out.println("Sorted integers in ascending order:");
i = 0;
while (i < 10) {
System.out.println(numbers[i]);
i++;
}
// Prompt for another input
System.out.print("Input Another? [Y/N]: ");
choice = scanner.next().charAt(0);
System.out.println();
}
System.out.println("Program Terminated.");
scanner.close();
}
}
Explanation of the Program
-
Importing Scanner:
- The
Scanner class from java.util is used to capture user input.
-
Main Method:
- The
main method initializes the program, sets up the loop for repeated execution based on user input, and handles termination.
-
User Input:
- A
while loop is used to prompt the user to enter ten integers.
- Input validation ensures that only integers are accepted. If a non-integer is entered, the program notifies the user and prompts again.
-
Bubble Sort Implementation:
- The Bubble Sort algorithm is implemented using nested
while loops.
- The outer
do-while loop continues to run as long as a swap has occurred in the previous pass.
- The inner
while loop compares adjacent elements and swaps them if they are out of order.
- The
pass variable optimizes the sort by reducing the number of comparisons in each subsequent pass since the largest elements "bubble up" to the end of the array.
-
Displaying Sorted Numbers:
- After sorting, another
while loop is used to print each sorted integer on a new line.
-
Repeating the Process:
- The user is prompted with "Input Another? [Y/N]: " to decide whether to run the program again.
- The program continues or terminates based on the user's input (
Y/y to continue, any other input to terminate).
-
Termination:
- When the user decides not to continue, the program prints "Program Terminated." and closes the
Scanner resource.
Sample Output
Here is an example of how the program interacts with the user:
Enter Number 1 : 1
Enter Number 2 : 3
Enter Number 3 : 5
Enter Number 4 : 7
Enter Number 5 : 9
Enter Number 6 : 2
Enter Number 7 : 4
Enter Number 8 : 6
Enter Number 9 : 8
Enter Number 10 : 10
Sorted integers in ascending order:
1
2
3
4
5
6
7
8
9
10
Input Another? [Y/N]: Y
Enter Number 1 : ...
...
Input Another? [Y/N]: N
Program Terminated.
How to Use This Program
-
Compilation:
-
Execution:
- Run the compiled program using the command:
java BubbleSortProgram
-
Interacting with the Program:
- The program will prompt you to enter ten integers one by one.
- After entering all ten integers, it will display them in ascending order.
- You will then be asked whether you want to input another set of numbers. Enter
Y to continue or N to terminate the program.
-
Submission:
- As per the activity instructions, ensure that you handwrite the source code using CPE plates.
- Take screenshots of the program's execution and compile them into a PDF.
- Submit both the handwritten code and the PDF screenshots to Google Classroom before the due date.
Important Notes
-
Input Validation: The program includes basic input validation to ensure that only integers are accepted. If a user enters a non-integer value, the program will prompt them to re-enter the value.
-
Loop Usage: The entire program uses while loops exclusively, adhering to the activity's requirement to use only while loops for control structures.
-
Resource Management: The Scanner is properly closed at the end of the program to prevent resource leaks.
Feel free to reach out if you have any questions or need further assistance with understanding the code!