Computer Science Project Work 1
Computer Science Project Work 1
ISC COMPUTER
SCIENCE PROJECT
SUBMITTED BY:-
ANSH MISHRA
XII-A
ACKNOWLEDGEMENT
This is to inform that I Ansh Mishra of class XII th A has completed
my computer project . Project work is the best way of learning by doing . I
learned a lot by doing this project .This project contains 30 programs and
each program contains an algorithm , Program code , Variable description
table and an output screen .
_________________ ________________
Teacher ’s Signature Student ’s Signature
_________________ _________________
Examiner ’s Signature Principal ’s Signature
Index:
3. Arrays.
● Program 1
● Program 2
● Program 3
● Program4
4. Recursion.
● Program 1
● Program 2
● Program 3
● Program4
5. Inheritance
● Program 1
● Program 2
● Program 3
6. Data
Structure.
● Program 1
● Program 2
● Program 3
7. Date and
Time
related
programs.
● Program 1
● Program 2
8. Object
Passing.
● Program 1
● Program 2
● Program 3
9. Bibliography
ARRAYS:-
Program 1:
Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must
be greater than 3 and less than 10. Allow the user to accept three different
characters from the keyboard and fill the array according to the instruction
given below: (i) Fill the four corners of the square matrix by character 1. (ii) Fill
the boundary elements of the matrix (except the four corners) by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3. Test your
program with the following data and some random data: Example 1:
INPUT:
N=4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #
OUTPUT:
@??@
?##?
?##?
@??@
ALGORITHM:
STEP 1: START.
STEP 2: ASK FOR USER INPUT.
STEP 3: CHECK FOR THE CORNER ELEMENTS IN THE FIRST IF
STATEMENT AND FILL THE 1st CHARACTER.
STEP 4: CHECK FOR THE NON-BOUNDARY ELEMENTS AND
FILL THE 3rd CHARACTER.
STEP 5: FILL THE 2nd CHARACTER IN THE ELSE PART.
STEP 6: PRINT THE ARRAY IN MATRIX FORM.
STEP 7: END.
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT
1137 607
2106 1094
0245 165
import java.util.*;
public class DArray
{ //class opened
public static void main(String args[])
{ //main opened
int ar[][],M,N,j=0,n=0,k;
double s=0d;
Scanner sc =new Scanner(System.in);
System.out.println("Enter Number of Rows");
M=sc.nextInt();
System.out.println("Enter Number of
Columns");
N=sc.nextInt();
ar=new int[M][N];
k=N-1;
while(j<M)
{
System.out.print("Enter elements
of"+(j+1)+"th row (0-7 only): ");
for(int i=0;i<N;i++)
{
ar[j][i]=sc.nextInt();
}
j++;
}
while(n<M)
{
s=0;
k=N-1;
for(int i=0;i<N;i++) //calculates the
decimal equivalent
{
s=s+(ar[n][i]*Math.pow(8,k));
k--;
}
System.out.println("Decimal Equivalent
"+n+"th row: "+s);
n++;
}
} //main closed
} //class closed
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NO. NAME
1. ar int Array to store integers as per user
inputs
2. M int Stores number of rows of the array
3. N int Stores number of columns of the
array
4. k int Stores the power of 8 according to
the number of columns
5. s double Stores the decimal equivalent of the
given row
Program 3:
Write a program to transpose a matrix. Perform the following tasks:-
(i) Take input in a double dimensional array. (ii)Rotate the array by 90
degrees clockwise.
(iii) Print the updated array in matrix form. Example:
INPUT: 1 2 3
456
789
OUTPUT:
741
852
963
import java.util.*;
public class DDArray90
{
public static void main(String args[])
{
int n=3;
int arr[][]=new int [n][n]; int
arr1[][]=new int [n][n]; Scanner
sc=new Scanner(System.in);
System.out.println("Enter array elements");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
arr[i][j]=sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
for(int j=0;j<n;j++)
{
for(int i=0;i<n;i++)
{
arr1[i][n-1-j]=arr[j][i];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(arr1[i][j]+" ");
System.out.println();
}
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. n int Used to store
order of matrix.
2. arr int Used to take the
original array as
user input.
3. arr1 int Stores the
updated array.
4. i int Looping variable.
5. j int Looping variable.
Program 4:
Write a program to declare a matrix A[][] of order (M*N) such that the
value of both M & N must be greater than 2 and less than 8. Allow
the user to input integers into the matrix and perform the following
task:
a) sort the matrix elements in descending order
b) calculate sum of boundary elements before and after sorting
c) display the original matrix and sorted matrix and sum also.
ALGORITHM:
STEP 1: START.
STEP 2: TAKE INPUT IN THE ARRAY..
STEP 3: TRANSFER ARRAY ELEMENTS INTO A
SINGLE DIMENSIONAL ARRAY..
STEP 4: SORT THE SINGLE DIMENSIONAL ARRAY USING
BUBBLE SORT TECHNIQUE..
STEP 5: TRANSFER THE SORTED ARRAY TO THE
DOUBLE DIMENSIONAL ARRAY .
STEP 6: DISPLAY BOTH ORIGINAL AND ARRANGED ARRAY.
STEP 7: END.
import java.util.*;
public class DDSort
{
public static void main(String args[])
{
int A[][],M,N,arr[],k=0,sum=0; Scanner
sc=new Scanner(System.in);
System.out.println("Enter number of rows
and number of columns respectively");
M=sc.nextInt();
N=sc.nextInt();
A=new int[M][N];
arr=new int[M*N];
if(M>2 && M<8 && N<8 && N>2)
{
System.out.println("Enter array
elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
A[i][j]=sc.nextInt();
if(k<(M*N))
{
arr[k]=A[i][j];
k++;
}
}
}
for(int i=0;i<(M*N);i++)
{
for(int j=0;j<(M*N)-i-1;j++)
{
if(arr[j]<arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if(((i==0)||(i==M-1)) ||
((j==0)||(j==N-1)))
{
sum=sum+A[i][j];
}
}
}
System.out.println("ORIGINAL ARRAY");
for(int i=0;i<M;i++) {
for(int j=0;j<N;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}
k=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
A[i][j]=arr[k++];
}
}
System.out.println("SORTED ARRAY");
for(int i=0;i<M;i++) {
for(int j=0;j<N;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}
System.out.println("Sum of Boundary
Elements: "+sum);
}
}
}
Program 1:
An Evil number is a positive whole number which has an even number of
1’s in its binary equivalent. Example: Binary equivalent of 9 is 1001, which
contains an even number of 1’s. Thus, 9 is an Evil Number. A few Evil
numbers are 3, 5, 6, 9..... Design a program to accept a positive whole
number ‘N’ where N>2 and N<100. Find the binary equivalent of the
number and count the number of 1s in it and display whether it is an
Evil number or not with an appropriate message.
ALGORITHM:
STEP 1: START.
STEP 2: ASK FOR USER INPUT.
STEP 3: CALCULATE THE BINARY EQUIVALENT OF THE NUMBER.
STEP 4: COUNT THE NUMBER OF 1s IN THE BINARY EQUIVALENT.
STEP 5: IF THERE ARE EVEN NUMBER OF 1s, EVIL NUMBER.
STEP 6: ELSE NOT AN EVIL NUMBER..
STEP 7: END.
import java.util.*;
public class evil
{
int b=0;
int binary(int n) //finds the binary
equivalent of the number
{
if(n>0)
{
int d=n%2;
binary(n/2);
b=b*10+d;
}
return b;
}
public static void main(String args[])
{
int N,c,d=0,a=0;
Scanner sc=new Scanner(System.in);
evil ev=new evil();
System.out.println("Enter a number");
N=sc.nextInt();
c=ev.binary(N);
while(c>0) //counts the number of 1s in the
number
{
a=c%10;
if(a==1)
d++;
c=c/10;
}
if(d%2==0)
System.out.println("Evil Number");
else
System.out.println("Not an Evil Number");
}
}
VARIABLE DESCRIPTION TABLE
SERI VARIAB DATATYP DESCRIPTION
AL LE E
NO. NAME
1. N int Stores the number
2. c int Stores the binary equivalent of the number
3. a int Extracts digits from the number
4. d int Stores number of 1s present in the number
Program 2:
A Prime-Adam integer is a positive integer (without leading zeros) which is a
prime as well as an Adam number. Prime number: A number which has only
two factors, i.e., 1 and the number itself. Example: 2, 3, 5, 7 … etc. Adam
number: The square of a number and the square of its reverse are
reverse to each other. Example: If n=13 and reverse of ‘n’= 31, then, (13)2
= 169 (31)2 = 961 which is the reverse of 169. Thus 13, is an Adam
number. Accept two positive integers m and n, where m is less than n as
user input. Display all Prime-Adam integers that are in the range between
m and n (both inclusive) and output them along with the frequency, in the
format given below: Test your program with the following data and some
random data:
Example
INPUT:
m=5n
= 100
OUTPUT:
THE PRIME-ADAM INTEGERS
ARE: 11, 13, 31
FREQUENCY OF PRIME-ADAM INTEGERS IS:
3 import java.util.*;
public class PrimeAdam
{
int prime(int n) //checks if the number is
prime
{
int i,f=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
f++;
break;
}
}
return f;
}
int reverse(int p) //returns the reverse of
the number
{
int rev=0,r;
while(p>0)
{
r=p%10;
rev=rev*10+r;
p=p/10;
}
return rev;
}
int adam(int n) //checks if the number is
PRIME-ADAM
{
if(prime(n)==0 &&
n*n==reverse(reverse(n)*reverse(n)))
return 1;
else
return 0;
}
public static void main(String args[])
{
int m,n,c=0;
Scanner sc=new Scanner(System.in);
import java.util.*;
public class CircularPrime
{
boolean prime(int n)
{ //checks for prime number
int f=1;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
f=0;
}
}
if(f==1)
return true;
else
return false;
}
String circle(String s)
{
return(s.substring(1)+s.charAt(0));
}
public static void main(String args[])
{
int a,c=0;
Scanner sc=new Scanner(System.in);
CircularPrime cp=new CircularPrime();
System.out.println("Enter a number");
a=sc.nextInt();
int m=a;
while(cp.prime(a)==true)
{
String as=Integer.toString(a);
String d=cp.circle(as);
a=Integer.parseInt(d); c++;
}
String b=Integer.toString(m);
if(c==b.length())
System.out.println("CIRCULAR PRIME");
else
System.out.println("NOT A CIRCULAR PRIME");
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. f int Flag variable.
2. a int Stores the
number given by
the user.
3. c int Counter
variable.
4. m int Stores the copy
of the number
5. as String Stores the
number in string.
6. d String Stores the
number after its
rearrangement.
7. b String Stores the
number in string.
Program 4:
273
273*1 = 273
273*2 = 546
273*3 = 819
Concatenating result: 273546819, which contains all the digits from 1 to
9 exactly once.
Accept two positive integers m and n, where m must be less than n and
the values of both ‘m’ and ‘n’ must be greater than 99 & less than 10000
as user input. Display all the Fascinating numbers that are in range
between m and n (both inclusive) and output them along with frequency, in
the format given below:-
INPUT: m = 100
n = 500
OUTPUT: The fascinating number
are 192 219 273 327
Frequency: 4
import java.util.*;
public class Fascinating
{
int fascinate(int n)
{
int a=n*2;
int b=n*3;
int k=0;
String d="123456789";
String a1=Integer.toString(a);
String b1=Integer.toString(b);
String c1=Integer.toString(n);
String f=c1+ a1+ b1;
for(int i=0;i<d.length();i++)
{
char ch=d.charAt(i);
if(f.indexOf(ch)==-1 ||
f.indexOf(ch)!=f.indexOf(ch))
{
k=1;
break;
}
}
return k;
}
public static void main(String args[])
{
int m,n,v=0;
Scanner sc=new Scanner(System.in);
Fascinating fs=new Fascinating();
System.out.println("Enter lower limit");
m=sc.nextInt();
System.out.println("Enter upper limit");
n=sc.nextInt();
if(m<n && m>99 && m<1000 && n>99 &&
n<10000)
{
System.out.println("The Fascinating
numbers are:-");
for(int i=m;i<=n;i++)
{
if(fs.fascinate(i)==0)
{
System.out.print(i+" ");
v++;
}
}
System.out.println("FREQUENCY: "+v);
}
else
System.out.println("INVALID RANGE");
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NUMBER
1. a int To store the
product of
number with 2.
2. b int To store the
product of the
number with 3.
3. k int Flag variable.
4. f String Stores the
concatenated
result.
5. m int Stores lower
boundary.
6. n int Stores upper
boundary.
7. v int Stored frequency.
Program 5:
ALGORITHM:
STEP 1: START.
STEP 2: ASK THE USER TO ENTER A NUMBER.
STEP 3: EXTRACT THE DIGITS OF THE NUMBER ONE BY ONE.
STEP 4: CALCULATE THE SUM OF DIGITS.
STEP 5: CALCULATE THE PRODUCT OF DIGITS.
STEP 6: COMPARE THE SOME OF DIGITS WITH PRODUCT
OF DIGITS.
STEP 7: IF EQUAL, IT IS A SPY NUMBER.
STEP 8: ELSE IT IS NOT A SPY NUMBER.
STEP 9: END.
import java.util.Scanner;
public class Spy_Number
{
public static void main(String[] args)
{ Scanner sc= new Scanner(System.in);
int digit, num;
System.out.println("Enter a number");
num = sc.nextInt();
int product = 1;
int sum = 0;
//Extract digit, add to sum and multiply to
product
while(num>0){
digit = num%10;
sum += digit;
product *= digit;
num=num/10;
}
if(sum == product)
System.out.println("Spy Number");
else
System.out.println("Not a Spy Number");
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. num int Stores the
number entered
by the user.
2. digit int Extracts and
stores digits.
3. sum int Stores the sum
og digits.
4. product int Stores the
product of digits.
strings:-
Program 1:
The potential of a word is found by adding the ASCII value of the
alphabets. (ASCII values of A to Z are 65 to 90). Write a program to
accept a sentence which may be terminated by either “.”, “?” or “!” only.
The words of the sentence are separated by a single blank space and
are in the UPPER CASE. Decode the words according to their potential
and arrange them in ascending order of their potential strength .
import java.util.*;
public class Potential
{
int position(String w) //returns the
potential of the word
{
int p=0;
for(int i=0;i<w.length();i++)
{
p=p+(int)w.charAt(i);
}
return p;
}
String s=sc.nextLine();
StringTokenizer st=new
StringTokenizer(s,".,!,?");
n=st.countTokens();
num=new int[n];
arr=new String[n];
while(st.hasMoreTokens())
{
b=st.nextToken();
arr[j]=b;
num[j]=pt.position(b);
j++;
}
for(int k=0;k<n-1;k++) //number of passes
to sort the array in ascending order of their
potentials
{
for(int i=0;i<n-k-1;i++)
{
if(num[i]>num[i+1])
{
int temp=num[i];
num[i]=num[i+1];
num[i+1]=temp;
String tempo=arr[i];
arr[i]=arr[i+1];
arr[i+1]=tempo;
}
}
}
System.out.println("Sorted sentence");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}
if(f==0)
System.out.println("VALID");
else
System.out.println("INVALID");
}
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NO. NAME
1. f int Checks for the presence of all the
valid characters
2. s String Stores the string entered by the user
3. v String Stores the set of all valid characters
4. p String Extracts all the characters from the
string one by one
Program 3:
Write a program to accept a sentence which may be terminated by
either’.’ ,’?’or ‘!’only. The words are to be separated by a single blank space in
uppercase. Check for the validity of the sentence and convert the non-
palindrome words by concatenating the words by its reverse( excluding
the last character ).Display the original sentence along with the converted
sentence .
INPUT : THE BIRD IS FLYING.
OUTPUT : THEHT BIRDRIB ISI FLYINGNIYLF
import java.util.*; // importing scanner
class
public class PalindromeSentence
{ //class opened
String revStr(String s) //returns the reverse
of the word
{
String p="";
for(int i=0;i<s.length()-1;i++)
{
p=s.charAt(i)+p;
}
return p;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
PalindromeSentence Ps=new
PalindromeSentence();
String s,w,p="";
System.out.println("Enter the sentence");
s=sc.nextLine();
s=s.toUpperCase();
StringTokenizer st= new StringTokenizer(s);
while(st.hasMoreTokens())
{
w=st.nextToken();
if(w.equals(Ps.revStr(w))
p=p+w+“ ”;
else
p=p+w+Ps.revStr(w)+" ";
}
p=p.trim();
System.out.println("ORIGINAL STRING:"+s);
System.out.println("UPDATED STRING:"+p);
}
}
Program 4:
A class Rearrange has been defined to modify a word by bringing all the
vowels in the word at the beginning followed by the consonants. [10]
Example:
ORIGINAL becomes OIIARGNL
Some of the members of the class are given below:
Class name: Rearrange
Data Member/instance variable:
wrd: to store a word
newwrd: to store the rearranged
word Member functions/methods:
Rearrange(): default constructor
void readword(): to accept the word in UPPER case
vow freq_vow_con(): finds the frequency of vowels and consonants in
the word and displays them with an appropriate message
void arrange(): rearranges the word by bringing the vowels at
the beginning followed by consonants
void display(): displays the original word along with the rearranged word
Specify the class Rearrange, giving the details of the constructor(), void
readword(), void freq _vow_con(), void arrange() and void display().
Define the main() function to create an object and call the functions
accordingly to enable the task.import java.util.*;
public class Rearrange
{
String wrd,newwrd;
Rearrange()
{
wrd="";
newwrd="";
}
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the word");
wrd=sc.nextLine().toUpperCase();
}
void freq_vow_con()
{
String v3="AEIOU";
int c=0,v=0;
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(v3.indexOf(ch)!=-1)
v++;
else
c++;
}
System.out.println("Number of vowels: "+v);
System.out.println("Number of consonants:
"+c);
}
void arrange()
{
String v2="AEIOU";
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(v2.indexOf(ch)!=-1)
newwrd=newwrd+ch;
}
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(v2.indexOf(ch)==-1)
newwrd=newwrd+ch;
}
}
void display()
{
System.out.println("ORIGINAL WORD: "+wrd);
System.out.println("REARRANGED WORD:
"+newwrd);
}
public static void main(String args[])
{
Vonsonants vc=new Rearrange();
vc.readword();
vc.freq_vow_con();
vc.arrange();
vc.display();
}
}
Program 5:
A class Encrypt has been defined to replace only the vowels in a word
by the next
corresponding vowel and forms a new word. i.e. A → E, E → I, I → O,
O → U and U → A
Example: Input: COMPUTER
Output: CUMPATIR
Some of the members of the class are given below:
Class name : Encrypt
Data members/instance variables:
wrd : to store a word
len : integer to store the length of the word
newwrd : to store the encrypted word
Methods / Member functions:
Encrypt( ) : default constructor to initialize data
members with legal initial values
void acceptword( ) : to accept a word in UPPER CASE
void freqvowcon( ) : finds the frequency of the vowels
and consonants in the word stored in ‘wrd’ and displays
them with an appropriate message
void nextVowel( ) : replaces only the vowels from the word
stored in ‘wrd’ by the next corresponding vowel and
assigns it to ‘newwrd’, with the
remaining alphabets unchanged
void disp( ) : Displays the original word along with
the encrypted word
Specify the class Encrypt giving details of the constructor( ),
void acceptword( ),
void freqvowcon( ), void nextVowel( ) and void disp( ). Define a main( )
function to create an object and call the functions accordingly to
enable the task.
import java.util.*;
public class Encrypt
{
String wrd,newwrd;
int len;
Encrypt()
{
wrd="";
len=0;
newwrd="";
}
void acceptword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the word");
wrd=sc.nextLine().toUpperCase();
len=wrd.length();
}
void freqvowcon()
{
String v="AEIOU";
int a=0,b=0;
for(int i=0;i<len;i++)
{
char ch=wrd.charAt(i);
if(v.indexOf(ch)!=-1)
a++;
else
b++;
}
System.out.println("Number of vowels: "+a);
System.out.println("Number of consonants:
"+b);
}
void nextVowel()
{
int i=0;
String v="AEIOUAEIOU";
while(i<wrd.length())
{
char ch=wrd.charAt(i);
if(v.indexOf(ch)!=-1)
{
int d=v.indexOf(ch);
newwrd=newwrd+v.charAt(d+1);
}
else
newwrd=newwrd+ch;i++;
}
}
void display()
{
System.out.println("ORIGINAL WORD: "+wrd);
System.out.println("ENCRYPTED WORD:
"+newwrd);
}
public static void main(String args[])
{
Encrypt ep=new Encrypt();
ep.acceptword();
ep.freqvowcon();
ep.nextVowel();
ep.display();
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. wrd String To store word
entered by the
user.
2. len int Stores length of
the word.
3. newwrd String Stores the
updated word.
4. a int Stores number
of vowels.
5. b int Stores number
of consonants.
6. v String A spring of
vowels.
Program 6:
Write a program in java to reverse a sentence
without reversing its individual words.You can assume that there is only
single space between the words.
Example:
INPUT: I LOVE MY DOG TEDDY
OUTPUT: TEDDY DOG MY LOVE I
import java.util.*;
public class RevLoop
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
//Input sentence
System.out.print("Enter a sentence: ");
String str = in.nextLine();
//Append whitespace for the extraction of last
word str += " ";
String reversed = "";
//repeat until no word is left
while(str.indexOf(" ") != -1)
{ //Extract word
int idx = str.indexOf(" ");
String word = str.substring(0, idx);
//Concatenate in reverse order
reversed = word + " "+ reversed;
//Remove word from the sentence
str = str.substring(idx+1);
} //Output the reverse
System.out.print("Reverse: ");
System.out.println(reversed);
}
}
Program 1:
A super class VEHICLE stores the details of a Vehicle. Another class
car is derived from VEHICLE which performs certain operations. class
name: VEHICLE
Data members/Instance Variables
String regnum: to store the registration number
char permit: the national permit category [either N, S or
L] Member Functions
VEHICLE(….) parameterized constructor to initialize data members
void Print( ) to display data members
class name: CAR
Data member/ Instance Variable
long price: to store the price of the car
double Exc: to store the excise duty to be levied on the
car Member Functions
CAR(….): parameterized constructor to initialize data members of both the
classes
void calEx( ): calculates the excise duty as per the following chart:
Permit Excise Amount
N 22% of the price
S 11% of the price
L 5% of the price
void Print( ) to display all the details of both the classes with
proper message
class VEHICLE
{
String regnum;
char permit;
VEHICLE(String r, char p) //parameterized
constructor
{
regnum=r;
permit=p;
}
void Print() //displays the member methods
of the class
{
System.out.println("Registration
Number:"+regnum);
System.out.println("Permit:"+permit);
}
}
of the class
{
System.out.println("Employee
number:"+empNo);
System.out.println("Employee
name:"+empName);
System.out.println("Designation:"+empDesig);
}
}
{
super.display();
System.out.println("Basic Pay:"+basic);
System.out.println("DA:"+DA);
System.out.println("HRA:"+HRA);
System.out.println("Salary:"+Salary);
System.out.println("PF:"+PF);
System.out.println("Net Salary:"+Net);
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE NAME DATATYPE DESCRIPTIO
NO. N
1. empNo String to store the
employee
number
of the Cuboid
{
return(length*breadth*height);
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NO. NAME
1. radius double Stores radius of Cylinder
2. hgtc double Stores height of Cylinder
3. length double Stores length of Cuboid
4. breadth double Stores breadth of Cuboid
5. height double Stores height of Cuboid
RECURSION:-
Program 1:
Write a program to check whether the given number is a “Special
number” by using the concept of recursion.
Class name : Special
Data members :
int num: for storing the number.
int f : to find and to store the factorial of the number.
int sum : to store the sum of factorial of each digit.
Methods:
special ( ) : parameterized constructor.
int factorial(int a):to return the factorial of a number using the
recursive technique.
void digits( ) : to find the sum of factorial of each digit of num.
void display( ) : to print whether the number is a special number or not .
Also write the main( ) function to call other functions.
import java.util.*;
public class Special
{
int num,f,sum;
Special(int n) //parameterized constructor
{
num=n;
f=0;
sum=0;
}
int factorial(int a) //returns the factorial
of the number
{
if(a==0)
return 1;
else
return a*factorial(a-1);
}
int digits(int p) //finds the sum of
factorial of each digit of the number
{
int b=0;
while(p>0)
{
b=p%10;
sum=sum+factorial(b);
p=p/10;
}
return sum;
}
void display() //displays the conclusion
{
if(digits(num)==num)
System.out.println("SPECIAL NUMBER");
else
System.out.println("NOT A SPECIAL NUMBER");
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=sc.nextInt();
Special pc=new Special(n);
pc.display();
}
}
import java.util.Scanner;
public class Rev_Recur{
public static void main(String
args[]){Scanner in =new
Scanner(System.in);
//Input sentence
System.out.print("Enter a Sentence: ");
String sentence = in.nextLine();
//Reverse by calling the recursive method
String reversed = reverse(sentence);
//output the reversed word
System.out.print("Reverse: ");
System.out.println(reversed);
}
public static String reverse(String str)
{
int idx = str.indexOf(" ");
//Base condition - when str has only one
word
if(idx == -1)
return str;
//return after concatenating in reverse
order
return reverse(str.substring(idx+1))
+"" str.substring(0, idx);
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. sentence String Stores the
sentence
entered by the
user.
2. reversed String Stores the
reversed
sentence.
3. idx int Stores index of
a single space.
Data structures:-
Program 1:
A linear data structure enables the user to add address from rear end
and remove address from front. Define a class Diary with the following
details: Class name : Diary
Data members / instance variables:
Q[ ] : array to store the addresses
size : stores the maximum capacity of the array
start : to point the index of the front end
end : to point the index of the rear end
Member functions:
Diary (int max) : constructor to initialize the data member size=max,
start=0 and end=0 void pushadd(String n) : to add address in the
diary from the rear end if possible, otherwise display the message “
NO SPACE”
String popadd( ) : removes and returns the address from the front end
of the diary if any, else returns “?????”
void show( ) : displays all the addresses in the diary
(a) Specify the class Diary giving details of the functions void
pushadd(String) and String popadd( ). Assume that the other functions have
been defined. The main function and algorithm need NOT be written.
(b) Name the entity used in the above data structure arrangement.
import java.util.*;
public class ISC2019Q11 {
public static void main(String[] args)
{
int max;
int choice = -1;
Scanner input = new Scanner(System.in);
System.out.println("Enter the maximum
strength of the diary:");
max = input.nextInt();
Diary obj = new Diary(max);
System.out.println("Enter 0 to exit:");
System.out.println("Enter 1 to add:");
System.out.println("Enter 2 to remove:");
System.out.println("Enter 3 to display:");
do {
System.out.println("Enter your choice:");
choice = input.nextInt(); switch(choice)
{case 0:
break;
case 1:
String n;
System.out.println("Enter the address
to add:");
n = input.next();
obj.pushAdd(n);
break;
case 2:
System.out.println(obj.popAdd());break;
case 3:
obj.show();
break;
}while(choice !=0);
input.close();
}
}
class Diary {
public void show() {
for(int i = start; i<end; i++)
System.out.println(Q[i]+" ");
}
public String popAdd()
{String frontValue;
if ( end == start)
return ("QUEUE IS EMPTY");
else {
frontValue = Q[start];
for(int i = 0; i < end-1 ;i++)
Q[i]=Q[i+1];
end--;
return (frontValue);
}
}
public void pushAdd(String n)
{if (end < size) {
Q[end] = n;
end++;
}
else
System.out.println("NO SPACE:");
}
Diary (int
max){ this.size =
max;this.start = 0;
this.end = 0;
Q = new String[this.size];
}
private String[] Q;
private int size;
private int start;
private int end;
}
import java.util.*;
public class Register
{
String stud[];
int top,cap;
void push(String n) //adds the given element
in the Array
{
if(top==cap-1)
System.out.println(“OVERFLOW”);
else
{
top++;
stud[top]=n;
}
}
void pop() //removes the last element in the
array
{
if(top==-1)
{
System.out.println("$$");
return ;
}
else
{
int v=stud[top];
System.out.println("Element
popped:"+v);
top--;
}
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NO. NAME
1. stud String array to store the names of the
students
Program 1:
Write a program to accept date in the string format dd/mm/yyyy and
accept the name of the day on 1st January of the corresponding
year. Find the day for the given date.
import java.util.*;
public class date
{
public static void main(String args[])
{
String
ar[]={"MONDAY","TUESDAY","WEDNESDAY","THUSDAY","FRI
DAY","SATURDAY","SUNDAY","MONDAY","TUESDAY","WEDNES
DAY","THURSDAY","FRIDAY","SATURDAY"};
int
last[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dd1, mm1, y1,a=0,s,f,g=0;
String d,day,dd,mm,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the date");
d=sc.nextLine();
dd=d.substring(0,2);
mm=d.substring(3,5);
y=d.substring(6);
dd1=Integer.parseInt(dd);
mm1=Integer.parseInt(mm);
y1=Integer.parseInt(y);
System.out.println("Enter the day on 1st
Jan");
day=sc.nextLine();
day=day.toUpperCase();
for(int i=0;i<mm1;i++) //calculates the
number of days
{
a=a+last[i];
}
a=a+dd1;
int n=a;
while(n>=7)
{
n=n-7;
}
for(int i=0;i<7;i++) //finds the index of
the day on 1st January in the array of names of
days
{
if(day.equals(ar[i]))
{
g=i;
break;
}
}
f=g+(n-1);
System.out.println(ar[f]);
}
}
VARIABLE DESCRIPTION TABLE
SERIAL NO. VARIABLE DATATYPE DESCRIPTION
NAME
1. ar String stores names of
days
2. last int Stores no. of
days of each
month
3. d String Stores the date
entered by user
4. day String Stores the day
on 1st January
5. dd String Stores date
6. mm String Stores month
7. y String Stores year
8. a int Stores after how
many days the
given date
occurs
9. f int Stores the index
of the final day
Program 2:
Write a program to input time in hours: minutes (12 hours format)
0<hours <12,
0<=minutes <60, and to convert the time in words and print the required
details.
Example:
06:45 QUARTER TO SEVEN
12:50 TEN MINUTES TO ONE
03:30 HALF PAST THREE
04:15 QUARTER PAST FOUR
import java.util.*;
public class Time
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m,h;
String p="";
String
ar[]={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN
","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN",
"FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN
","NINETEEN","TWENTY","TWENTY ONE","TWENTY
TWO","TWENTY THREE","TWENTY FOUR","TWENTY
FIVE","TWENTY SIX","TWENTY SEVEN","TWENTY
EIGHT","TWENTY NINE"};
System.out.println("Enter hour");
h=sc.nextInt();
System.out.println("Enter minutes");
m=sc.nextInt();
if(h>0 && h<=12 && m>=0 && m<60)
{
if(m==0)
p=ar[h-1];
if(m<=30) //conditions for the
time in words if minutes are less than or equal to
30 {
if(m==15)
p="QUARTER PAST "+ar[h-1];
else if(m==30)
p="HALF PAST "+ar[h-1];
else if(m==1)
p=ar[m-1]+" MINUTE PAST "+ar[h-1];
else
p=ar[m-1]+" MINUTES PAST "+ar[h-1];
}
else //conditions for the time in words
if minutes are greater than 30
{
if(h==12) //if it's 12th hour
{
if(m==45)
p="QUARTER TO ONE";
else if(m==59)
p=ar[0]+" MINUTE TO ONE";
else
p=ar[59-m]+" MINUTES TO ONE";
}
else
{
if(m==45)
p="QUARTER TO "+ar[h];
else if(m==59)
p=ar[0]+" MINUTE TO "+ar[h];
else
p=ar[59-m]+" MINUTES TO "+ar[h];
}
}
System.out.println(p);
}
else
{
System.out.println("INVALID INPUT");
}
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NO. NAME
1. h int stores the hour
2. m int Stores the minutes
3. ar String Stores the string format for
numbers(1-60)
Object passing:-
Program 1:
A class Mixer has been defined to merge two sorted integer arrays in
ascending order. Some of the members of the class are given below:
[10] Class name: Mixer
Data members/instance variables:
int arr[ ]: to store the elements of an array
int n: to store the size of the array
Member functions:
Mixer(int nn): constructor to assign n=nn
void accept(): to accept the elements of the array in ascending
order without any duplicates
Mixer mix (Mixer A): to merge the current object array elements with
the parameterized array elements and return the resultant object void
display(): to display the elements of the array
Specify the class Mixer, giving details of the constructor(int), void
accept(), Mixer mix(Mixer) and void display(). Define the main( ) function
to create an object and call the function accordingly to enable the task
import java.util.*;
public class Mixer
{
int arr[];
int n;
Mixer(int nn)
{
n=nn;
arr=new int[n];
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array elements");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
Mixer Mix(Mixer A)
{
Mixer C =new Mixer(n+A.n);
int x,y,temp;
for(x=0;x<n;x++)
{
C.arr[x]=arr[x];
}
for(y=0;y<A.n;y++)
{
C.arr[x]=A.arr[y];
x++;
}
for(x=0;x<C.n-1;x++)
{
for(y=0;y<C.n-x-1;y++)
{
if(C.arr[y]>C.arr[y+1])
{
temp=C.arr[y];
C.arr[y]=C.arr[y+1];
C.arr[y+1]=temp;
}
}
}
return C;
}
void display()
{
System.out.println("\nARRAY ELEMENTS
ARE:-");
for(int i=0;i<n;i++)
System.out.print(arr[i]+” ”);
}
public static void main(String args[])
{
int l1,l2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter length of 1st
array");
l1=sc.nextInt();
System.out.println("Enter length of 2st
array");
l2=sc.nextInt();
Mixer A=new Mixer(l1);
Mixer B=new Mixer(l2);
A.accept();B.accept();
A.display();B.display();
Mixer R=B.Mix(A);
System.out.println("\nMIXED ARRAY
IS:-");
R.display();
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. arr int Array to store
integers.
2. n int Stores length of
the array.
3. x int Looping
variable.
4. y int Looping
variable.
Program 2:
A class Merger concatenates two positive integers that are greater than
0 and produces a newly merged integer. [10]
Example: If the first number is 23 and the second is 764, then
the concatenated number will be 23764.
Some of the members of the class are given below:
Class name: Merger
Data members/instance variables:
n1: long integer to store the first number
n2: long integer to store the second number
mergNum: long integer to store the merged number
Member functions:
Merger(): constructor to initialize the data members
void readNum(): to accept the values of the data members n1 and
n2 voidjoinNum(): to concatenate the numbers n1 and n2 and store
it in mergNum
void show(): to display the original numbers and the merged number
with appropriate messages
Specify the class Merger giving the details of the constructor, void
readNum(), void joinNum() and void show(). Define the main() function
to create an object and call the functions accordingly to enable the task.
import java.util.*;
public class Merger
{
long n1,n2,mergnum;
Merger()
{
n1=0;
n2=0;
mergnum=0;
}
void readNum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number");
n1=sc.nextLong();
System.out.println("Enter the second
number");
n2=sc.nextLong();
}
void joinNum()
{
int n11,n22;
n11=(int)n1;
n22=(int)n2;
String m=Integer.toString(n11);
String n=Integer.toString(n22);
String s=m+n;
int f=Integer.parseInt(s);
mergnum=mergnum+f;
}
void show()
{
System.out.println("First number: "+n1);
System.out.println("Second number: "+n2);
System.out.println("Merged number:
"+mergnum);
}
public static void main(String args[])
{
Merger mg=new Merger();
mg.readNum();
mg.joinNum();
mg.show();
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. n1 int To store the first
number entered
by the user.
2. n2 int To store the
second number
entered by the
user.
3. mergnum int Stores the final
merged number.
Program 3:
The coordinates of a point P on a two-dimensional plane can be
represented by P(x, y) with x as the x-coordinate and y as the
oordinate. The coordinates of the midpoint of two points P1(x1, y1)
and P2(x2, y2) can be calculated as P(x, y) where: [10] x=(x1+x2)/2
y=(y1+y2)/2
Design a class Point with the following details:
Class name: Point
Data Members/instance variables:
x: stores the x-coordinate
y: stores the y-coordinate Member functions:
Point (): constructor to initialize x = 0, y = 0
void readpoint (): accepts the coordinates x and y of a point
Point midpoint (Point A, Point B): calculates and returns the midpoint
of the two points A and B
void displaypoint (): displays the coordinates of a point
Specify the class Point giving details of the constructor (), member
functions void readpoint ( ), Point midpoint (Point, Point) and void
displaypoint () along with the main () function to create an object and call
the functions accordingly to calculate the midpoint between any two
given points
import java.util.*;
public class Point
{
double x,y;
Point()
{
x=0.0d;
y=0.0d;
}
void readpoint()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter x and y coordinate
of the point respectively");
x=sc.nextDouble();
y=sc.nextDouble();
}
Point midpoint(Point A, Point B)
{
Point C=new Point();
C.x=(A.x+B.x)/2.0;
C.y=(A.y+B.y)/2.0;
return C;
}
void displaypoint()
{
System.out.println("Midpoint:
("+x+","+y+")");
System.out.println("X-coordinate: "+x+"\nY-
coordinate: "+y);
}
public static void main(String args[])
{
Point X=new Point();
Point Y=new Point();
Point Z=new Point();
X.readpoint();
Y.readpoint();
Z=X.midpoint(X,Y);
Z.displaypoint();
}
}
VARIABLE DESCRIPTION TABLE
SERIAL VARIABLE DATATYPE DESCRIPTION
NUMBER NAME
1. x double Stores
x-coordinate of
the point.
2. y double Stores
y-coordinate of
the point.
BIBLIOGRAPHY: