0% found this document useful (0 votes)
51 views16 pages

PHYSICS PROJECT-Class 12 ISC

The class defines an array with integer data members and methods. The arraydel method deletes duplicate elements in an array by shifting elements and reducing the size, keeping a single copy of each unique element. The common method returns a merged array containing only the even elements from the original array and an object. The main method creates objects, calls the input and print methods.

Uploaded by

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

PHYSICS PROJECT-Class 12 ISC

The class defines an array with integer data members and methods. The arraydel method deletes duplicate elements in an array by shifting elements and reducing the size, keeping a single copy of each unique element. The common method returns a merged array containing only the even elements from the original array and an object. The main method creates objects, calls the input and print methods.

Uploaded by

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

QUESTION 14- define a class

unique with following data


members:
int arr[10] and following methods: void
input( ): to input 10 integers in arr.
Int check(int n): returns 1 if n is a unique no. Else it returns 0.
A unique no. Is a no. Whose all the digits are different (eg.
1273 is unique but 121 is not unique. ) void print( ) : to print
the original array and then print all the unique numbers in
the array with proper messages.
Also write main( ).

PROGRAM
import java.Io.*;

class unique

{ //class is started

int a[] = new int[10]; //an integer of size 10 is created

void input() throws ioexception { //function to input all ten elements in an array
bufferedreader in = new bufferedreader(new inputstreamreader(system.In));
system.Out.Println("enter all the 10 elements of the array");

for (int i = 0; i < 10; i++) { a[i] =


integer.Parseint( in .Readline());
}
} //function ended int check(int n) { //function to
check if n is unique or not int c = 0;

for (int i = 0; i <= 9; i++) { //loop to calculate frequency of all digits from 0 to 9
int m = n; //value of n is assigned to a new variable m in each iteration c=
0; // variable to calculate the frequency of digit i

61
do { //nested loop to check whether i is a digit if m or not
int r = m % 10;

if (i = = r) {
c++; //increments the value of c if i is a digit of m
}

m = m / 10; // m's value is changed in each iteration of nested loop


} //frequency if digit i is calculated
while (m! = 0);

if (c >= 2) { //if frequency of any number is greater than 1 then its not a unique
number and loop can be stopped break;
}
}

if (c >= 2) {
return 0;
} else { //if at each iteration c's value remained 0 or 1 then n is a unique number
return 1;
}
} //function ends void print() { //function
to print the details for (int i = 0; i < 10; i++)
{ //prints the array system.Out.Print(a[i]
+ " ");
}

system.Out.Println(); //counter moves to the next line line


system.Out.Println("unique numbers are: ");

for (int i = 0; i < 10; i++) { //loop to check which elements of the array are unique
int y = check(a[i]); //check function returns either 1 or 0 here
if (y = = 1) {
system.Out.Println(a[i]);

62
}
}
} //function ends void main() throws
ioexception { //main function unique ob = new
unique(); //object is created ob.Input();
ob.Print(); } //function ends
} //class end

ALGORITHM
start step 1- create an array of
size 10.
Step 2- input all 10 elements into the array.
Step 3- print the array.
Step 4- initialize a variable c.
Step 5- initialize a variable i as 0.
Step 6- c is assigned as 0.
Step 7- the number which has to be checked, n, if its unique or not is assigned to a
new variable m.
Step 8- last digit of m is extracted and checked if its equals to i or not. If it is equal to
i, c is incremented. Step 9- m is divided by 10 step 10- repeat steps 8 and 9 as long as
m is unequal to 0.
Step 11- once m becomes 0, it is checked if c is greater than 1 or not.
Step 12- if c is 0 or 1 then is value is incremented by 1 and steps from 6 to 11 are
repeated as long as it is less than or equals to 9 but if c is greater than 2, move to the
next step.
Step 12- cs value is checked. If c is greater than 1, 0 is returned else 1 is returned.
Step 13- initialize a variable i as 0.
Step 14- repeat the steps from 4 to 12 with [i]th element of the array acting as m.
And if 1 is returned, [i]th value of the array is printed.
Step 15- increment is value by 1 and repeat step 14 as long as it is less than 10.
Stop
OUTPUT
enter all the 10 elements of the array
656

63
222
12
69
665
999
158
328
125
112
656 222 12 69 665 999 158 328 125 112
unique numbers are:
12
69
158
328
125

64
QUESTION 15 - define a class array with following
members: int a[ ] int size and following functions: array(int n) :
to initialize size=n and create array.
Void input( ): to input elements in array.
Array merge(array a1, array a2): to return merge array a1 and a2.
Void print( ): to print the array.
Write main( ) also.

PROGRAM
import java.Io.*;
class array { //class begin
int a[];
int size; //data members initialized
array(int n)
{ //parametrized constructor
size = n; a = new int[size];
}
void input() throws ioexception { //function to input values into the array
bufferedreader in = new bufferedreader(new inputstreamreader(system.In));
system.Out.Println(); system.
Out.Println("enter the elements");
for (int i = 0; i < size; i++) {
a[i] = integer.Parseint( in .Readline());
}
system.Out.Println();
} //function over
array merge(array a1, array a2)
{ //function to merge two arrays
array ob = new array(a1.Size + a2.Size);
//an object is created
int x = 0;
for (int i = 0; i < a1.Size; i++)

65
{ //values of a1. Arr are assigned into the array of object ob
ob.A[x] = a1.A[i];
x++;
}
for (int i = 0; i < a2.Size; i++) { //values of a2. Arr are assigned into the array of
object ob
ob.A[x] = a2.A[i];
x++;
}
ob.Size = x; //x is asigned as ob's size
return ob; //ob is returned
} //function over void print() {
//function to print the array
system.Out.Println(); for (int i = 0; i <
size; i++) { system.Out.Print(a[i] + "
");
}
} //function over
void main() throws ioexception
{ //main function
array ob1 = new array(5); //first object
array ob2 = new array(10); //second object
array ob3 = new array(0); //third object
ob1.Input(); //values are inputted into the array of first object
system.Out.Println("first array");
ob1.Print(); //array of ob1 is printed
ob2.Input(); //values are inputted into the array of second object
system.Out.Println("second array"); ob2.Print(); //array of ob2 is printed
ob3 = ob3.Merge(ob1, ob2); //arrays of ob1 and ob2 are merged and stored in third
object
system.Out.Println(); system.Out.Println();
system.Out.Print("merged array");
ob3.Print(); //merged array is printed
} //function over} //class over

66
ALGORITHM
start step 1- create three objects ob1 with argument 5, ob2 with argument 10 and ob3
with argument 0.
Step 2- input elements into the array of object ob1 and then print it.
Step 3- input elements into the array of object ob2 and then print it.
Step 4-initialize 0 as value of a new variable x.
Step 5- create a loop from 0 to 4.
Step 6- assign [i]th element of the array of object ob1 as value of [x]th element of
the array of object ob3.
Step 7- x is incremented by 1.
Step 8- create a loop from 0 to 9.
Step 9- assign [i]th element of the array of object ob2 as value of [x]th element of
the array of object ob3.
Step 10- x is incremented by 1.
Step 11- print the array of object ob3.
Stop

OUTPUT
enter the elements
10
12
13
14
15
first array
10 12 13 14 15
enter the elements
1
2
3
4
5
6

67
7
8
9 0 second array 1 2 3 4 5 6 7 8 9 0 merged array
10 12 13 14 15 1 2 3 4 5 6 7 8 9 0

68
QUESTION 16- define a class arrayop with following data members: int a[ ] int
size and following methods: arrayop(int n) : to initialize size=n and to create
array.
Arrayop arraydel(arrayop a1) : to delete all duplicate elements of a1 only single
copy should be there of each element.
Void input( ) : to input elements in the array.
Arrayop common(arrayop obj) : to return the merged array of obj and current array
object only of even elements.
Void print( ) : to print the array.
Write main( ) also.

PROGRAM
import java.Io.*; class
arrayop { //class begin
int a[]; int size; //data members
initialized arrayop(int n) {
//constructor size = n; a=
new int[n]; //array initialized
}
void input() throws ioexception

{ //function to input the size of the array bufferedreader in = new


bufferedreader(new inputstreamreader(system.In));
system.Out.Println("enter the elements");
for (int i = 0; i < size; i++) {
a[i] = integer.Parseint( in .Readline());
}
}
arrayop arraydel(arrayop a1) { //function to delete the duplicate elements
int max = a1.A[0]; //to store the biggest value in the array
int min = a1.A[0]; //to store the smallest value in the array
for (int i = 0; i < a1.Size; i++) { //biggest and smallest elements are searched out
if (a1.A[i] > max) {

69
max = a1.A[i]; }
else if (a1.A[i]<min) {
min = a1.A[i];
}
}
for (int m = 0; m < a1.Size; m++) { //loop from min to max
for (int i = min; i <= max; i++) {
int f = 0; //to calculate the frequency of number i
for (int j = 0; j < a1.Size; j++) {
if (i == a1.A[j]) { f++;
}
if (f > 1) { //just when frequency gets more than 1
f = 0;
for (int k = j; k < a1.Size - 1; k++) {
a1.A[k] = a1.A[k + 1]; //all values are shifted to 1 index behind
}
a1.Size = a1.Size - 1; //size of the aray is reduced by 1
}
}
}
}
return a1; } //function over
arrayop common(arrayop obj) { //function to combine the even elements of obj and
current object
arrayop obj1 = new arrayop(size + obj.Size); int x = 0;
for (int i = 0; i < size; i++) {
if (a[i] % 2 == 0) {
obj1.A[x] = a[i]; x++;
}
}
for (int i = 0; i < obj.Size; i++) {
if (obj.A[i] % 2 == 0) {
obj1.A[x] = obj.A[i]; x++;
}

70
}
obj1.Size = x;
return obj1;
} void print() { //function to print the array
for (int i = 0; i < size; i++) {
system.Out.Print(a[i] + " ");
}
}
void main() throws ioexception
{ //main function
arrayop ob1 = new arrayop(5);
arrayop ob2 = new arrayop(5);
arrayop ob3 = new arrayop(6);
arrayop ob4 = new arrayop(0);
ob1.Input(); ob3.Input(); //array of objects, ob1 and ob3, are inputted
ob4 = ob1.Common(ob3); //two arrays, ob1 and ob3, are combined and stored in
object ob4
ob4.Print(); //object ob4 is printed
system.Out.Println();
ob2.Input(); //array of object ob2 is inputted
ob2 = arraydel(ob2); //duplicate elements are deleted
ob2.Print(); //object ob2 is printed
} //function ends
} //class over
ALGORITHM
start step 1- create two arrays of size 5 and 6
simultaneously.
Step 2- elements are inputted into both the arrays.
Step 3- third array is created. Its arrays size is sum of sizes of first two arrays.
Step 4- initialize a variable x and assign 0 as its value.
Step 5- initialize a variable i as 0.
Step 6- if [i]th element of the first array is even, it is assigned as the value of [x]th
element of the third array. X is incremented by 1.
Step 7- increment is value by 1 and repeat step 6 as long as it is less than 5.

71
Step 8- initialize a variable i as 0.
Step 9- if [i]th element of the second array is even, it is assigned as the value of [x]th
element of the third array. X is incremented by 1.
Step 10- increment is value by 1 and repeat step 9 as long as it is less than 6.
Step 11- print third array.
Step 10- create fourth array of size 5.
Step 11- calculate the biggest and the smallest element of the array and store them
in max and min respectively.
Step 12- initialize a variable i as min.
Step 13- calculate the frequency of number i. Just when frequency gets more than 1,
all elements are shifted 1 index behind and arrays size is reduced by 1 and again
frequency becomes 0.
Step 14- increment is value by 1 and repeat step 13 as long as it less than 5.
Step 15- fourth array is printed. Stop

OUTPUT
enter the elements
1
2
3
4 5 enter the
elements
6
7
8
9
10
11
2 4 6 8 10

72
QUESTION 17- define a class pattern with following data members and
functions:
int size, int arr[ ][ ] void input( ): to input size(>=3)
and to create array.
Void print( ): to generate following matrix.
If n is odd if n is even if n=5 o/p if n=4 o/p
4 2 1 3 5 1234
9 7 6 8 10
14 12 11 13 15 4123
19 17 16 18 20
24 22 21 23 25 3412

PROGRAM
import java.Io.*;

class pattern { //class started int size; //data


member for the size of the array int arr[][];
//data member for the array pattern() {
//constructor size = 0;
}

void input() throws ioexception { //function to input the value of size


bufferedreader in = new bufferedreader(new inputstreamreader(system.In));
system.Out.Println("enter any number greater than 3");
size = integer.Parseint( in .Readline());
arr = new int[size][size]; //creates the array of size 'size'
} //function ends
void print() { //function to assign values in the matrix
if (size % 2! = 0) { //if the size of the array is an odd number
int y = 1; //first element in the matrix
int x = size / 2; //middle point array
for (int i = 0; i < size; i++) { //loop to assign elements in matrix
arr[i][x] = y;

73
y++; //each element of the array increments by 1
for (int a = 1; a <= x; a++) { //nested loop to move on either side of of middle of
row(i)
arr[i][x - a] = y; //on the left side of the middle
y++;
arr[i][x + a] = y; //on the right side of the middle
y++;
}
} //values assigned
for (int i = 0; i < size; i++) { //loop for each row
for (int j = 0; j < size; j++) { //nested loop for each column
system.Out.Print(arr[i][j] + "\t"); //printing elements of a column in a row
}

system.Out.Println(); // shift the counter to next row


}
} else if (size % 2 = = 0) { //if the size of the array is an even number

for (int i = 0; i < size; i++) { //loop for each row

int a = 1; //first element to be assigned in a row

for (int j = i; j < size; j++) { //nested loop to move the control to next column
arr[i][j] = a; a++;

if ((i! = 0) && (j == (size - 1))) { //if the value isn't assign in column 0 of any
row
for (int k = 0; k < i; k++) { //controls starts assigning values from column 0
once it has already assigned value to column as per in the nested loop
arr[i][k] = a; a++;
}
}
}
}

74
for (int i = 0; i < size; i++) { //printing the matrix
for (int j = 0; j < size; j++) {
system.Out.Print(arr[i][j]);
}

system.Out.Println(); //shifting the control to next row


system.Out.Println();
}
}
} //function ends void main() throws ioexception {
//main function starts input(); print();
} // function ends} // class ends

ALGORITHM
start step 1- input a number as
size step 2- create a matrix of that
size.
Step 3- if value of size is an odd then then initialize 1 as value of a new variable y.
Step 4- find the middle of the size as x.
Step 5- start assigning values from middle of each row.
Step 6- but if size is even values are assigned from the [i][i]th position where i varies
from 0 to the size. Stop
OUTPUT
enter any number greater than 3
5
4 2 1 3 5
9 7 6 8 10
14 12 11 13 15
19 17 16 18 20
24 22 21 23 25
enter any number greater than 3
6

75
123456
612345
561234
456123
345612
234561

76

You might also like