0% found this document useful (0 votes)
7 views9 pages

24 25

The document describes two Java classes, InsSort and StringOp, which implement sorting and string manipulation functionalities, respectively. InsSort sorts an array of integers in descending order using the insertion sort algorithm and calculates the average of odd numbers, while StringOp removes characters from an original string based on a mask string. Both classes include methods for user input, processing, and displaying results.

Uploaded by

idku.tmkcbkl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

24 25

The document describes two Java classes, InsSort and StringOp, which implement sorting and string manipulation functionalities, respectively. InsSort sorts an array of integers in descending order using the insertion sort algorithm and calculates the average of odd numbers, while StringOp removes characters from an original string based on a mask string. Both classes include methods for user input, processing, and displaying results.

Uploaded by

idku.tmkcbkl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Question 24

A class IntSort contains an array of integers which sorts the elements in a particular order
Some of the members of the class are given below
Class name : InsSort
Data members/Instance variables:
arr[] : stores the array elements
size : stores the number of elements in the array
Methods/Member functions:
InsSort(int s) : constructor to initialize size = s
void getArray() : accepts the array elements
void insertionSort() : sorts the elements of the array in descending order Insertion Sort
technique
double find() : calculates and returns the average of all the add numbers in the array
void display() : displays the elements of the array in a sorted order along with the average of
all the odd numbers in the array by invoking the function find() with
Program
import java.util.Scanner;

public class InsSort {


private int[] arr; // stores the array elements
private int size; // stores the number of elements in the array

// Constructor to initialize the size and create the array


public InsSort(int s) {
size = s;
arr = new int[size]; // create the array with given size
}

// Function to accept the array elements from the user


public void getArray() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
}

// Function to perform Insertion Sort in descending order


public void insertionSort() {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1], that are smaller than key, to one position ahead
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Function to calculate and return the average of odd numbers in the array
public double find() {
int sum = 0, count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 != 0) { // Odd number check
sum += arr[i];
count++;
}
}
// If no odd numbers are found, return 0 to avoid division by zero
return (count > 0) ? (double) sum / count : 0;
}

// Function to display the sorted array and the average of odd numbers
public void display() {
// Display the sorted array
System.out.println("Sorted Array (Descending Order):");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();

// Display the average of odd numbers


double avg = find();
System.out.println("Average of Odd Numbers: " + avg);
}

// Main method to test the functionality


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Accept size of the array from the user


System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();

// Create an object of InsSort class


InsSort insSort = new InsSort(n);
// Get the array elements from the user
insSort.getArray();

// Sort the array in descending order


insSort.insertionSort();

// Display the sorted array and average of odd numbers


insSort.display();
}
}

Algorithm

Step 1: Start

Step 2: Create the InsSort class

Step 3: Define the Constructor (InsSort(int s))

Step 4: Define the getArray() Method

Step 5: Define the insertionSort() Method

Step 6: Define the find() Method

Step 7: Define the display() Method

Step 8: Define the main() Method

Step 9: End
Variable description

Variable
Type Description
Name
arr[] int[] Array that stores the integer elements input by the user.
size int Stores the number of elements in the array.
key int
Temporary variable used in the Insertion Sort algorithm to store the
current element being inserted.
j int Index used to compare and shift elements during the Insertion Sort.
sum int
Stores the sum of the odd numbers in the array (used in the find()
method).
count int
Stores the count of the odd numbers in the array (used in the find()
method).
avg double
Stores the average of the odd numbers (calculated using the find()
method).
n int
Temporary variable used in the main() method to store the size of the
array.

Output

Question 25
A class StringOp is defined as follows to perform above operation.
Some of the members of the class are given below:
Class name:StringOpData
members/instance variables:
str: :to store the original string
msk:to store the mask string
nstr:to store the resultant string
Methods/Member functions:
StringOp() : default constructor to initialize the data member with legal initial value
void accept():to accept the original string str and the mask string msk in lower case
void form():to form the new string nstr after removal of characters present in mask from the
original string
void display():to display the original string and the newly formed string nstr
Specify the class StringOp giving details of the constructor(), void accept(), void form) and
void display(). Define a main() function to create an object and call all the functions
accordingly to enable the task.
Program
import java.util.Scanner;

public class StringOp {


// Instance variables
private String str; // to store the original string
private String msk; // to store the mask string
private String nstr; // to store the resultant string after removal

// Default constructor
public StringOp() {
str = ""; // Initialize the original string as empty
msk = ""; // Initialize the mask string as empty
nstr = ""; // Initialize the resultant string as empty
}

// Method to accept the original string and the mask string (both in lowercase)
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the original string: ");
str = scanner.nextLine().toLowerCase(); // Convert to lowercase
System.out.print("Enter the mask string: ");
msk = scanner.nextLine().toLowerCase(); // Convert to lowercase
}

// Method to form the new string after removal of characters present in the mask from
the original string
public void form() {
nstr = ""; // Initialize nstr to empty before forming the new string
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (msk.indexOf(ch) == -1) { // Check if character ch is not in the mask string
nstr += ch; // Append character to the resultant string
}
}
}

// Method to display the original string and the resultant string


public void display() {
System.out.println("Original String: " + str);
System.out.println("New String after removal: " + nstr);
}

// Main method to test the functionality


public static void main(String[] args) {
// Create an object of the StringOp class
StringOp stringOp = new StringOp();

// Accept input from the user


stringOp.accept();

// Form the new string by removing characters from the mask


stringOp.form();

// Display the original string and the new string


stringOp.display();
}
}
Algorithm

Step 1: Start the Program

Step 2: Class Declaration

Step 3: Declare Instance Variables

Step 4: Define the Constructor (StringOp())

Step 5: Define the accept() Method

Step 6: Define the form() Method

Step 7: Define the display() Method

Step 8: Define the main() Method

Step 9: End the Program

Variable Description
Variable
Type Description
Name
str String Stores the original string input by the user.
msk String
Stores the mask string input by the user. This string contains
characters to be removed from str.
nstr String
Stores the resultant string after removing all characters from str that
are present in msk.
ch char
Temporary variable used inside the form() method to store each
character of str during the iteration.
scanner Scanner
Used to take user input for the original string (str) and the mask
string (msk).
Output

You might also like