Computer Applications
Define a class to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
Answer
import java.util.Scanner;
public class KboatBubbleSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 15;
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}Output
Related Questions
Define a class to accept a number and check whether the number is Neon or not. A number is said to be Neon if sum of the digits of the square of the number is equal to the number itself.
E.g.
Input: 9Output:
9 * 9 = 81, 8 + 1 = 9
9 is Neon number.Define a class Student described as below
Data members/instance variables
name, age, m1, m2, m3 (marks in 3 subjects), maximum, averageMember methods
(i) Student (…) : A parameterised constructor to initialise the data members.(ii) compute() : To compute the average and the maximum out of three marks.
(iii) display() : To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.
Define a class to overload the method display as follows:
void display(): To print the following format using nested loop
5 5 5 5 5 4 5 5 5 5 3 4 5 5 5 2 3 4 5 5 1 2 3 4 5void display(int n): To check and display if the given number is a Perfect number or not. A number is said to be perfect, if sum of the factors of the number excluding itself is equal to the original number.
E.g.
6 = 1 + 2 + 3, where 1, 2 and 3 are factors of 6 excluding itself.Define a class to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word.
E.g.
Input : Enter the sentence:
Hello, this is wow world
Enter the word:
wowOutput:
Searched word occurs 1 times.