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

Ques 1

Uploaded by

Mehul Sinha
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 views3 pages

Ques 1

Uploaded by

Mehul Sinha
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/ 3

1. A class Collection contains an array of 100 integers.

Using the following class description, create an array


with common elements from two integer arrays. Some of the members of the class are given below:
Class name : Collection
Data members :
ar[] : integer array of 100 elements
len : length of the array
Member methods :
Collection() : default constructor
Collection(int ) : parameterized constructor to assign the length of
the array
void input() : reads array elements
Collection common(Collection): return the Collection containing the common
elements of current Collection and the collection
object passed as parameter.
void display() : displays the array Collection elements
Write the main() method to generate the necessary output.

Algorithm:

1. Initialize Class ‘Collection’:


a. Declare ‘int ar[]’ with size 100.
b. Declare ‘int len’.
2. Define Default Constructor ‘Collection()’:
a. Set ‘len’ to 100.
3. Define Parameterized Constructor ‘Collection(int length)’:
a. Set ‘len’ to ‘length’.
4. Define Method ‘void input()’:
a. Create ‘Scanner sc’.
b. Print "Enter array elements, one by one:"
c. For ‘l’ from 0 to ‘len-1’:
d. Read ‘sc.nextInt()’ into ‘ar[l]’.
e. Close ‘sc’.
5. Define Method ‘Collection common(Collection obj)’:
a. Create ‘Collection comm’.
b. Initialize ‘int k’ to 0.
c. For ‘i’ from 0 to ‘len-1’:
d. For ‘j’ from 0 to ‘obj.ar.length-1’:
e. If ‘ar[i]’ equals ‘obj.ar[j]’:
f. Set ‘comm.ar[k]’ to ‘ar[i]’.
g. Increment ‘k’.
h. Return ‘comm’.
6. Define Method ‘void display()’:
a. For ‘m’ from 0 to ‘len-1’:
b. Print ‘ar[m]’.
7. Define ‘main’ Method:
a. Create ‘Scanner sc’.
b. Print "Enter number of elements in array 1:"
c. Read ‘sc.nextInt()’ into ‘int len1’.
d. Create ‘Collection arr1’ with ‘len1’.
e. Call ‘arr1.input()’.
f. Print "Enter number of elements in array 2:"
g. Read ‘sc.nextInt()’ into ‘int len2’.
h. Create ‘Collection arr2’ with ‘len2’.
i. Call ‘arr2.input()’.
j. Create ‘Collection common’.
k. Set ‘common’ to ‘arr1.common(arr2)’.
l. Print "Array 1:"
m. Call ‘arr1.display()’.
n. Print "Array 2:"
o. Call ‘arr2.display()’.
p. Print "Common Elements:"
q. Call ‘common.display()’.
r. Close ‘sc’.

Source Code:

import java.util.Scanner;
class Collection {
int ar[] = new int[100]; // Array to store elements
int len = 0; // Length of the array
Collection() {
len = 100; // Default constructor sets length to 100
}
Collection(int length) {
len = length; // Parameterized constructor sets length to given value
}
void input() {
Scanner sc = new Scanner(System.in); // Create Scanner object for input
System.out.println("Enter array elements, one by one:"); // Prompt user for input
for (int l = 0; l < len; l++)
ar[l] = sc.nextInt(); // Read array elements
sc.close(); // Close Scanner
}
Collection common(Collection obj) {
Collection comm = new Collection(); // Create new Collection for common elements
int k = 0; // Index for common elements array
for (int i = 0; i < len; i++) // Loop through current array
for (int j = 0; j < obj.ar.length; j++) // Loop through passed array
if (ar[i] == obj.ar[j]) {
comm.ar[k] = ar[i]; // Store common element
k++; // Increment index
}
return comm; // Return common elements Collection
}
void display() {
for (int m = 0; m < len; m++)
System.out.println(ar[m]); // Print array elements
}
public static void main(String[] args) {
System.out.println("Enter number of elements in array 1:");
Scanner sc = new Scanner(System.in); // Create Scanner for input
Collection arr1 = new Collection(sc.nextInt()); // Create first Collection
arr1.input(); // Input elements for first Collection
System.out.println("Enter number of elements in array 2:");
Collection arr2 = new Collection(sc.nextInt()); // Create second Collection
arr2.input(); // Input elements for second Collection
Collection common = new Collection(); // Create Collection for common elements
common = arr1.common(arr2); // Find common elements
System.out.println("Array 1:");
arr1.display(); // Display elements of first array
System.out.println("Array 2:");
arr2.display(); // Display elements of second array
System.out.println("Common Elements:");
common.display(); // Display common elements
}
}

Variable Description Table (VDT):

Variable Data
Description
Name Type
ar int[] Stores the elements of the array
len int Stores the length of the array
sc Scanner Reads input from the user
l int Loop counter for reading elements into ar
obj Collection The Collection object passed to the common method
comm Collection Stores the common elements
k int Index for storing common elements in comm.ar
i int Loop counter for iterating over this.ar
j int Loop counter for iterating over obj.ar
m int Loop counter for printing elements of ar
arr1 Collection First Collection object
arr2 Collection Second Collection object
common Collection Stores the common elements between arr1 and arr2
len1 int Stores the length of the first array
len2 int Stores the length of the second array

Output Screen:

You might also like