Computer Applications
Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.
Java
Java Arrays
ICSE 2023
70 Likes
Answer
import java.util.Scanner;
public class KboatCharBubbleSort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
char ch[] = new char[10];
System.out.println("Enter 10 characters:");
for (int i = 0; i < ch.length; i++) {
ch[i] = in.nextLine().charAt(0);
}
System.out.println("Original Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
//Bubble Sort
for (int i = 0; i < ch.length - 1; i++) {
for (int j = 0; j < ch.length - 1 - i; j++) {
if (ch[j] > (ch[j + 1])) {
char t = ch[j];
ch[j] = ch[j + 1];
ch[j + 1] = t;
}
}
}
System.out.println("\nSorted Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
}
}
Variable Description Table
Program Explanation
Output

Answered By
30 Likes
Related Questions
Write the value of n after execution:
char ch ='d'; int n = ch + 5;
Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated(Declare the variables using appropriate data types)
Member methods:
void accept() — Accept name, age and marks using methods of Scanner class.
void allocation() — Allocate the stream as per following criteria:mks stream >= 300 Science and Computer >= 200 and < 300 Commerce and Computer >= 75 and < 200 Arts and Animation < 75 Try Again void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
Define a class to overload the function print as follows:
void print() - to print the following format
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
void print(int n) - To check whether the number is a lead number. A lead number is the one whose sum of even digits are equal to sum of odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1