Вы находитесь на странице: 1из 112

SCHOOL:- ST.

TERESA’s
COLLEGE

by:-AKARSH
SRIVASTAVA
1
class-12th
science
INDEX

S. NO PROGRAM PAGE NO. SIGNATURE

2
Acknowledgement
In the accomplishment of this project successfully, many people have
given me their blessings and the heart pledged support, this time I
am utilising to thank all the people who have been concerned with
this project.

Primarily, I would thank god for being able to complete this project
with success. Then I would like to thank my principal Mrs.Geetika
Kapoor and computer teacher Mr.Luqman Khan whose valuable
guidance has been the ones that helped me patch this project and
make it full proof success. His suggestions and his instructions has
served as the major contributor towards the completion of the
project.

Then I would like to thank my and friends for their strong and
continuous support. Their valuable suggestions and guidance has
been helpful in various phases of the completion of the project.

Last but not the least I would like to thank my classmates who have
helped me a lot.

-AKARSH SRIVASTAVA

3
Introduction to java
Java is a general purpose object oriented language. It was developed
in 1991 in the U.S.A. by Sun Microsystems.

It is much reliable and portable language than C++. It has web-


oriented features and is very popular among web designers also. Java
is a platform-independent language.

JAVA FEATURES-

Java possesses the following features :

(i) It is platform independent.


(ii) It is object oriented.
(iii) Programs developed in it can be run on any system.
(iv) Java is highly suitable for Internet, Web designing, etc.

JAVA ENVIROMENT-

Java environment has a number of tools and hundreds of


libraries. All the development tools are the parts of a system known
as Java Development Kit (JDK). Classes and methods are the parts
of Java Standard Library (JSL), which are also called Application
Program Interface (API).

BLUEJ-

BlueJ is a windows based platform for Java Development Kit (JDK).


It can work for JDK 1.3 version and not for lower versions.

New Java Projects can be opened in that environment. BlueJ is an


IDE (Integrated Development Enviroment).

4
Double
dimensional
array Programs

5
Ques.01-WAP to input numbers in 4*4 matrix and display the
original matrix. Transpose the original matrix and display it.
import java.util.*; //Package included for Scanner class

class transpose //Start of class

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner (System.in);

int arr[][]=new int[4][4]; //Array declaration

int i,j;

System.out.println("Enter the numbers in 4*4 matrix");

for(i=0;i<4;i++) //loop to input numbers in


matrix form

for(j=0;j<4;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original matrix :-");

for(i=0;i<4;i++)

for(j=0;j<4;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();
6
}

System.out.println("Transpose matrix :-");


for(i=0;i<4;i++)

for(j=0;j<4;j++)

System.out.print(arr[j][i]+"\t");

System.out.println();

} //End of void main

} //End of class

Output :-

Enter the numbers in 4*4 matrix

10

11

12 7
13

14
15

16

Original matrix :-

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Transpose matrix :-

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

arr Int Double Dimensional Array to store


the numbers in the matrix
i int Used for outer for loop

j Int Used for inner for loop

Ques.02-WAP to input numbers in 4*4 matrix and display only


8
boundary elements of the matrix.
import java.util.*; //Package included for Scanner class

class boundary //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner (System.in);

int arr[][]=new int[4][4]; //Array declaration

int i,j;

System.out.println("Enter the numbers in 4*4 matrix");

for(i=0;i<4;i++) //loop to input numbers in matrix


form

for(j=0;j<4;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original matrix :-");

for(i=0;i<4;i++)

for(j=0;j<4;j++)

System.out.print(arr[i][j]+"\t");

System.out.println(); 9
}

System.out.println("Boundary Elements matrix :-");


for(i=0;i<4;i++)

for(j=0;j<4;j++)

if(i==0||j==0||i==3||j==3)

System.out.print(arr[i][j]+"\t");

else

System.out.print(" "+"\t");

System.out.println();

} //End of void main

} //End of class

Output :-

Enter the numbers in 4*4 matrix

8 10
9

10
11

12

13

14

15

16

Original matrix :-

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Boundary Elements matrix :-

1 2 3 4
5 8
9 12
13 14 15 16

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Arr int Double Dimensional Array to store


the numbers in the matrix
I int Used for outer for loop

J int Used for inner for loop

11
Ques.03-WAP to input numbers in 4*4 matrix and display the
elements above the right diagonal.
import java.util.*; //Package included for Scanner class

class diagonal //Start of class

public static void main(String args[]) //Start of void


main

{ Scanner sc=new Scanner(System.in);

int arr[][]=new int[4][4]; //Array declaration

System.out.println("Enter the numbers in 4*4 matrix");

int i=0,j=0;

for(i=0;i<=3;i++) //loop to input numbers in matrix


form

for(j=0;j<=3;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(i=0;i<=3;i++)

for(j=0;j<=3;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

} 12
System.out.println("Elements above right diagonal are");

for(i=0;i<4;i++)
{

for(j=0;j<4;j++)

if((i+j)<3)

System.out.print(arr[i][j]+"\t");

System.out.println();

} //End of void main

} //End of class

Output :-

Enter the numbers in 4*4 matrix

10

11

12 13
13

14
15

16

Original Matrix

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Elements above right diagonal are

1 2 3
5 6
9

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

arr int Double Dimensional Array to store


the numbers in the matrix
i int Used for outer for loop

j int Used for inner for loop

Ques.04-WAP to input numbers in 4*4 matrix in DDA. Arrange


the numbers of each row in ascending order and display the
14
result.
import java.util.*; //Package included for Scanner class
class arrangement //Start of class

public static void main(String args[]) //Start of


void main

Scanner sc=new Scanner(System.in);

int i,j,k,temp=0;

int arr[][]=new int [4][4]; //Array declaration

System.out.println("Enter the numbers in 4*4 matrix");

for(i=0;i<4;i++) //loop to input numbers in matrix form

for(j=0;j<4;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(i=0;i<4;i++)

for(j=0;j<4;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

}
15
for(k=0;k<4;k++)

{
for(i=0;i<3;i++)

for(j=0;j<3-i;j++)

if(arr[k][j]>arr[k][j+1])

temp=arr[k][j];

arr[k][j]=arr[k][j+1];

arr[k][j+1]=temp;

System.out.println("Sorted Matrix");

for(i=0;i<4;i++)

for(j=0;j<4;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

} //End of void main

} //End of class

Output :- 16
Enter the numbers in 4*4 matrix

4
3

12

11

10

16

15

14

13

Original Matrix

4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13
Sorted Matrix

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

VARIABLE DESCRIPTION TABLE 17


Variable Data Type Description

arr int Double Dimensional Array to store


the numbers in the matrix
i int Used for outer for loop

j int Used for inner for loop

k int Used in for loop for sorting

temp int Used for swapping

Ques.05-WAP to declare a square matrix of order N(n<20).


18
Allow the user to input positive integers into matrix. Perform
the following tasks on the matrix.

(a) Output the original matrix.


(b) Find the SADDLE POINT for the matrix. A saddle point
is an element of the matrix such that it is the minimum
for the row to which it belongs and the maximum
element for the column to which it belongs. Saddle
point for a given matrix is always unique. If the matrix
has no saddle point, show the message “NO SADDLE
POINT”.

Test your program for the following data and some random
data:

Sample data:

Input n=4

Matrix ARR[ ][ ]=

2 5 6 9
8 4 1 3
2
6 7 3 1
1 2 2 1
2 4 1
Output:

Matrix ARR[ ][ ]=

2 5 6 9
8 4 1 3
2
6 7 3 1
1 2 2 1
2 4 1
No Saddle Point

19
import java.util.*; //Package included for Scanner class
class Saddle //Start of class

public static void main() //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the matrix size");

int n=sc.nextInt();

int min=0,max=0,flag=0;

int arr[][]=new int[n][n]; //Array declaration

int i=0,j=0,k=0,r=0,c=0;

System.out.println("Enter numbers in 4*4 matrix");

for(i=0;i<n;i++) //loop to input numbers in matrix


form

for(j=0;j<n;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

System.out.print(arr[i][j]+"\t");

}
20
System.out.println();

}
for(i=0;i<n;i++)

min=arr[i][0];

c=0;r=0;

for(j=0;j<n;j++)

if(arr[i][j]<min)

min=arr[i][j];

c=j;

max=arr[0][c];

for(k=0;k<n;k++)

if(arr[k][c]>max)

max=arr[k][c];

r=k;

if(min==max)

{ flag=1;

break;

} 21
else

{
min=0;

max=0;

if(flag==1)

System.out.println("Saddle point is "+min+" at the index


("+r+","+c+")");

else

System.out.println("No Saddle Point");

} //End of void main

} //End of class

Output :-

Enter the matrix size

Enter numbers in 4*4 matrix

10

11
22
12

13
14

15

16

Original Matrix

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Saddle point is 13 at the index (3,0)

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Arr int Double Dimensional Array to store


the numbers in the matrix
I int Used for outer for loop

J int Used for inner for loop

K int Used for inner for loop

min int Used to store minimum element of


the row
Max int Used to store maximum element of
the column

23
Ques.06-WAP to enter natural numbers in a double
dimensional array m*n (where m is the number of rows and n
is the number of columns). Display the greatest element of
the matrix. Display the new matrix.

Sample Input:

5 8 2 3
7 4 6 2
8 1 3 7
9 2 6 5
Sample Output:

9 8 2 9
7 9 9 2
8 9 9 7
9 2 6 9

import java.util.*; //Package included for Scanner class

class Replace //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of rows and columns");

int m=sc.nextInt();

int n=sc.nextInt();

int arr[][]=new int[m][n]; //Array declaration

int i=0,j=0;

System.out.println("Enter the numbers in "+m+"*"+n+" matrix");

form
for(i=0;i<m;i++) 24
//loop to input numbers in matrix

for(j=0;j<n;j++)
{

arr[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

int max=arr[0][0];

for(i=0;i<m;i++)

for(j=0;j<n;j++)

if(arr[i][j]>max)

max=arr[i][j];

System.out.println("Greatest element of the matrix is "+max);

for(i=0;i<m;i++)

for(j=0;j<n;j++) 25
{

if((i==j)||(i+j)==3)
arr[i][j]=max;

System.out.println("Modified Matrix");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

} //End of void main

} //End of class

Output :-

Enter the number of rows and columns

Enter the numbers in 4*4 matrix

4 26
6

2
8

Original Matrix

5 8 2 3
7 4 6 2
8 1 3 7
9 2 6 5
Greatest element of the matrix is 9

Modified Matrix

9 8 2 9
7 9 9 2
8 9 9 7
9 2 6 9

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Arr int Double Dimensional Array to store


the numbers in the matrix
I int Used for outer for loop

J int Used for inner for loop

Max int Used to store the greatest element

M int Used to store number of the rows

N int Used to store number of the


columns

27
Ques.07-WAP to store the numbers in n*m matrix in Double
Dimensional Array. Find the sum of the elements in each row
and each column. Display the sum of each row and each
column corresponding to the rows and columns.

Sample Input:

1 1 1 1
0 5 6 8
1 1 1 1
5 4 2 1
1 1 1 1
1 2 6 7
1 1 1 1
2 0 4 6
Sample Output:

1 1 1 1 5
0 5 6 8 9
1 1 1 1 5
5 4 2 1 2
1 1 1 1 5
1 2 6 7 6
1 1 1 1 5
2 0 4 6 2
4 5 5 6
8 1 8 2

import java.util.*; //Package included for Scanner class

class RCsum //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of rows and columns of the


matrix");

int n=sc.nextInt();

int m=sc.nextInt();
28
int arr[][]=new int[n+1][m+1]; //Array declaration

int i=0,j=0,s=0;
System.out.println("Enter numbers in the "+n+"*"+m+" matrix");

for(i=0;i<n;i++) //loop to input numbers in matrix


form

for(j=0;j<m;j++)

arr[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(i=0;i<n;i++)

for(j=0;j<m;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

for(i=0;i<n;i++)

s=0;

for(j=0;j<m;j++)

s=s+arr[i][j];

arr[i][m]=s;
29
}
for(j=0;j<m;j++)

s=0;

for(i=0;i<n;i++)

s=s+arr[i][j];

arr[n][j]=s;

System.out.println("Modified Matrix");

for(i=0;i<n+1;i++)

for(j=0;j<m+1;j++)

System.out.print(arr[i][j]+"\t");

System.out.println();

} //End of void main

} //End of class

Output :-

Enter the number of rows and columns of the matrix

Enter numbers in the 4*4 matrix 30


10

15
16

18

15

14

12

11

11

12

16

17

12

10

14

16

Original Matrix

10 15 16 18
15 14 12 11
11 12 16 17
12 10 14 16
Modified Matrix

10 15 16 18 59
15 14 12 11 52
11 12 16 17 56
12 10 14 16 52
48 51 58 62 0

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Arr int Double Dimensional Array to store


the numbers in the matrix
I int Used for outer for loop

J int Used for inner for loop


31
S int Used to store the sum of rows and
columns
N int Used to store number of the rows

M int Used to store number of the


columns

String Programs

32
Ques.08-WAP to accept a string in a mixed case and display
the same in reversed order using ‘String Tokenizer’.

import java.util.*; //Package included for Scanner class

class reverse_order //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter a sentence");

String str=sc.nextLine();

String rev="";int i;

StringTokenizer str1=new StringTokenizer(str," ");

int c=str1.countTokens();

for(i=1;i<=c;i++)

String w=str1.nextToken();

rev=w+" "+rev;

System.out.println("The sentence in reverse order is");

System.out.println(rev);

} //End of void main

} //End of class
33
Output :-

Enter a sentence

This is a cat

The sentence in reverse order is

cat a is This

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

str String To store the string entered by the


user
i int Used in for loop

c int Stores the number of tokens

w String Stores every token in it one by one

rev String Stores the reversed String

str1 String Object of class StringTokenizer

34
Ques.09-WAP to accept a sentence and convert ‘a’ to ‘an’
present in the string. The alphabet ‘a’ must not be the part of
word present in the string.
import java.util.*; //Package included for Scanner class

class convert_article //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter a sentence");

String str=sc.nextLine();

str=" "+str+" ";

int l=str.length(); //storing the length of the


string

String str1="";int i;

for(i=0;i<l-2;i++)

char ch=str.charAt(i);

char ch1=str.charAt(i+1);

char ch2=str.charAt(i+2);

if(ch==' '&&ch1=='a'&&ch2==' ')

str1=str1+ch1+"n";

else

str1=str1+ch1;

System.out.println("New sentence is-");


35
System.out.println(str1);

} //End of void main


} //End of class

Output :-

Enter a sentence

He is a nice and a good boy.

New sentence is-

He is an nice and an good boy.

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

str String To store the String entered by the


user
i int Used in for loop

l int Stores the length of the String

ch char Stores character at index ‘i’

ch1 char Stores character at index ‘i+1’

ch2 char Stores character at index ‘i+2’

str1 String To store the new sentence

36
Ques.10-WAP to accept two words in lower case and frame a
new word by taking one by one character from each word.
Assuming that the length of both the words is same.
import java.util.*; //Package included for Scanner class

class frame //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

int i,l;

char ch1,ch2;

String str1,str2,str3="";

System.out.println("Enter the first String");

str1=sc.nextLine();

l=str1.length(); //storing the length of the string

System.out.println("Enter the second String");

str2=sc.nextLine();

for(i=0;i<l;i++)

ch1=str1.charAt(i);

str3=str3+ch1;

ch2=str2.charAt(i);

str3=str3+ch2;

System.out.println("The new String is-");

System.out.println(str3); 37
} //End of void main

} //End of class
Output :-

Enter the first String

dizzy

Enter the second String

world

The new String is-

dwiozrzlyd

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

str1 String To store the first String

i int Used in for loop

l int Stores the length of the first String

str2 String To store the second String

ch1 char Stores character of first String

ch2 char Stores character of second String

str3 String To store the new String

38
Ques.11-WAP to accept a String from the user and display the
frequency of word ‘the’ in the String.

import java.util.*; //Package included for Scanner class

class frequency //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

int i,l,f=0;

char ch;

String str1,str2="";

System.out.println("Enter a String");

str1=sc.nextLine();

str1=str1+" ";

l=str1.length(); //storing the length of the string

for(i=0;i<l;i++)

ch=str1.charAt(i);

if(ch!=' ')

str2=str2+ch;

else

if(str2.equalsIgnoreCase("the"))

f=f+1;

str2="";
39
}

}
System.out.println("Total frequency of 'the' in the string is-"+f);

} //End of void main

} //End of class

Output :-

Enter a String

This is the best place in the world.

Total frequency of 'the' in the string is-2

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

str1 String To store the String entered by the


user
i int Used in for loop

l int Stores the length of the first String

str2 String To store each word for comparing

ch char Stores character of the String

f int To store the frequency of the


String

40
Ques.12-A simple encryption system uses a shifting process
to hide a message.The value of the shift can be in the range
of 1 to 26(otherwise,error ‘Invalid Shift Value’ should
appear).

For example, Shift 7 means that A=U, B=V, C=W and so on


---------.

Text: A BC D E F G H I J K L M N O P Q R S T U V W X Y Z

Code; U V W X Y Z A B C D E F G H I J K L M N O P Q R S T

An extra space is added to the end of the string. To make


things a little more difficult, spaces within the original text
are replaced with QQ before the text in encrypted. Double Q
(QQ) was selected because no English word ends with Q or
QQ.

Additionally, the coded message is printed in blocks of six


characters separated by spaces. The last block might not
contain six characters. Write a program that takes the coded
text(less than 100 characters), the shift value and prints the
decoded original text. Your program must reject any non valid
value for shift. Assume all characters in upper case.

Input coded text : UHINBYLKKQCHHYLKK

Shift : 7

Decoded Text : ANOTHER WINNER

Input coded text : RUIJGGEVGGBKSAGG

Shift : 29

Decoded Text : Invalid Shift Value

41
import java.util.*; //Package included for Scanner class

class Decode //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Coded Text");

String str=sc.nextLine();

int l=str.length(); //storing the length of the string

System.out.println("Enter Shift");

int a=sc.nextInt();

int i=0,n=0,b=0;

str=str.toUpperCase();

String str1="";

if(a>26)

System.out.println("Invalid Shift Value");

else

for(i=0;i<l;i++)

char ch=str.charAt(i);

b=(int)ch;
42
n=b+(a-1);

if(n>90)
n=n-26;

char ch1=(char)n;

str1=str1+ch1;

str1=str1.replace("QQ"," ");

System.out.println("Decoded Text");

System.out.println(str1);

} //End of void main

} //End of class

Output :-

Enter the Coded Text

UHINBYLKKQCHHYLKK

Enter Shift

Decoded Text

ANOTHER WINNER

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Str String To store the String entered by the


user
I int Used in for loop as the index

L int Stores the length of the String

str1 String Stores the decoded String

Ch char
43
Stores character of the String

A int To store the shift value


B int To store the ASCII code of each
character of the String

Ques.13-WAP in Java to accept a string . Display the


character, which occurs the maximum number of times within
the string. The program also displays whether the character
is a vowel or a consonant.

Sample Input: PRAYER IS ESSENTIAL IN ALL STAGES OF LIFE

Sample Output: The character with maximum frequency: E

The character E is a vowel

import java.util.*; //Package included for Scanner class

class StringFun //Start of class

public static void main(String args[]) //Start of void


main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Sentence");

String str=sc.nextLine();

str=str.toUpperCase();

int l=str.length(); //storing the length of the string

int i=0,j=0,c=0,m=0;

char ch1,ch2;

char x=' ';

for(i=65;i<=90;i++)

{ ch1=(char)i;

c=0;
44
for(j=0;j<l;j++)

{
ch2=str.charAt(j);

if(ch1==ch2)

c++;

if(c>m)

m=c;

x=ch1;

System.out.println("The character with maximum frequency:


"+x);

if("AEIOU".indexOf(x)>-1)

System.out.println("The character "+x+" is a vowel");

else

System.out.println("The character "+x+" is a consonant");

} //End of void main

} //End of class

Output :-

Enter the Sentence

PRAYER IS ESSENTIAL IN ALL STAGES OF LIFE

The character with maximum frequency: E

The character E is a vowel

45
VARIABLE DESCRIPTION TABLE

Variable Data Type Description

str String To store the String entered by the


user
i int Used in outer for loop

j int Used in inner for loop

l int Stores the length of the String

ch1 char Stores all the alphabets one by


one
ch2 char Stores the character of the String

x char Stores the character with


maximum frequency
c int To store the frequency of a
character
m int To store the maximum frequency
of a character

46
Ques.14-While typing, a typist has made two or more
consequtive blank spaces in a sentence. Write a program in
Java to replace two or more consequtive blanks by a single
blank.

Also Display the total number of vowels as well as total


number of consonants.

Sample Input: Understanding - - - - Computer - - - - - - -


Science - - - is - - - - written - - - - - - - - - - by - - - - - - - Dilip - -
- - - - Kumar - - - - - - Dey - - - - - and - - - - Vijay - - - Kumar - - -
- - - - - - Pandey

Sample Input: Understanding Computer Science is written by


Dilip Kumar Dey and Vijay Kumar Pandey

import java.util.*; //Package included for Scanner class

class Typist //Start of class

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Sentence");

String str=sc.nextLine();

str=str+" ";

int l=str.length(); //storing the length of the string

int i=0;

char ch1,ch2;

String str1="";

int c=0,v=0; 47
for(i=0;i<(l-1);i++)
{

ch1=str.charAt(i);

ch2=str.charAt(i+1);

if("aeiouAEIOU".indexOf(ch1)>-1)

v++;

else if("aeiouAEIOU".indexOf(ch1)==-1&&ch1!=' ')

c++;

if(ch1==' '&&ch2==' ')

continue;

else

str1=str1+ch1;

System.out.println("Modified Sentence is:");

System.out.println(str1);

System.out.println("Total Number of vowels are");

System.out.println(v);

System.out.println("Total number of consonants are:");

System.out.println(c);

} //End of void main

} //End of class

Output :-

Enter the Sentence

Raju is a very good boy

Modified Sentence is:

Raju is a very good boy 48


Total Number of vowels are

8
Total number of consonants are:

10

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Str String To store the String entered by the


user
I int Used in for loop as the index

L int Stores the length of the String

str1 String Stores the Modified String

ch1 char Stores character at the index i

ch2 char Stores character at the index (i+1)

C int To store the number of consonants

V int To store the number of vowels

49
PATTERN &
SERIES
PROGRAMS

50
Ques.15- Write a programme to print the following
pattern
54321
4321
321
21
1
21
321
4321
54321
class pattern
{
public static void main(String args[])
{
int a,b,c,i,j,k,p=0,q=2,w=4;
System.out.println(“the pattern”);
for(i=5;i>=1;i--)
{
for(j=0;j<=p;j++)
{
System.out.print(k);
}
System.out.println();
}
p=p+1;
}
for(a=1;a<=4;a++)
{
51
for(b=w;b>=1;b--)
{
System.out.print(“ “);

}
for(c=q;c>=1;c--)
system.out.print(c);
}
System.out.println();
Q=+1;
W=w=+1;
}
}
}

VARIABLE DESCRIPTION TABLE

Variable Data type Description

A int To run the for loop


B int To run the for loop
C int To run the for loop
I int To run the for loop
K int To run the for loop
P int It’s a counter
variable
Q int It’s a counter
variable
W int It’s a counter
variable
J int To run the for loop

Output :-

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
2 1
3 2
4 3 2
1
1
52
5 4 3 2 1
Ques.16- WAP to generate the following pattern-
* * * * *
* *
* *
* *
* * * * *
class Pattern2 //Start of class

public static void main() //Start of voidmain

int i=0,j=0;

System.out.println("The Pattern is...");

for(i=1;i<=5;i++) //loop to print first row

System.out.print("*"+" ");

System.out.println();

for(i=1;i<=3;i++) //loop to print 3 middle rows

for(j=1;j<=5;j++)

if(j==1||j==5)

System.out.print("*"+" ");

if(j>1&&j<5)

53
System.out.print(" "+" ");

}
System.out.println();

for(i=1;i<=5;i++) //loop to print last row

System.out.print("*"+" ");

} //end of void main

} //end of class

Output :-

The Pattern is...

* * * * *

* *

* *

* *

* * * * *

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

I int Used for outer for loop

J Int Used for inner for loop

54
Ques.17- WAP to generate the following pattern-
0
12
345
6789
543
21
0
class Pattern3 //Start of class

public static void main() //Start of void main

int i=0,j=0,p=0;

System.out.println("The Pattern is...");

for(i=1;i<=4;i++) //loop to print upper half

for(j=1;j<=i;j++

System.out.print(p+" ");

p++;

System.out.println();

p=5;
55
for(i=3;i>=1;i--) //loop to print lower half

{
for(j=i;j>=1;j--)

System.out.print(p+" ");

p--;

System.out.println();

} //end of void main

} //end of class

Output :-

The Pattern is...

12

345

6789

543

21

56
VARIABLE DESCRIPTION TABLE
Variable Data Type Description

I int Used for outer for loop

J Int Used for inner for loop

P Int Used to store numbers in the


Pattern

57
Ques.18- WAP to print the sum of the following series-
x x2 x3
s= + + … … … … … .. nterms
1 2 3

import java.util.*; //Package included for Scanner Class

class series //Start of Class

public static void main(String args[]) //Start of Void Main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value of n");

int n=sc.nextInt();

System.out.println("Enter the value of x");

int x=sc.nextInt();

double s=0,i,num=1;

for(i=1;i<=n;i++) //Start of For Loop

s=s+Math.pow(x,num)/i;

num++;

} //End of For loop

System.out.println("The sum of the series is "+s);

} //End of Void Main

} //End of Class

58
Output :-
Enter the value of n
3

Enter the value of x

The sum of the series is 6.666666666666666

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

I Double Used in for loop and as a


denominator

S Double Used to store the sum of series

Num Double Used as power of x

X Int Used as a numerator

N Int Used to store number of terms

59
Ques.19- WAP to print the sum of the following series-
x x x x
s= − + − … … … … … .. n terms
1 2 3 4

import java.util.*; //Package included for Scanner Class

class series_1 //Start of Class

public static void main(String args[]) //Start of Void Main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value of n");

int n=sc.nextInt();

System.out.println("Enter the value of x");

int x=sc.nextInt();

double s=0.0,sign=1.0,i;

for(i=1;i<=n;i++) //Start of For Loop

s=s+sign*(x/i);

sign=-sign;

} //End of For loop

System.out.println("The sum of the series is "+s);

} //End of Void Main

} //End of Class

60
Output :-
Enter the value of n
2

Enter the value of x

The sum of the series is 1.5

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

I Double Used in for loop and as a


denominator

S Double Used to store the sum of series

Sign Double Used to change the sign


alternately

X Int Used as a numerator

N Int Used to store number of terms

61
Ques.20- WAP to print the sum of the following series-
1 2 3 4
s=1+ + + + … … … … … … n terms
1! 2! 3! 4!

import java.util.*; //Package included for Scanner


Class

class series_2 //Start of Class

public static void main(String args[]) //Start of Void Main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value of n");

int n=sc.nextInt();

int i,f=1,j;double s=1;

for(i=1;i<=n;i++) //Start of outer For


Loop

f=1;

for(j=1;j<=i;j++) //Start of inner For


Loop

f=f*j;

} //End of inner For


Loop

s=s+i/f;

} //End of outer For


loop

System.out.println("The sum of the series is "+s);

}
62
//End of Void Main

//End of Class

Output :-
Enter the value of n
5

The sum of the series is 3.0

VARIABLE DESCRIPTION TABLE


Variable Data Type Description

I Int Used in outer for loop and as a


numerator

S Double Used to store the sum of series

F Int Used to find the factorial and as a


denominator

J Int Used in inner for loop

N Int Used to store number of terms

63
Number
programs

64
Ques.21-WAP to print all the Armstrong numbers between
100 and 1000.

An  Armstrong number of three digits is an integer such that


the sum of the cubes of its digits is equal to
the number itself.

Ex :−13 +53 +33=153 (It is an Armstrong number.)

class Armstrong //Start of Class

public static void main(String args[]) //Start of Void Main

int i=0,t=0,s=0,r=0;

System.out.println("The armstrong numbers between 100 and 1000


are -");

for(i=100;i<=1000;i++) //Start of for loop

t=i; s=0; r=0;

while(t>0) //Start of while


Loop

r=t%10;

s=s+r*r*r;

t=t/10;

} //End of while
Loop

if(s==i)

System.out.print(i+", ");

} 65
//End of for Loop

} //End of Void
Main
} //End of Class

Output :-

The armstrong numbers between 100 and 1000 are -

153, 370, 371, 407,

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

I int Used in the for loop

T int Used as a temporary variable to


store the value of i
R int Used to store the last digit of the
number
S int Used to store the sum

66
Ques.22-WAP to input a binary number and convert it into its
decimal Equivalent.

import java.util.Scanner; //Package included for


Scanner class

class binary_conversion //Start of class

public static void main(String args[]) //Start of void main

int n, d=0, i=1, r=0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter Binary Number : ");

n = sc.nextInt();

int t=n;

while(t!= 0) //Start of while


loop

r =t%10;

d = d + r*i;

i = i*2;

t = t/10;

} //End of while loop

System.out.println("Equivalent Decimal Value of " +n+ " is :");

System.out.println(d);

} 67
//End of void main

} //End of class
Output:-

Enter Binary Number :

1100

Equivalent Decimal Value of 1100 is :

12

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

N int Used to store the Binary number


input by the user
T int Used as a temporary variable to
store the value of n
R int Used to store the last digit of the
number in each loop
D int Used to store the Decimal Equivalent
of the binary number
I int Used in conversion

68
Ques.23-WAP to input two numbers and print the HCF and
LCM of the two numbers.

import java.util.Scanner; //Package included for


Scanner class

public class hcf_lcm //Start of class

public static void main(String args[]) //Start of void main

int a, b, x, y, t, hcf, lcm;

Scanner scan = new Scanner(System.in);

System.out.println("Enter Two Number : ");

x = scan.nextInt();

y = scan.nextInt();

a = x;

b = y;

while(b != 0) //Start of while loop

t = b;

b = a%b;

a = t;

} //End of while loop

hcf = a;

lcm = (x*y)/hcf;

System.out.println("HCF = " +hcf);

System.out.println("LCM = " +lcm);


69
} //End of void main

} //End of class
Output:-

Enter Two Number :

10

HCF = 2

LCM = 30

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

X int Used to store the number input by


the user
Y int Used to store the number input by
the user
A int Temporary Variable, used to store
the value of x
B int Temporary Variable, used to store
the value of y
Hcf int Used to store the HCF of the
numbers
Lcm int Used to store the LCM of the
numbers
T int Variable used for swapping the
numbers for the execution of the
program

70
Ques.24-WAP to input a number and check whether it is a
Unique number or not.

A Unique number is a positive integer (without leading zeros)


with no duplicate digits. For example 7, 135, 214 are all
unique numbers whereas 33, 3121, 300 are not.
import java.util.*; //Package included for Scanner class

class unique //Start of class

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

int i, r=0,c=0, flag=1;

int t;

for(i=0;i<=9;i++) //Start of for loop

t=n; c=0;

while(t>0) //Start of while loop

r=t%10;

if(i==r)

c++;

t=t/10;

} //End of while loop

if(c>1)

{ 71
flag=0;

break;
}

} //End of for loop

if(flag==1)

System.out.println("It is an Unique Number");

else

System.out.println("It is not an Unique Number");

} //End of void main

} //End of class

Output:-

Enter the number

4215

It is an Unique Number

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

N int Used to store the number input by


the user
I int Used in the for loop and to check
repetition of each digit in number
T int Used as a temporary variable to
store the value of n
R int Used to store the last digit of the
number
C int Used to count the no. of times a
digit is repeated
Flag int A variable whose value defines
whether the no is unique or not

72
Ques.25-WAP to input a decimal number and convert it into
its Binary Equivalent.
import java.util.*; //Package included for Scanner
class

class decimal_conversion //Start of class

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

long n=sc.nextInt();

long c=1,r=0,rev=0;

long t=n;

while(t>0) //Start of while loop

r=t%2;

rev=rev+r*c;

c=c*10;

t=t/2;

} //End of while loop

System.out.println("The Binary equivalent is");

System.out.println(rev);
73
} //End of void main

} //End of class
Output:-

Enter the number

12

The Binary equivalent is

1100

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

N long Used to store the Decimal number


input by the user
T long Used as a temporary variable to
store the value of n
R long Used to store the last digit of the
number in each loop
Rev long Used to store the Binary Equivalent
of the binary number
C long Used in conversion

74
Ques.26-WAP to input elements in an array and sort them by
using insertion sort.
public class InsertionSort //Start of class

public static void insertionSort(int array[]) //Start of void


insertionSort

int n = array.length;

for (int j = 1; j < n; j++)

int key = array[j];

int i = j-1;

while ( (i > -1) && ( array [i] > key ) )

array [i+1] = array [i];

i--;

array[i+1] = key;

} //End of void insertionSort

public static void main(String a[]) //Start of void main

int[] arr1 = {9,14,3,2,43,11,58,22};

System.out.println("Before Insertion Sort");

for(int i:arr1)

}
System.out.print(i+" ");
75
System.out.println();
insertionSort(arr1); //sorting array using insertion sort

System.out.println("After Insertion Sort");

for(int i:arr1)

System.out.print(i+" ");

} //End of void main

} //End of class

Output:-

Before Insertion Sort


9 14 3 2 43 11 58 22
After Insertion Sort
2 3 9 11 14 22 43 58

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

N int To store the length of the array

J int Used in for loop for indexing

Array int Formal parameter to store the array

arr1 int To store the actual array

76
Function
programs

77
Ques.27-Write a program to accept two numbers and check
whether they are twin prime or not. The function returns 1, if
it is twin prime otherwise, returns 0.

Twin Prime numbers are the prime numbers whose difference


is 2.

e.g. : (5, 7), (11, 13)........................are the twin primes.

import java.util.*; //Package included for Scanner class

class Twinp //Start of class

int prime(int n) //Start of function ‘prime’

int i,c=0,f=0;

for(i=1;i<=n;i++) //Start of For loop

if(n%i==0)

c=c+1;

} //End of For loop

if(c==2)

f=1;

return(f);

} //End of function ‘prime’

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

int m,p,x,y;

Twinp ob=new Twinp();


78
//Object Creation Line

System.out.println("Enter your first number");

m=sc.nextInt();
System.out.println("Enter your second number");

p=sc.nextInt();

x=ob.prime(m);

y=ob.prime(p);

if((x==1&&y==1)&&(m-p==2||p-m==2))

System.out.println("The number "+m+" and "+p+" are twin primes");

else

System.out.println("The number "+m+" and "+p+" are not twin


primes");

} //End of void main

} //End of class

Output :-

Enter your first number

11

Enter your second number

13

The number 11 and 13 are twin primes

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

int
C int Used to breaking the number into
two parts
int A variable for returning 1 or 0
N int Formal parameter to store the
entered number
M int Used to store First number
P int Used to store Second number
X int 79
Used to store the returned value
from function
Y int Used to store the returned value
from function
Ques.28-WAP to enter a number and check whether it is a
kapraker number or not, using function.

Kaprekar numbers for a given base is a non-negative integer,


the representation of whose square in that base can be split
into two parts that add up to the original number again. For
instance, 45 is a Kaprekar number, because 45² = 2025 and
20+25 = 45

Return 1 if the sum of parts is equal to the number else


return 0.

import java.util.*; //Package included for Scanner class

class kaprakers //Start of class

public int kap(int n) //Start of function 'kap'

int t, c=1;

t=n;

while(t>0) //Start of while loop

c=c*10;

t=t/10;

} //End of while loop

int sq=n*n;

int r=sq%c;

int p=sq/c;

int z=p+r;
80
if(z==n)

return 1;
else

return 0;

} //End of function 'kap'

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int a=sc.nextInt();

kaprakers ob=new kaprakers();

int x=ob.kap(a);

if(x==1)

System.out.println("It is a Kapraker number");

else

System.out.println("It is not a Kapraker number");

} //End of void main

} //End of class

81
Output :-

Enter the number

45

It is a Kapraker number

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

A int Actual parameter to store the


number input by user
Sq int To store the square of the number
Z int To store the sum of two parts
N int Formal parameter to store the
entered number
X int Used to store the returned value
from function
R int To store the half part of the square
P int To store the other half part of the
square

82
Ques.29-WAP to enter a number and check whether it is an
Evil number or not, using function. It returns 1 if the number
of 1’s is even else returns 0.

An Evil number is a positive whole number which has even


number of 1’s in its binary equivalent.

import java.util.*; //Package included for Scanner class

class Evil //Start of class

public int Enumber(long n) //Start of function 'Enumber'

long c=1, r=0, p=0,rev=0;

long t=n;

while(t>0) //while loop for conversion of decimal number into


binary

r=t%2;

rev=rev+r*c;

c=c*10;

t=t/2;

System.out.println("The Binary Equivalent is:-");

System.out.println(rev);

t=rev;

83
while(t>0) //while loop for counting no. of 1 in binary
equivalent

r=t%10;

if(r==1)

p++;

t=t/10;

if(p%2==0)

return 1;

else

return 0;

} //End of function 'Enumber'

public static void main(String args[]) //Start of void main

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number-");

long a=sc.nextInt();

Evil ob=new Evil();

long z=ob.Enumber(a);

if(z==1)

System.out.println("It is an Evil Number");

else

System.out.println("It is not an Evil Number");

}
} //End of void main

//End of class
84
Output :-

Enter the number-

12

The Binary Equivalent is:-

1100

It is an Evil Number

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

A long Actual parameter to store the


number input by user
R long To store the last digit of a number
Rev long To store the binary equivalent
N long Formal parameter to store the
entered number
Z long Used to store the returned value
from function
T long Temporary variable to store the
value of n and rev
P long To store the no. of occurrence of 1 in
binary equivalent
C long Used in conversion

85
Ques.30-WAP to enter a number and check whether it is an
Automorphic number or not, using function. It returns 1 if the
number is automorphic else returns 0.

import java.util.*; //Package included for Scanner class

class automorphic //Start of class

public int morphic(int n) //Start of function 'morphic'

int c=0, t=n;

while(t>0) //Start of while loop

c++;

t=t/10;

} //End of while loop

int sq=n*n;

System.out.println("The square of the number is:- "+sq);

int z=(int)Math.pow(10,c);

int k=sq%z;

if(k==n)

return 1;

else

return 0;

} //End of function 'morphic'


86
public static void main(String args[]) //Start of void main
{

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int a=sc.nextInt();

automorphic ob=new automorphic();

int x=ob.morphic(a);

if(x==1)

System.out.println("It is an Automorphic number");

else

System.out.println("It is not an Automorphic number");

} //End of void main

} //End of class

Output :-

Enter the number

25

The square of the number is:- 625

It is an Automorphic number

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

A int Actual parameter to store the


number input by user
Sq int To store the square of the number
N int Formal parameter to store the
entered number
X int Used to store the returned value

T int
from function
87
Temporary variable to store the
value of n
Ques.31- A class Merger concatenates two positive integers
that are greater than 0 ad produces a new merged integer.
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 first


number.

n2 : long integer to store second


number.

mergNum : long integer to store merged


number.

Member functions:

Merger : constructor to initialize the data


members

void readNum() : to accept the values of the data


members n1 and n2

void JoinNum() : to concatenate the numbers n1


and n2 and store it in mergNum

void show() : to display the original numbers


and the merged number

Specify the class Merger, giving the details of the constructor, void
readNum(), void JoinNum(), void show(). Define the main() function to
create an object and call the functions accordingly.

import java.util.*; //Package included for Scanner class

class Merger //Start of class

long n1,n2,mergNum;
88
Merger() //Constructor

{
n1=0;

n2=0;

mergNum=0;

void readNum() //to accept values of data


members

Scanner sc=new Scanner(System.in);

System.out.println("Enter two numbers");

n1=sc.nextInt();

n2=sc.nextInt();

void JoinNum() //to concatenate the numbers

String s=Long.toString(n1);

String s1=Long.toString(n2);

String s2=s+s1;

mergNum=Long.valueOf(s2);

void show() //to display the numbers

System.out.println("First Number= "+n1);

System.out.println("Second Number= "+n2);

System.out.println("Merged Number= "+mergNum);

public static void main()

{
89
//Start of void main

Merger ob=new Merger();

ob.readNum();
ob.JoinNum();

ob.show();

} //End of void main

} //End of class

Output :-

Enter two numbers

23

746

First Number= 23

Second Number= 746

Merged Number= 23746

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

n1 long To store first number

n2 long To store second number

mergNum long Stores the merged number

S String To store the value of n1

s1 String To store the value of n2

s2 String To store the concatenated value

90
Ques.32- A class ConsChange has been defined with the
following details:
Class name : ConsChange

Data members/instance variables

word : stores the word

len : stores the length of the word

Member functions:

ConsChange() : default constructor

void readword() : accepts the word in lowercase

void shiftcons() : shifts all the consonants of the


word at the beginning followed by the vowels (e.g. spoon becomes
spnoo)

void changeword() : changes the case of all


occurring consonants of the shifted word to uppercase, for e.g. (spnoo
becomes SPNoo)

void show() : displays the original word,


shifted word and the changed word

Specify the class ConsChange, giving the details of the constructor,


void readword(), void shiftcons(), void changeword(), void show().
Define the main() function to create an object and call the functions
accordingly.

import java.util.*; //Package included for Scanner class

class ConsChange //Start of class

String word;

int len;

ConsChange() //Constructor

{
91
len=0;

word="";

}
void readword() //to accept word in lowercase

Scanner sc=new Scanner(System.in);

System.out.println("Enter word in Lower case");

word=sc.next();

len=word.length();

void shiftcons() //to shift all the consonants

String s="";

char c;

for(int i=0;i<len;i++)

c=word.charAt(i);

if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')

s+=c;

for(int i=0;i<len;i++)

c=word.charAt(i);

if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')

s+=c;

System.out.print("\n Sorted Word= "+s);

word=s;

} 92
void changeword() //to change the case of shifted
words

{
char c;String s="";

for(int i=0;i<len;i++)

c=word.charAt(i);

if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')

s+=(char)(c-32);

else

s+=c;

System.out.println("\n Changed Word= "+s);

void show() //to display the original, shifted and


changed word

System.out.print("\n Original Word= "+word);

shiftcons();

changeword();

public static void main() //Start of void main

ConsChange ob=new ConsChange();

ob.readword();

ob.show();

} //End of void main

} //End of class

93
Output :-
Enter word in Lower case

welcome

Original Word= welcome

Sorted Word= wlcmeoe

Changed Word= WLCMeoe

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Word String To store the original word

I int Used in for loop

Len int Stores the length of the word

S String To store the sorted and changed


word
C char Stores character of the original
word

94
inheritence
programs

95
Ques.33- Write a class Sentence to store a sentence and
another class Duplicate to replace the duplicate characters.
The details of the classes are given below:

Class name : Sentence

Data members/instance variables

str : To store a sentence in the


string variable of protected type

Member functions:

Sentence() : Constructor to assign string


variable

void Accept() : To accept a sentence in the


variable str

void Display() : To display the sentence after


performing task

Class name : Duplicate

Data members/instance variables

p : To store the length of the


sentence

Member functions:

void Remove() : To remove the duplicate


characters in sequence by its single occurrence in the string
. The new string should contain only one space
between two words, if it contains
more spaces in the original string.

Specify the class Sentence, giving the details of the constructor, void
Accept(), void Display(). Define the main() function to create an
object and call the functions accordingly 96
Using the concept of inheritance, specify the class Duplicate giving
the details of the functions void Remove(), also initialise both the
constructors in the sub-class.
Sample Input: Ammmiitt Kuuulkkkarnni iiissss aa nicccceee
bbbooy

Sample Output: Amit Kulkarni is a nice boy


import java.util.*; //Package included for Scanner class

class Sentence //Start of class

protected String str;

Sentence() //Constructor

str="";

void Accept()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the sentence");

str=sc.nextLine();

void Display()

System.out.println("Original Sentence");

System.out.println(str);

class Duplicate extends Sentence

int p;
97
Duplicate()

super();
p=0;

void Remove()

{ String str1="";

str=str+" ";

p=str.length();

for(int i=0;i<p-1;i++)

char ch1=str.charAt(i);

char ch2=str.charAt(i+1);

if(ch1==ch2)

continue;

else

str1=str1+ch1;

Display();

System.out.println("Modified Sentence");

System.out.println(str1);

public static void main(String args[]) //Start of void


main

Duplicate ob=new Duplicate();

ob.Accept();

ob.Remove();

}
} //End of void main

//End of class
98
Output :-

Enter the sentence

Ammmiitt Kuuulkkkarnni iiissss aa nicccceee bbbooy

Original Sentence

Ammmiitt Kuuulkkkarnni iiissss aa nicccceee bbbooy

Modified Sentence

Amit Kulkarni is a nice boy

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Str String To store the original sentence

I int Used in for loop

P int Stores the length of the sentence

str1 String To store the modified sentence

ch1 char Stores character at the index i

ch2 char Stores character at the index i+1

99
Ques.34- A super class Bank has been defined to store the
details of a customer. Define a sub-class Account thet enables
transactions for the customer with the bank. The details of
the both the classes are given below:

Class name : Bank

Data members/instance variables

name : stores the name of the


customer

accno : stores the account number

p : stores the principal


amount in decimals

Member functions:

Bank(…) : parameterised constructor to assign


values to instance variables

void Display() : To display the details of the


customer

Class name : Account

Data members/instance variables

amt : stores the transactions amount


in decimals

Member functions:

Account(…) : parametrized constructor to


assign values to the instance variables of both the classesv
void deposit(..) : accepts the amount and
updates the principal as
p=p+amt
void withdraw()
100
: accepts the amount and
updates the principal as p=p-amt
If the withdrawl amount is more than the principal amount, then
display message “INSUFFICIENT BALANCE” . If the principal amount
after withdrawl is less than 500, then a penalty is imposed by using
the formula
p=p-(500-p)/10
void display() : displays the details of the
customer
Assume that the super class Bank has been defined. Using the
concept of Inheritence, specify the class Account giving details of the
constructor(…), void deposit( ), void withdraw( ) and void display( ).

import java.util.*; //Package included for Scanner class


class Bank //Start of class
{
String name;
long accno;
double p;

Bank(String n, long a, double p1) //Parameterised


Constructor
{
name=n;
accno=1;
p=p1;

}
void display()
{
System.out.println("The name is "+name);
System.out.println("The account number is "+accno);
System.out.println("The Principal amount is "+p);
}
}

import java.util.*;
class Account extends Bank
{
double amt;
Account(String n1, long a1, double p2)
{
super(n1,a1,p2);

}
amt=0.0;
101
void deposit()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the amount to deposit");
amt=sc.nextDouble();
p=p+amt;
}
void withdraw()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the amount to withdraw");
amt=sc.nextDouble();
if(amt>p)
System.out.println("INSUFFICIENT BALANCE");
else
{
p=p-amt;
if(p<500)
p=p-(500-p)/10;
}
}
void display()
{
super.display();
System.out.println("The new amount is "+ p);
}
public static void main(String args[]) //Start of void
main
{
Account ob=new Account("Raam Singh",111254587,5000.0);
ob.deposit();
ob.withdraw();
ob.display();
} //End of void main
} //End of class

102
Output :-

Enter the amount to deposit


500
Enter the amount to withdraw
6000
INSUFFICIENT BALANCE
The name is Raam Singh
The account number is 1
The Principal amount is 5500.0
The new amount is 5500.0

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

Name String Stores the name of the customer

Accno long Stores the account number

P double Stores the principal amount in


decimals
Amt double To store the transaction amount in
decimals
N char Temporary variable to initialise
name in the constructor
a1 long Temporary variable to initialise
accno in the constructor
p2 double Temporary variable to initialise p
in the constructor

103
Stack
Programs

104
Ques.35- Register is an entity which can hold maximum 100
names. The register enables the user to add and remove
names from the top most end only.

Class name : Register

Data members/instance variables

stud[ ] : array to store the names of


the students

cap : stores the maximum capacity of


the array

top : to point the index of


the top end

Member functions:

Register(int max) : constructor to initialize


the data member cap=max, top=-1 and create the string array

void push(String n) : to add names in the


register at the top location if possible, otherwise display the message
“OVERFLOW”

String pop() : removes and returns the names


from the top most location of the register if any, else returns “$$”

void display( ) : displays all the names in the


register

Specify the class Register giving the details of the functions void
push (String) and String pop( ). Assume that the other functions have
been defined
The main function need NOT be written.
105
import java.util.*; //Package included for Scanner class

class Register //Start of class

String stud[]=new String[100];

int cap;

int top;

Register(int max) //Parameterised Constructor

cap=max;

top=-1;

void push(String n) //To add names at the top location

if(top==cap-1)

System.out.println("OVERFLOW");

else

top++;

stud[top]=n;

}
106
String pop() //To remove names from the top location

{
if(top==-1)

return "$$";

else

String n=stud[top];

top--;

return n;

void display()

for(int i=0;i<=top;i++)

System.out.println(stud[i]);

} //End of class

VARIABLE DESCRIPTION TABLE

Variable Data Type Description

stud[] String Array to store the names of the


students
Cap int Stores the maximum capacity of
the array
Top int To point the index of the top end

Amt double To store the transaction amount in


decimals
Max int Temporary variable to initialise
cap in the constructor
N String To add names in the register at the
top location

107
queue
programs

108
Ques.36- Queue is an entity which can hold maximum 100
integers. The queue enables the user to add integers from
the rear and remove integers from the front.

Class name : Queue

Data members/instance variables

Que[ ] : array to hold the integer


elements

size : stores the size of the


array

front : to point the index of the


front

rear : to point the index of the


rear

Member functions:

Queue(int mm) : constructor to initialize the


data sixe=mm, front=0, rear=0

void addele(int v) : to add integer from the


rear if possible, otherwise display “Overflow”

int delete() : removes elements from the front if


present, otherwise displays the message “Underflow” and return
-9999

void display( ) 109


: displays all array
elements

Specify the class Queue giving the details of the functions void
addele(int) and int delete( ). Assume that the other functions have
been defined
The main function need NOT be written.

import java.util.*; //Package included for Scanner class

class Queue //Start of class

int Que[]=new int[100];

int size;

int front;

int rear;

Queue(int mm) //Parameterised Constructor

size=mm;

front=0;

rear=0;

void addele(int v) //To add integers from the rear

{ 110
if(rear==size-1)

System.out.println("OVERFLOW");
else

if(front==0&&rear==0)

front=1;

rear=1;

else

rear++;

Que[rear]=v;

int delete() //To remove integers from the front

if(front==0&&rear==0)

System.out.println("Underflow");

return -9999;

else

int v=Que[front];

if(front==rear)

front=0;

rear=0; 111
}

else
front++;

return v;

void display()

for(int i=front;i<=rear;i++)

System.out.println(Que[i]);

} //End of class

THE END 112

Вам также может понравиться