Project
Project
ALGORITHM:
1. Begin
2. Declare memory variables(sc, n , a[][], i, j, f, min, col_ind, k)
3. Display “Enter size of matrix”
4. Take input from user and store in n
5. Display “Enter values row wise”
6. Create a 2D integer array a[][] with size n
7. Intialize i by 0 and j by 0
8. To run a loop continue steps 9 to 12 till the value of i is less than n
otherwise go to step 13
9. To run a loop continue steps 10 to 11 till the value of j is less than n
otherwise go to step 12
10. Take input from user and store it in respective row and column of the matrix
11. Increase j by 1 and go to step 9
12. Increase i by 1 and go to step 8
13. Display “Orginal Matrix”
14. Intialize i by 0 and j by 0
15. To run a loop continue steps 16 to 20 till the value of i is less than n
otherwise go to step 21
16. To run a loop continue steps 17 to 18 till the value of j is less than n
otherwise go to step 19
17. Display the corresponding value of array a[][] {[Link](a[i][j] + “ “);}
18. Increase j by 1 and go to step 16
19. Display a new line
20. Increase i by 1 and go to step 15
21. Initialize f by 0
22. Initialize i by 0
23. To run a loop continue steps 24 to till the value of i is less than n otherwise
go to step 34
24. Intialize min by a[i][0] and col_ind by 0 and j by 1
25. To run a loop continue steps 26 to 27 till the value of j is less than n
otherwise go to step 28
26. If the value of min is greater than the corresponding value if a[i][j] then
a. store the value of a[i][j] in min
b. store the value of j in col_ind
27. Increase j by 1 and go to step 25
28. Intialize k by 0
29. To run a loop continue steps 30 to 31 till the value of k is less than n
otherwise go to step 32
30. If the value of min is less than the corresponding value of array a[][] {if(min
< a[k][col_ind])} then break out of the loop
31. Increase k by 1 and go to step 29
32. If the value of k is equal to n then
a. Display “Saddle point value “ and the value of min
b. increase f by 1;
33. Increase the value of i by 1 and go to step 23
34. If the value of f is equal to 0
then Display “No saddle point”
35. End of program
PROGRAM:
import [Link].*;
class P16 // start of class
{
public static void main(String args[]) // start of main method
{
Scanner sc = new Scanner([Link]); // creating object of Scanner class to take input
[Link]("Enter size of matrix:");
int n = [Link]();
[Link]("Enter values row wise:");
int a[][] = new int[n][n];
// loop for taking input of the elements
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = [Link]();
}
}
[Link]("ORIGINAL MATRIX:");
// loop for displaying the original matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
[Link](a[i][j] + " ");
}
[Link]();
}
int f = 0;
// loop for checking the saddle point
for (int i = 0; i < n; i++)
{
// checking for the minimum value in a row and storing its column index int
min = a[i][0], col_ind = 0;
for (int j = 1; j < n; j++)
{
if (min > a[i][j])
{
min = a[i][j];
col_ind = j;
}
}
// checking for the maximum value in the column at the minimum index
int k;
for (k = 0; k < n; k++)
{
if (min < a[k][col_ind])
break;
}
// if condition is satisfied then printing the saddle point value
if (k == n)
{
[Link]("Saddle point value " + min); f+
+;
}
}
// in case of no saddle point
if (f == 0)
[Link]("No saddle point");
}
}
OUTPUT:
ALGORITHM:
1. Begin
2. Declare memory variables(sc, d, a, b, sum, c)
3. Display “Enter degree of polynomial”
4. Take input in d from user
5. Create a 1D array a[] with size d+1
6. Create a 1D array b[] with size d+1
7. Create a 1D array sum[] with size d+1
8. Display “Enter the coefficents in decreasing order”
9. Display in a new line “Enter the 1st polynomial”
10. Intialize i by 0
11. To run a loop continue steps 12 to 13 till the value of i is less than
(d+1) otherwise go to step 14
12. Take input from user and store in corresponding postion in array a[] {a[i] =
[Link]()}
13. Increase i by 1 and go to step 11
14. Display in a new line “Enter the 2nd polynomial”
15. Initialize i by 0
16. To run a loop continue steps 17 to 18 till the value of i is less than
(d+1) otherwise go to step 19
17. Take input from user and store in corresponding position in array b[] {b[i]
=[Link]()}
18. Increase i by 1 and go step 16
19. Initialize i by 0
20. To run a loop continue steps 21 to 22 till the value of i is less than
(d+1) otherwise go to step 23
21. Add the corresponding values of array a[] and b[] and store in array sum[]
22. Increase i by 1 and go to step 20
23. Initialize c by the value of d
24. Display in a new line “Sum of the two is: “
25. Initialize i by 0
26. To run a loop continue steps 27 to 29 until the value of i is less than
d otherwise go to step 30
27. Display the corresponding value of sum[] and “x^” and value of c and “+”
28. Decrease c by 1
29. Increase i by 1 and go to step 26
30. Display the corresponding value of sum[d] {[Link](sum[d])}
31. End of program
PROGRAM:
import [Link].*;
class P17 // start of class
{
public static void main(String args[]) // start of main method
{
Scanner sc = new Scanner([Link]); // creating object of Scanner class
[Link]("Enter degree of polynomial:");
int d = [Link](); // taking input of the size of the array int
a[] = new int[d + 1]; // creating array for 1st polynomial int
b[] = new int[d + 1]; // creating array for 2nd polynomial
int sum[] = new int[d + 1]; // creating array for storing sum
[Link]("Enter the coefficients in decreasing order");
[Link]("Enter the 1st polynomial:");
for (int i = 0; i < d + 1; i++) // taking input of 1st polynomial
{
a[i] = [Link]();
}
[Link]("Enter the 2nd polynomial:");
for (int i = 0; i < d + 1; i++) // taking input of 2nd polynomial
{
b[i] = [Link]();
}
// calculating the sum of two polynomials
for (int i = 0; i < d + 1; i++)
{
sum[i] = a[i] + b[i];
}
int c = d;
[Link]("\nSum of the two is:");
// printing the sum polynomial
for (int i = 0; i < d; i++)
{
[Link](sum[i] + "x^" + c + " +
"); c--;
}
[Link](sum[d]);
}
}
18) Write a program to input a sentence. Bring all the vowels of each
word in front and consonants at the back of each word. Print a
new sentence.
ALGORITHM:
1. Begin
2. Declare memory variables (sc, s, ns, st, str, s1, i, ch)
3. Display “Enter a sentence”
4. Take input from user and store it in s.
5. Intialize ns by null.
6. Create String Tokenizer object named st with s as parameter.
7. To run a loop continue steps 8 to 15 till st has more tokens.
8. Store the next token into str and initialize s1 by null.
9. Intialize i by 0.
10. To run a loop continue steps 11 to 13 till the value of i is less than the length
of str.
11. Store the character present in str at index i in ch.
12. If ch is present in “AEIOUaeiou”
then Add value of s1 to ch and store it
in s1. Else
Add ch to s1 and store it in s1.
13. Increase i by 1 and go to step 10.
14. Add the value of s1 and space to ns and store it in ns.
15. Go to step 7.
16. Display “ New Sentence: “ and the value of ns.
17. End of Program
PROGRAM:
import [Link].*;
class P18 // start of class
{
public static void main(String args[]) // start of main method
{
Scanner sc = new Scanner([Link]); // creating object of Scanner class
[Link]("Enter a sentence:");
String s = [Link](); // taking input of the sentence
String ns = ""; // variable to store the new sentence
StringTokenizer st = new StringTokenizer(s); // extracting each word of the sentence as a token
while ([Link]()) // loop for passing through each word
{
String str = [Link](); // storing the word in a variable
String s1 = "";
// loop for bringing vowels at front and consonants at back for
(int i = 0; i < [Link](); i++)
{
char ch = [Link](i); // extracting each character
// checking for vowel and performing necessary operations if
("AEIOUaeiou".indexOf(ch) > -1)
s1 = ch + s1;
else
s1 = s1 + ch;
}
ns = ns + s1 + " "; // framing the new sentence
}
[Link]("New Sentence: " + ns); // printing the new sentence
}
}
OUTPUT:
ALGORITHM:
1. Begin.
2. Declare memory variables(s, st, st1, ns, ns1, t, index, wtr, rw).
3. Display "Enter a sentence".
4. Input a sentence in s.
5. Extract all the words of s and store in st.
6. Extract all the words of s and store in st1.
7. Initialize ns and ns1 with "".
8. Display "Enter a word to replace".
9. Input a word in wtr.
10. Display "Enter a word by which it will be replaced".
11. Input a word in rw.
12. To run a loop continue steps 13 to 15 till st has more words left otherwise go to
step 16.
13. Store current word in str.
14. If the word is same as the word to be replaced then concatenate ns,rw and
a blank space and store in ns, otherwise concatenate ns, str and a blank
space and store in ns.
15. Go to step 12 to continue the loop.
16. Display "1st Replaced Sentence : " and ns.
17. To run a loop continue steps 18 to 22 till st1 has more words left otherwise
go to step 23.
18. Store current word in str1.
19. Convert str1 to lowercase and store in t.
20. Initialize index with 0.
21. If the index of wtr in t is greater than -1 then
concatenate ns1, all the letters of str1 from position 0 to the position of wtr
in t, rw, all the letters from position- the sum of the position of wtr in t and
the length of wtr- to the length of str1, a blank space and store in ns1.
otherwise concatenate ns1, str1, a blank space and store in ns1.
22. Go to step 17 to continue the loop.
23. Display "2nd Replaced Sentence: " and ns1.
24. End of program.
PROGRAM:
import [Link].*;
class P19 // start of class
{
public static void main(String args[]) // start of main method
{
Scanner sc = new Scanner([Link]); // creating object of Scanner
class [Link]("Enter a sentence:");
String s = [Link](); // taking input of the sentence
StringTokenizer st = new StringTokenizer(s); // breaking the sentence into words
StringTokenizer st1 = new StringTokenizer(s); // breaking the sentence into
words String ns = "", ns1 = "";
[Link]("Enter a word to replace:");
String wtr = [Link](); // word to replace
[Link]("Enter a word by which it will be
replaced:"); String rw = [Link](); // replacement word
// 1st method
while ([Link]()) // loop for iterating through the words
{
String str = [Link](); // storing each word in str
if ([Link](wtr)) // checking if the word is same as word to be
replaced ns = ns + rw + " "; // if yes, replacing
else
ns = ns + str + " "; // if no, not replacing
}
[Link]("1st Replaced Sentence: " + ns);
// 2nd method
while ([Link]()) // loop for iterating through the words
{
String str1 = [Link](); // storing each word in str
String t = [Link](); // converting into lowercase
int index = -1; // index variable
if ((index = [Link](wtr)) != -1) // if the word matches the word to replace
{
ns1 = ns1 + [Link](0, [Link](wtr))
+ rw
+ [Link]([Link](wtr) + [Link](), [Link]())
+ " ";
}
else
{
ns1 = ns1 + str1 + " ";
}
}
[Link]("2nd Replaced Sentence: " + ns1);
}
}
OUTPUT:
ALGORITHM:
A) Begin
B) Algorithm for void dudeneyNumber(int a)
1. Begin.
2. Initialise the variable sum to 0.
3. Store the value of a in the variable temp.
4. Repeat steps 5 and 6 while temp is greater than 0.
5. Find the last digit of temp and add it to sum.
6. Divide temp by 10.
7. If the cube of sum is equal to a, then display "a is a Dudeney Number".
8. Otherwise, display "a is NOT a Dudeney Number".
9. End of method.
C) Algorithm for void goldBack(int a, int b)
1. Begin.
2. Display the message "Goldback numbers from a to b".
3. Repeat steps 4 to 12 for all numbers n from a to b.
4. If n is greater than 2 and even, then proceed.
5. Initialise flag variable f to 0.
6. Repeat steps 7 to 11 for all values of i from 2 to n/2.
7. Count the number of factors of i.
8. Count the number of factors of (n - i).
9. If both i and (n - i) have exactly two factors, then they are prime.
10. If f is equal to 0, display "n = " and increment f.
11. Display the pair i + (n - i).
12. Move to the next value of n.
13. End of method.
D) Algorithm for int Fibonacci(int a, int b)
1. Begin.
2. Initialise f1 to 0 and f2 to 1.
3. Initialise countPrimeFib to 0.
4. Display the message "Fibonacci numbers between a and b".
5. If a is equal to 0, display f1 and f2.
6. Repeat steps 7 to 13 while f3 is less than or equal to b.
7. Find the next Fibonacci number f3 = f1 + f2.
8. If f3 is greater than or equal to a, display f3.
9. Count the number of factors of f3.
10. If f3 has exactly two factors, increment countPrimeFib.
11. Assign f2 to f1.
12. Assign f3 to f2.
13. Continue the loop.
14. Return the value of countPrimeFib.
15. End of method.
E) End of Program
PROGRAM:
class NumberPrograms
{
void dudeneyNumber(int a)//Check if it is a Dudeney Number or not
{
int sum = 0, temp = a;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
if (sum * sum * sum == a)
[Link](a + " is a Dudeney Number");
else
[Link](a + " is NOT a Dudeney Number");
}
OUTPUT:
ALGORITHM:
A) Begin
B) Declare Data members(n, rev, p, q)
C) Algorithm for Constructor Number(int val, String word)
1. Begin.
2. Store the value of val in the data member n.
3. Store the value of word in the data member p.
4. Initialise the data member rev with zero.
5. Initialise the data member q with an empty string.
6. End of constructor.
D) Algorithm for void reverse(int num)
1. Begin.
2. If the value of num is equal to zero, then return to the calling method.
3. Find the last digit of num.
4. Multiply the value of rev by ten and add the last digit to it.
5. Call the method reverse again by passing the value obtained after dividing num by ten.
6. End of method.
E) Algorithm for String reverse2(String h)
1. Begin.
2. If the length of the string h is less than or equal to one, then return h.
3. Call the method reverse2 by passing the substring of h starting from the second character
and concatenate the first character of h at the end.
4. Return the reversed string.
5. End of method.
F) Algorithm for void check()
1. Begin.
2. Call the method reverse by passing the value of n.
3. If the value of n is the same as the value of rev, then display "n is a palindrome
number", otherwise display "n is not a palindrome number".
4. Call the method reverse2 by passing the string p and store the returned value in rwd.
5. If the string p is equal to rwd ignoring the difference between uppercase and lowercase
letters, then display "p is a palindrome word", otherwise display "p is not a palindrome word".
6. End of method.
G) Algorithm for void main()
1. Begin.
2. Create an object of the Scanner class to accept input values.
3. Display the message "Enter a number".
4. Accept the number and store it in the variable num.
5. Display the message "Enter a word".
6. Accept the word and store it in the variable wd.
7. Create an object obj of class Number by passing num and wd as arguments to the
parameterised constructor.
8. Call the method check using the object obj.
9. End of main.
H) End of Program
PROGRAM:
import [Link].*; class
Number
{
// Data members int
n, rev;
String p, q;
// Constructor to initialize data members
Number()
{
n=rev=0; p=q=null;
}
Number(int val, String word)
{
n = val;
p = word;
rev = 0;
q = "";
}
//Reverse the number n using recursion void
reverse(int num)
{
if (num == 0)
{
return;
}
else
{
int digit = num % 10;
rev = rev * 10 +
digit; reverse(num /
10);
}
}
// Reverse the word recursively
String reverse2(String h)
{
if ([Link]() <= 1)
{
return h;
}
return reverse2([Link](1)) + [Link](0);
}
// Check if n and p are palindromes
void check()
{
reverse(n);
if (n == rev) {
[Link](n + " is a palindrome number.");
} else {
[Link](n + " is not a palindrome number.");
}
// Check string palindrome using reverse2
String rwd = reverse2(p);
if ([Link](rwd)) {
[Link]( p + " is a palindrome word.");
} else {
[Link](p + " is not a palindrome word.");
}
}
void main()
{
Scanner sc = new
Scanner([Link]);
[Link]("Enter a number:
"); int num = [Link]();
[Link]();
[Link]("Enter a word: ");
String wd = [Link]();
Number obj = new Number(num, wd);
[Link]();
}
}
OUTPUT:
22) Write a program to design the following class
Number2 Data members: int n, p, c, s;
Constructors: 3 constructor (2 parameterized[1 parameter and 2
parameter] and 1 default)
Methods:
i)void calculate1() – Will calculate the sum of the cube of
digits of the number (recursively).
ii) void check1() – Will check the number is Armstrong or not.
iii) void count() – Will count no. of digits of p by recursion.
iv)void check2() – Will print all automorphic numbers present
in range 10-100 recursively.
v) void main(): Print a menu i) Armstrong ii)
Automorphic Input a the number, create an
object and call proper
method to check Armstrong and for Automorphic create
object with 2 values.
ALGORITHM:
A) Begin.
B) Declare data members(n, p, c, s).
C) Algorithm for default constructor
1. Initialize n,p,c and s with 0.
2. End of default constructor.
D) Algorithm for parameterized constructor
1. Begin.
2. Initialize the n of the current object with n passed as parameter.
3. Initialize p with n.
4. Initialize c and s with 0.
5. End of parameterized constructor.
E) Algorithm for void calculate1()
1. Begin.
2. Declare memory variable(d).
3. If n is greater than 0
i) Divide n by 10 and store the remainder in d.
ii) Calculate the sum of s and the cube of d and store the result in s.
iii) Divide n by 10 and store the quotient in n.
iv) Recursively call calculate1() method.
4. End of method.
F) Algorithm for void check1()
1. Begin.
2. If s is equal to p then display p and " is
Armstrong". otherwise display p and "is not
Armstrong".
3. End of method.
G) Algorithm for void count()
1. Begin.
2. If p is greater than 0
i) Increase the value of c by 1.
ii) Divide p by 10 and store the quotient in p.
iii) Recursively call count() method
3. End of method
H) Algorithm for void check2()
1. Begin.
2. If n is greater than 100 then return the control to method
call. otherwise
i) Store n in p.
ii) Store 0 in c.
iii) Call count() method.
iv) Calculate the square of n and store in s.
v) Divide s by 10 raised to the power of c and store the remainder in p.
vi) If n is equal to p then display n.
vii) Increase the value of n by 1.
viii) Recursively call check2() method.
3. End of method.
I) Algorithm for void main()
1. Begin.
2. Declare memory variables(op,no).
3. Display "Choose an option: 1. Armstrong Number Checking 2.
Automorphic Numbers in the range 10 - 100" in separate lines.
4. Input the option in op.
5. If op is equal to 1
i) Create an object obj1 of Number2 class with no parameters.
ii) Display "Enter a number".
iii) Input a number in no.
iv) Create an object obj1 of Number2 class with no as parameter.
v) Call calculate1() method of obj1 object.
vi) Call check1() method of obj1 object.
otherwise if op is equal to 2
i) Create an object obj2 of Number2 class with no parameters.
ii) Create an object obj2 of Number2 class with 10 and 100 as parameters.
iii) Call check2() method of obj2 object.
otherwise
display "Wrong choice".
6. End of method
J) End of program.
PROGRAM:
import [Link].*;
class Number2 // start of class
{
Scanner sc = new Scanner([Link]); // creating object of Scanner class to take input int
n, p, c, s; // declaring data members
Number2() // default constructor to initialize data members
{
n = p = c = s = 0;
}
Number2(int n) // parameterized constructor with one parameter
{
this.n = n; p
= n;
c = s = 0;
}
Number2(int n, int p) // parameterized constructor with two parameters
{
this.n = n;
this.p = p;
c = s = 0;
}
// method for calculating sum of cube of digits using recursion void
calculate1()
{
if (n > 0) // base condition
{
int d = n % 10;
s = s + (d * d * d);
n = n / 10;
calculate1(); // calling recursively
}
}
// method to check for Armstrong number
void check()
{
if (s == p)
[Link](p + " is Armstrong");
else
[Link](p + " is not Armstrong");
}
// method for counting the number of digits using recursion
void count()
{
if (p > 0)
{
c++;
p = p / 10;
count(); // calling recursively
}
}
// method for printing automorphic numbers in the range 10 - 100 using recursion
void check2()
{
if (n > 100) // base condition
return;
else
{
p = n;
c = 0;
count(); // counting number of digits
s = n * n;
s = s % (int)[Link](10, c);
// checking for automorphic number using its logic
if (n == s)
{
[Link](n);
}
n++;
check2(); // calling recursive
}
}
// main method for running the program
void main()
{
[Link]("Choose an option:\n1. Armstrong Number Checking\n2. Automorphic
Numbers in the range 10 – 100” );
int op = [Link](); // taking input of option
switch (op)
{
case 1: // option 1: Armstrong number
Number2 obj1 = new Number2();
[Link]("Enter a number:");
int no = [Link](); // taking input of number
obj1 = new Number2(no); // creating object
[Link](); // calculating sum of cube of
digits [Link](); // checking for armstrong number
break;
case 2: // option 2: Automorphic number in 10 - 100
Number2 obj2 = new Number2(10, 100); // creating object
obj2.check2(); // checking for automorphic
number break;
default: [Link]("Wrong choice"); // base case
}
}
}
OUTPUT:
ALGORITHM:
A) Begin.
B) Declare data members(a,b) which will contain words
C) Declare array (wd[])
D) Algorithm for parameterized constructor
1. Begin
2. Declare memory variable(c,i).
3. Initialize a with s.
4. Add a blank space to a.
5. Initialize c and i with 0.
6. To run a loop continue steps 6 to 7 if is less than the length of a otherwise
go to step 8.
7. If the character at i position of the sentence a is a blank space then
increase the value of c by 1.
8. Increase the value of i by 1 and go to step 5 to continue the loop.
9. Create an array wd[] of size c.
10. End of parameterized constructor.
E) Algorithm for void extract()
1. Begin.
2. Declare memory variables(word,x,i).
3. Initialize word with "",x with 0 and i with 0.
4. To run a loop continue steps 5 to 6 if the value of i is less than the length of
a otherwise go to step 7.
5. If the character at i position of a is not equal to a blank space then
concatenate the character to word and store in word
otherwise
i) Store word in the x position of wd[].
ii) Initialize word with "".
iii) Increase the value of x by 1.
6. Increase the value of by 1 and go to step 4 to continue the loop.
7. End of method.
F) Algorithm for void change1()
1. Begin.
2. Declare memory variables(v,x,i,t,p,j).
3. Initialize v with "AEIOUaeiou",x with "" and i with 0.
4. To run a loop continue steps 5 to 12 if i less than the length of wd otherwise
go to step 13.
5. Store the word at i position of wd in t.
6. Initialize p and i with 0.
7. To run a loop continue steps 8 to 10 if j is less than the length of t otherwise
go to step 11.
8. Find the position of the character which appears at j position of t in the
variable v and store the result in p.
9. If p is not equal to -1 then move the control out of the loop.
10. Increase the value of j by 1 and go back to step 7 to continue the loop.
11. If p is equal to -1 then concatenate x,t and "ay" and store the result in
x otherwise if j is equal to 0 then concatenate x,t and a blank space in x
otherwise concatenate x, all the letters extracted from j position of t till the end,
all the letters extracted from
0 position of t till j and "ay" and store in x.
12. Increase the value of i by 1 and go back to step 4 to continue the loop.
13. Display "Pig Latin Form:" and x.
14. End of method.
G) Algorithm for void change2()
1. Begin.
2. Declare memory variable(i).
3. Initialize b with "" and i with 0.
4. To run a loop continue steps 5 to 6 if i is less than the length of wd
otherwise go to step 7.
5. Call modify() method with the word at i position of wd as
parameter, concatenate it with b and a blank space and
store the result in b.
6. Increase the value of i by 1 and go to step 4 to continue the loop.
7. Display "Modified sentence =" and b.
8. End of method.
H) Algorithm for String modify(String s)
1. Begin.
2. Declare memory variables(v,i,t,t1).
3. Store "AEIOUaeiou" in v.
4. Initialize i with 0.
5. To run a loop continue steps 6 to 7 if i is less than the length of s otherwise
go to step 8.
6. If the position of the character,at i position of s, of v is not equal to -1 then
i) Extract all the letters from i position of s till the i+1 position of s and store in t.
ii) Concatenate all the letters extracted from 0 position of s till the i position of
s with all the letters extracted
from i+1 position of till the end and store in t1.
iii) Return t concatenated with the recursive call of modify with t1 as parameter.
7. Increase the value of i by 1 and go back to step 5 to continue the loop.
8. Return s.
9. End of method.
I) Algorithm for void main()
1. Begin.
2. Display "Enter a sentence".
3. Input a sentence in s.
4. Create an object ob of class Sentence with s as parameter.
5. Call extract(), change1() and change2() methods of class ob.
6. End of method.
J) End of program.
PROGRAM:
import [Link].*;
class Sentence
{
String a, b, wd[];
// Constructor to initialize the sentence and count the number of words
Sentence(String s)
{
a = s;
a = a + " ";
int c = 0;
for (int i = 0; i < [Link](); i++)
{
if ([Link](i) == ' ')
c++;
}
wd = new String[c];
}
// Method to extract words from the sentence and store them in an array
void extract()
{
String word = "";
int x = 0;
for (int i = 0; i < [Link](); i++)
{
if ([Link](i) != ' ')
word = word + [Link](i);
else
{
wd[x++] = word;
word = "";
}
}
}
// Method to convert the sentence to Pig Latin
void change1()
{
String v = "AEIOUaeiou", x = "";
for (int i = 0; i < [Link]; i++)
{
String t = wd[i];
int p = 0, j = 0;
for (; j < [Link](); j++)
{
p=
[Link]([Link](j)); if
(p != -1)
break;
}
if (p == -1)
{
x = x + t + "ay ";
}
else if (j == 0)
{
x = x + t + " ";
}
else
{
x = x + [Link](j) + [Link](0, j) + "ay ";
}
}
[Link]("Pig Latin Form: " + x);
}
// Method to modify the sentence using recursion
void change2()
{
b = "";
for (int i = 0; i < [Link]; i++)
{
b = b + modify(wd[i]) + " ";
}
[Link]("Modified sentence = " + b);
}
// Recursive method to modify a word
String modify(String s)
{
String v = "AEIOUaeiou";
for (int i = 0; i < [Link](); i++)
{
if ([Link]([Link](i)) != -1)
{
String t = [Link](i, i + 1);
String t1 = [Link](0, i) + [Link](i + 1);
return (t + modify(t1));
}
}
return s;
}
// Main method to accept input and perform transformations
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a
sentence"); String s = [Link]();
OUTPUT:
ALGORITHM:
A). Begin
B). Declare data members(a,b,ch).
C) Algorithm for parameterized constructor
1. Begin
2. Initialize a with s, b with "", and ch with 0
3. End of parameterized constructor.
D) Algorithm for void encode()
1. Begin.
2. Declare memory variables(n,i,j,f,vowel).
3. Store the length of a in n.
4. Declare array(arr[]) which will contain characters of size n.
5. Initialize i,j and f with 0.
6. Store "AEIOUaeiou" in vowel.
7. Initialize array(code[]) with "V1","V2","V3","V4","V5".
8. To run a loop continue steps 9 to 10 if i less than n otherwise go to step 11.
9. Store the character at i position of a in the i position of arr.
10. Increase the value of i by 1 and go to step 8 to continue the loop.
11. Initialize i with 0.
12. To run a loop continue steps 13 to 19 if I less than n otherwise go to step 20.
13. Initialize f with 0.
14. Initialize j with 0.
15. To run a loop continue steps 16 to 17 if j is less than 10 otherwise go to step 18.
16. If the character at i position of arr is equal to character at j position of
vowel then
i) Concatenate the word at j position of code with b and store the result in b.
ii) Store 1 in f.
iii) Take the control out of the loop.
17. Increase the value of j by 1 and go to step 15 to continue the loop.
18. If f is equal to 0 and the character at i position of arr is a letter then
i) If the character at i position of arr is either- greater than 'A' and less than
'X'- or greater than 'a' and less than 'x' then
a) Increase the ASCII of the character at i position of arr by 3 and store in
the same position
b) Concatenate the character at i position of arr with b and store the result
in b.
ii) otherwise
a) Decrease the ASCII of the character at i position of arr by 23 and store
in the same position.
b) Concatenate b with the character at i position of arr and store in
b. otherwise if f is equal to 0 then
Concatenate b with the character at i position of arr and store in b.
19. Increase the value of i by 1 and go to step 12 to continue the loop.
20. End of method.
E) Algorithm for void decode()
1. Begin.
2. Initialise an array vowel1[] with 'A','E','I','O','U'.
3. Initialise an array vowel2[] with 'a','e','i','o','u'.
4. Initialize n with the length of a.
5. Initialize i and j with zero.
6. Declare array(arr[]), which will contain characters, of size n.
7. To run a loop continue steps 8 to 9 if i is less than n otherwise go to step 10.
8. Store character which is in i position of a in i position of arr[].
9. Increase the value of i by 1 and go to step 7 to continue the loop.
10. To run a loop continue steps 11 to 13 if i is less than n otherwise go to step 14.
11. If the character at i position of arr is greater than 'A' and less than 'Z' then
i) If i is not equal to n-1 and the character at i position of arr is equal to 'V'
and the character at i+1 position of arr is a digit then
a) Remove 0 from the digit at i+1 position of arr and store in pos1.
b) Concatenate b with the character at pos1-1 position of vowel1.
c) Increase i by 2 and store in i.
d) Ignore the rest of the statements and go to the next iteration of the loop.
ii) otherwise
a) If the character at i position of arr is less than or equal to 'C' then
increase the ASCII of the character at i position of arr by 23 and store in that
same position and concatenate that character to b and store in b
b) otherwise subtract 3 from the ASCII of the character at i position of arr
and store in that same position and concatenate that character to b and
store
in b.
c) Increase the value of i by 1.
12. Otherwise if the character at i position of arr is greater than 'a' and less than
'z' then
i) If i is not equal to n-1 and the character at i position of arr is equal to 'v'
and the character at i+1 position of arr is a digit then
a) Remove 0 from the digit at i+1 position of arr and store in pos2.
b) Concatenate b with the character at pos2-1 position of vowel2.
c) Increase i by 2 and store in i.
d) Ignore the rest of the statements and go to the next iteration of the loop.
ii) otherwise
a) If the character at i position of arr is less than or equal to 'C' then
increase the ASCII of the character at i position of arr by 23 and store in that
same position and concatenate that character to b and store in b
b) otherwise subtract 3 from the ASCII of the character at i position of arr
and store in that same position and concatenate that character to b and
store
in b.
c) Increase the value of i by 1.
13. Otherwise concatenate b with the character at i position of arr and store in
b and increase the value of i by 1.
14. End of method.
F) Algorithm for void display()
1. Begin.
2. If ch is equal to 1 call encode() method and display b
3. Otherwise if ch is equal to 2 call decode() method and display b.
4. Otherwise display "Invalid choice".
5. End of method.
G) Algorithm for void main()
1. Begin.
2. Declare memory variable(s).
3. Display "Enter a sentence".
4. Input a sentence in s.
5. Create an object s1 of Pro9 class with s as parameter.
6. Display "Enter choice 1. Encode 2. Decode".
7. Input an integer value in ch variable of s1 object of class Pro9.
8. Call display() method of s1 object.
9. End of method.
H). End of program
PROGRAM:
import [Link].*; class
P24
{
String a, b; // Variables to store the input sentence and the encoded/decoded sentence
int ch; // Variable to store the user's choice
// Constructor to initialize the sentence and choice
P24(String s)
{
a = s;
b = "";
ch = 0;
}
// Method to encode the sentence void
encode()
{
int n = [Link]();
char arr[] = new char[n];
int i, j, f = 0;
String vowel = "AEIOUaeiou";
String code[] = {"V1", "V2", "V3", "V4", "V5", "v1", "v2", "v3", "v4", "v5"};
for (i = 0; i < n; i++)
arr[i] = [Link](i);
for (i = 0; i < n; i++)
{
f = 0;
for (j = 0; j < 10; j++)
{
if (arr[i] == [Link](j))
{
b += code[j]; f
= 1;
break;
}
}
if (f == 0 && [Link](arr[i]))
{
if ((arr[i] >= 'A' && arr[i] < 'X') || (arr[i] >= 'a' && arr[i] < 'x'))
{
arr[i] += 3;
b += arr[i];
}
else
{
arr[i] -= 23;
b += arr[i];
}
}
else if (f == 0)
b += arr[i];
}
}
// Method to decode the sentence
void decode()
{
char vowel1[] = {'A', 'E', 'I', 'O', 'U'};
char vowel2[] = {'a', 'e', 'i', 'o', 'u'};
int n = [Link](), i;
char arr[] = new char[n];
for (i = 0; i < n; i++)
arr[i] = [Link](i);
for (i = 0; i < n; )
{
if (arr[i] >= 'A' && arr[i] <= 'Z')
{
if (i != n - 1 && arr[i] == 'V' && [Link](arr[i + 1]))
{
int pos1 = (arr[i + 1] - '0');
b += vowel1[pos1 - 1];
i += 2;
continue;
}
else
{
if (arr[i] <= 'C')
{
arr[i] += 23;
b += arr[i];
}
else
{
arr[i] -= 3;
b += arr[i];
}
i += 1;
}
}
else if (arr[i] >= 'a' && arr[i] <= 'z')
{
if (i != n - 1 && arr[i] == 'v' && [Link](arr[i + 1]))
{
int pos2 = (arr[i + 1] - '0');
b += vowel2[pos2 - 1];
i += 2;
continue;
}
else
{
if (arr[i] <= 'c')
{
arr[i] += 23;
b += arr[i];
}
else
{
arr[i] -= 3;
b += arr[i];
}
i += 1;
}
}
else
{
b += arr[i];
i += 1;
}
}
}
// Method to display the encoded or decoded sentence
void display()
{
if (ch == 1)
{
encode();
[Link](b);
}
else if (ch == 2)
{
decode();
[Link](b);
}
else
[Link]("Invalid Choice");
}
// Main method to accept input and perform encoding or decoding
void main()
{
Scanner sc = new
Scanner([Link]);
[Link]("Enter sentence");
String s = [Link]();
P24 s1 = new P24(s);
[Link]("Enter choice : \[Link]\[Link]");
[Link] = [Link]();
[Link]();
}
}
OUTPUT:
25) Write a program to design the following class:
Int a[], b;
i) void input() – Take input of array elements in a[]
and element to search in b.
ii) void linear(int p) – Perform
linear search by recursion.
iii) void bubbleSortOuter(int i) –
The outer loop of bubble sort.
iv) void bubbleSortInner(int j, int k) – The inner loop of bubble sort.
v) int bisearch(int lo, int hi) – Will search element by binary
search recursively.
vi) void create() – Create one object. Call input() to input size
of array, elements of array, element to be searched. Then call
the methods. First linear then binary search.
ALGORITHM:
A) Begin
B) Declare data members(a[],b)
C) Algorithm for void input()
1. Begin.
2. Declare memory variables(c,i).
3. Display "Enter the size of the array".
4. Input the size in c.
5. Create array a of size c.
6. Display "Enter elements".
7. Initialize i with 0.
8. To run a loop continue steps 9 to 10 if i is less than c otherwise go to step 11.
9. Input a number at i position of a[].
10. Increase the value of i by and go to step 8 to continue the loop.
11. Display "Enter the element to be searched".
12. Input the element in b.
13. End of method.
D) Algorithm for void linear(int p)
1. Begin.
2) If p is equal to the length of a then display b and "not found" and return the
control to method call
otherwise
i) If the number at p position of a is equal to b then display b, "found at index",
p and “by linear search” and return the control to method call
ii) otherwise recursively call linear() with p+1 as parameter.
3. End of method.
E) Algorithm for void bubbleSortOuter(int i)
1. If i is equal to the length of a-1 then return the control to method call.
2. Call bubbleSortInner method with 0 and i as parameters.
3. Recursively call bubbleSortOuter with i+1 as parameter.
4. End of method.
F) Algorithm for void bubbleSortInner(int j, int k)
1. Begin.
2. Declare memory variable(temp).
3. If j is equal to the length of a-k-1 then return the control to method call.
4. If the number at j position of a is greater than the number at j+1 position of
a then
i) Store the number at j position of a in temp.
ii) Store the number which is at j position of a in j+1 position of a.
iii) Store temp in j+1 position of a.
5. Recursively call bubbleSortInner() with j+1 and k as parameter.
6. End of method.
G) Algorithm for int bisearch(int lo, int hi)
1. Begin.
2. Declare memory variable(mid).
3. If lo is greater than hi then return -1.
4. Add lo and hi then divide the result with 2 and store in mid.
5. If the number at mid position of a is equal to b then return mid
otherwise if the number at mid position of a is less than b then recursively call
bisearch() with mid+1 and hi as parameter and return it
otherwise recursively call bisearch() with lo and mid-1 as parameter and
return it.
6. End of method.
H) Algorithm for void create()
1) Call input() method.
2) Call linear() method with 0 as parameter.
3) Call bubbleSortOuter() method with 0 as parameter.
4) Call bisearch() method with 0 and the length of a as parameter and store
the return value in ans.
5) If ans is equal to -1 then display "Element not found"
otherwise display b, "Element found at index", ans and “by binary search after
sorting”.
6. End of method.
I) End of program.
PROGRAM:
import [Link].*; class
P25
{
int a[], b; // Declaration of array 'a' and integer 'b' void
input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of the array");
int c = [Link](); // Read the size of the array from user
a = new int[c];
[Link]("Enter elements");
for(int i = 0; i < c; i++)
{
a[i] = [Link](); // Read elements of the array from user
}
[Link]("Enter the element to be searched");
b = [Link]();
}
void linear(int p)
{
if(p == [Link])
{
[Link](b + " not found"); // If end of array is reached
return;
}
else
{
if(a[p] == b)
{
[Link](b + " found at index " + p + " by linear search");
return;
}
else
linear(p + 1);
}
}
void bubbleSortOuter(int i)
{
if(i == [Link] - 1)
return;
bubbleSortInner(0, i);
bubbleSortOuter(i + 1);
}
void bubbleSortInner(int j, int k)
{
if(j == [Link] - k - 1)
return;
if(a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
bubbleSortInner(j + 1, k);
}
int bisearch(int lo, int hi)
{
if(lo > hi)
return -1;
int mid = (lo + hi) / 2;
if(a[mid] == b)
return mid;
else if(a[mid] < b)
return bisearch(mid + 1, hi);
else
return bisearch(lo, mid - 1);
}
void create()
{
input(); // Take input from user
linear(0); // Perform linear
search bubbleSortOuter(0); // Sort
the array
OUTPUT:
26) Write a program to perform the following tasks by recursion:
i)boolean magic(int n) – To check n is magic or not. It
will call sod(int).
ii) int sod(int n) – To calculate sum of digits recursively.
iii) String change(String w)- To increase vowels by
2 positin and consonants by 3 positin. (Case
sensitie). Perform
the task recursively.
iv)boolean happy (int n) – To check a number is
happy or not. (Sum of square of all digits = 1). It will
call sosq (int).
v) int sosq(int n) – To calculate the sum of squares of digits.
vi)String remove (String k) – To remove all vowels from k.
vii) void main () – No object required. Just call the methods.
ALGORITHM:
Algorithm for boolean magic(int n)
1) Begin.
2. Declare memory variables(sum).
3. Call sod() method with n as parameter and store the result in sum.
4. If sum is equal to 1 then return true
otherwise if sum is greater than 9 then recursively call magic() method with
sum as parameter
otherwise return false.
5. End of method.
Algorithm for int sod(int n)
1. Begin.
2. If n is equal to 0 then return 0
otherwise return the sum of the remainder of integer division of n by 10 and
the result of the
recursive calling of sod() method with the quotient of integer division of n by 10
as parameter.
3. End of method.
Algorithm for String change(String w)
1. If the length of w is equal to zero then return
"". otherwise
i) Store the character at 0th position of w in ch.
ii) If ch is greater than or equal to 65 and less than or equal to 87
a) If ch is present in the string "AEIOU" then return the concatenation of the
character conversion of the ASCII of ch+2
and the recursive calling of change method with all the letters extracted
from position 1 to the end of
the sentence w as parameter
otherwise
return the concatenation of the character conversion of the ASCII of ch+3
and the recursive calling of change method with all the letters extracted
from position 1 to the end of
the sentence w as parameter
otherwise if ch is greater than or equal to 97 and less than or equal to 119
a) If ch is present in the string "aeiou" then return the concatenation of
the character conversion of the ASCII of ch+2
and the recursive calling of change method with all the letters extracted
from position 1 to the end of
the sentence w as parameter
otherwise
return the concatenation of the character conversion of the ASCII of ch+3
and the recursive calling of change method with all the letters extracted
from position 1 to the end of
the sentence w as
parameter otherwise
a) If ch is equal to 'X' then return the concatenation of "A" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
b) If ch is equal to 'Y' then return the concatenation of "B" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
c) If ch is equal to 'Z' then return the concatenation of "C" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
d) If ch is equal to 'x' then return the concatenation of "a" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
e) If ch is equal to 'y' then return the concatenation of "b" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
f) If ch is equal to 'z' then return the concatenation of "c" and the recursive
calling of change method with all the letters extracted from position 1 to the
end of the sentence w as parameter.
g) return "".
2. End of method.
Algorithm for boolean happy(int n)
1. Begin.
2. Declare memory variable(sum).
3. If n is equal to 1 then return true
otherwise if n is equal to 4 then return false
otherwise call sosq() method with n as parameter and store the result in sum
and return the recursive call of happy with sum as parameter.
4. End of method.
Algorithm for int sosq(int n)
1. If n is equal to 0 then return 0
otherwise return the sum of the square of the remainder of the integer division
of n by 10 and sosq() with n/10 as parameter.
2. End of method.
Algorithm for String remove(String k)
1. Begin.
2. Declare memory variable(ch).
3. If the length of k is equal to 0 then return
"". otherwise
i) Store the character at 0th position of k in ch.
ii) If ch is present in "AEIOUaeiou" then return the concatenation of ch and
the recursive call for remove method with
all the letters extracted from position 1 to the end of the sentence k as
parameter.
otherwise
return the recursive call for remove method with all the letters extracted
from position 1 to the end of the sentence k as parameter.
4. End of method.
Algorithm for void main()
1. Begin.
2. Declare memory variables(p,q).
3. Display "Enter a number to check if it is a magic number or not".
4. Input an integer value in p.
5. Store the result obtained from magic() method with p as parameter in ans1.
6. If ans1 is equal to true then display p and " is a Magic
number" otherwise display p and " is not a Magic number".
7. Display "Enter a string to encode it".
8. Input a sentence in q.
9. Display the result obtained from change() method with q as parameter.
10. Display "Enter a number to check if it is a happy number or not".
11. Input an integer value in p.
12. Store the result obtained from happy() method with p as parameter in ans2.
13. If ans2 is equal to true then display p and " is a Happy
number" otherwise display p and " is not a Happy number".
14. Display "Enter a string to remove its vowels".
15. Input a sentence in q.
16. Display the result obtained from remove() method with q as parameter.
17. End of method.
{End of program}.
PROGRAM:
import [Link].*;
class P26 // start of class
{
// Method to check if a number is a magic number
boolean magic(int n)
{
int sum = sod(n);
if(sum == 1)
return true;
else if(sum > 9)
return magic(sum); // Recursive call
else
return false;
}
// Method to find sum of digits
recursively int sod(int n)
{
if(n == 0)
return 0;
else
return (n % 10) + sod(n / 10);
}
// Method to encode/change characters of a string
String change(String w)
{
if([Link]() ==
0) return "";
else
{
char ch = [Link](0);
if(ch >= 65 && ch <= 87) // A to W
{
if("AEIOU".indexOf(ch) > -1)
return (char)(ch + 2) + change([Link](1));
else
return (char)(ch + 3) + change([Link](1));
}
else if(ch >= 97 && ch <= 119) // a to w
{
if("aeiou".indexOf(ch) > -1)
return (char)(ch + 2) + change([Link](1));
else
return (char)(ch + 3) + change([Link](1));
}
else
{
switch(ch)
{
case 'X': return "A" +
change([Link](1)); case 'Y': return "B"
+ change([Link](1));
case 'Z': return "C" +
change([Link](1)); case 'x': return "a"
+ change([Link](1)); case 'y': return
"b" + change([Link](1)); case 'z':
return "c" + change([Link](1));
}
}
return "";
}
}
// Method to check happy number
boolean happy(int n)
{
if(n == 1)
return true;
else if(n == 4)
return false;
else
{
int sum = sosq(n);
return happy(sum); // Recursive call
}
}
// Method to find sum of squares of digits
int sosq(int n)
{
if(n == 0)
return 0;
else
return (n % 10) * (n % 10) + sosq(n / 10);
}
// Method to remove vowels from a string
String remove(String k)
{
if([Link]() == 0)
return "";
else
{
char ch = [Link](0);
if("AEIOUaeiou".indexOf(ch) < 0)
return ch + remove([Link](1));
else
return remove([Link](1));
}
}
// start of main method
void main()
{
int p;
String q;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number to check if it is magic number or not");
p = [Link]();
boolean ans1 = magic(p);
if(ans1 == true)
[Link](p + " is a Magic number");
else
[Link](p + " is not a Magic number");
[Link]();
[Link]("Enter a String to encode it");
q = [Link]();
[Link](change(q));
[Link]("Enter a number to check if it is happy number or not");
p = [Link]();
[Link]();
boolean ans2 = happy(p);
if(ans2 == true)
[Link](p + " is a Happy number");
else
[Link](p + " is not a Happy number");
[Link]("Enter a String to remove its vowels");
q = [Link]();
[Link](remove(q));
}
} // end of class
OUTPUT:
PROGRAM:
import [Link].*; // importing [Link] package to take input using Scanner class
P27 // start of class
{
// Method to reverse a string recursively
String rev(String x)
{
if([Link]() == 0)
return ""; // Base case: empty string
else
{
char ch = [Link](0);
return rev([Link](1)) + ch;
}
}
// Method to remove vowels from a string recursively
String change(String a)
{
if([Link]() == 0)
return "";
else
{
char ch = [Link](0); // Extract the first character
if("AEIOUaeiou".indexOf(ch) < 0)
return ch + change([Link](1));
else
return change([Link](1)); // Skip vowel
}
}
// Method to reverse a number recursively
int reverse(int n, int r)
{
if(n == 0)
return r;
else
{
r = (r * 10) + (n % 10);
return reverse(n / 10, r);
}
}
// Method to encode string into ASCII (reversed)
String code(String x)
{
if([Link]() == 0)
return "";
else
{
char ch = [Link](0); // Extract the first character
return [Link](reverse((int)ch, 0)) +
code([Link](1));
}
}
// main method
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string");
String str = [Link](); // Taking input of a sentence
[Link]("\nString without vowels : " + change(str));
[Link]("\nString encoded in ASCII : " +
code(str) + " & " +
reverse([Link](code(str)), 0));
}
} // end of class
OUTPUT:
PROGRAM:
import [Link].*;
import [Link].*;
class P28 // start of class
{
public static void main() throws IOException
{
// Creating a file object
File file = new File("[Link]");
// Writing to file
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
Scanner sc = new Scanner([Link]);
[Link]("Enter number of students");
int n = [Link]();
for(int i = 1; i <= n; i++)
{
// Taking input of details
[Link]("Enter details for Student " + i);
[Link]("Enter roll number");
int roll = [Link]();
[Link]();
[Link]("Enter name of Student");
String name = [Link]();
[Link]("Enter Total Marks");
double marks = [Link]();
[Link]("Enter Grade");
char grade = [Link]().charAt(0);
try
{
[Link](roll); // Writing roll
number [Link](name); //
Writing name [Link](marks); //
Writing marks [Link](grade); //
Writing grade
}
catch(Exception E)
{
[Link]("ERROR");
}
}
[Link]();
// Reading from file
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
try
{
while(true)
{
int roll = [Link]();
String name = [Link]();
double marks = [Link]();
char grade = [Link]();
[Link](
"Roll Number : " + roll +
", Name : " + name +
", Marks : " + marks +
", Grade : " + grade
);
}
}
catch(Exception E)
{
[Link]("File parsed completely");
}
[Link]();
}
} // end of class
ALGORITHM:
1. Begin.
2 Declare memory variables (s, t, x, y, g, h, i, j, ch, c).
3 Open a file “E:\\[Link]” in writing mode using the object fw. If any error occurs, display
the error message and go to step 23.
4. Create BufferedWriter and PrintWriter objects bw and pw for writing to the file.
5. Display “Enter Paragraph:”.
6. Take input in variable s.
7. Write the content of s to the file and close the file using pw.
8. Open files “E:\\[Link]” and “E:\\[Link]” in writing mode using objects fw2 and fw1. If any
error occurs, display the error message and go to step 23.
9. Create BufferedWriter and PrintWriter objects bw2, pw2 for “[Link]” and bw1, pw1 for “[Link]”.
10. Initialize variables t, x, y as empty strings.
11. To process each character in s, run a loop from i = 0 to length of s – 1. Continue steps 12 to 17.
12. Extract the character ch from s at position i.
13. If ch is not a period (‘.’), append it to t. Otherwise, continue to step 14.
14. Append a space to t and initialize c to 0.
15. Run a loop from j = 0 to length of t – 1 to count the number of spaces.
16. If the number of spaces (c) is odd, remove extra spaces from t, append a period, and add it to
x. Otherwise, remove extra spaces from t, append a period, and add it to y.
17. Reset t to an empty string and repeat step 11.
18. Write the content of x to “[Link]” using pw1 and close the file.
19. Write the content of y to “[Link]” using pw2 and close the file.
20. Open files “E:\\[Link]” and “E:\\[Link]” in reading mode using objects frr and frrr. If any
error occurs, display the error message and go to step 23.
21. Create BufferedReader objects brr and brrr to read from the files.
22. Read lines from both files simultaneously until the end of either file is reached. Read line g
from “[Link]” and h from “[Link]”. Display “[Link]” followed by content g and “[Link]” followed by
content h. Close both file readers.
23. End of program.
PROGRAM:
import [Link].*;
import [Link].*; class
P29
{
public static void main()
{
Scanner sc = new Scanner([Link]);
//WRITING INTO FILES
try
{
FileWriter fw = new FileWriter("E:\\[Link]");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
[Link]("Enter Paragraph:");
String s = [Link]();
[Link](s);
[Link]();
FileWriter fw2 = new FileWriter("E:\\[Link]");
BufferedWriter bw2 = new BufferedWriter(fw2);
PrintWriter pw2 = new PrintWriter(bw2);
FileWriter fw1 = new FileWriter("E:\\[Link]");
BufferedWriter bw1 = new BufferedWriter(fw1);
PrintWriter pw1 = new PrintWriter(bw1);
String t = "", x = "", y = "";
for (int i = 0; i < [Link](); i++)
{
char ch = [Link](i);
if (ch != '.')
{
t = t + ch;
}
else
{
int c = 0;
t = t + " ";
for (int j = 0; j < [Link](); j++)
{
if ([Link](j) == ' ')
c++;
}
if (c % 2 != 0)
{
t = [Link]() + ".";
x = x + t;
}
else
{
t = [Link]() + ".";
y = y + t;
}
t = "";
}
}
[Link](x);
[Link](y);
[Link](); [Link](); [Link]();
[Link](); [Link](); [Link]();
[Link](); [Link](); [Link]();
}
catch (Exception e)
{
[Link](e);
}
//READING FROM FILES
try
{
String g = "", h = "";
FileReader fr = new FileReader("E:\\[Link]");
BufferedReader br = new
BufferedReader(fr); FileReader frr = new
FileReader("E:\\[Link]");
BufferedReader brr = new BufferedReader(frr);
while ((g = [Link]()) != null && (h = [Link]()) != null)
{
[Link]("[Link]"+"\n"+g);
[Link]("[Link]"+"\n"+h);
}
[Link]();
[Link]();
[Link]();
[Link]();
}
catch (Exception e)
{
[Link](e);
}
}//end of main
}//end of class
ALGORITHM:
1. Begin.
2. Declare memory variables (result, numbers[], number,
invalidNumber, parsedNumber, nullString)
3. Divide 10 by 0 and store the result in result
4. Arithmetic Exception is handled by the program showing error
message “Division by 0”
5 Create an array called numbers which stores 3 numbers 1 2 and 3.
6. Store the value present in index 5 of the array
7. Array Index Out of Bound Exception is handled by the program showing
error message “Array Index Out of Bound Exception”
8. Store “abc” in invalidNumber
9. Convert the word in invalidNumberto an integer and store it in parsedNumber.
10. Number Format Exception is handled by the program showing error
message “Invalid parsing”
11. Store null in nullString
12. Display the length of nullString
13. Null Pointer Exception is handled by the program showing error
message “Null Pointer Exception”
14. Display “Program completed successfully”
15. End of program
PROGRAM:
class P30
{
// start of class
public static void main()
{
// start of method try
{
// ArithmeticException
int result = 10 / 0; // Division by zero
}
catch(ArithmeticException e)
{
[Link]("ArithmeticException caught: " + [Link]());
}
try
{
// ArrayIndexOutOfBoundsException
int[] numbers = {1, 2, 3};
int number = numbers[5]; // Invalid index
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexOutOfBoundsException caught: " + [Link]());
}
try
{
// NumberFormatException
String invalidNumber = "abc";
int parsedNumber = [Link](invalidNumber); // Invalid parsing
}
catch(NumberFormatException e)
{
[Link]("NumberFormatException caught: " + [Link]());
}
try
{
// NullPointerException
String nullString = null;
[Link]([Link]()); // Accessing method on null
}
catch(NullPointerException e)
{
[Link]("NullPointerException caught: " + [Link]());
}
[Link]("Program completed successfully.");
}
// end of method
}
// end of class
VARIABLE DESCRIPTION CHART:
OUTPUT: