0% found this document useful (0 votes)
97 views

Computer Practical Project Work

The document contains solutions to multiple Java programming problems involving string manipulation. The first solution counts the number of vowels in each word of a given sentence. The second generates Fibonacci strings up to a given number of terms by concatenating the previous two strings. The third checks if a given sentence is valid and converts any non-palindrome words to palindromes by concatenating the word with its reverse. The last displays any words from a given string that start and end with a vowel.

Uploaded by

Soma Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Computer Practical Project Work

The document contains solutions to multiple Java programming problems involving string manipulation. The first solution counts the number of vowels in each word of a given sentence. The second generates Fibonacci strings up to a given number of terms by concatenating the previous two strings. The third checks if a given sentence is valid and converts any non-palindrome words to palindromes by concatenating the word with its reverse. The last displays any words from a given string that start and end with a vowel.

Uploaded by

Soma Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

COMPUTER PRACTICAL PROJECT WORK

Q, WAP in java to input a sentence and count the number of vowels in each
word.

Soln. :-

import java.util.*;
class word_vowel_count
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s);//String Tokenizer class
int l=st.countTokens();//counts the number of words
System.out.println("Words\t\t\tNo. of Vowels");
for(int i=0;i<l;i++)
{
String w=st.nextToken();//store words individually
String w1=w;
w=w.toLowerCase();
int count=0;
for(int j=0;j<w.length();j++)
{
char c=w.charAt(j);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
count++;
}
}
System.out.println(w1+"\t\t\t"+count);
}
}
}
VARIABLE LIST

Variable Name Data Type Description


s String Stores the string entered by the user
l int Stores the number of words present in
String Tokenizer class
i int Stores the loop variable
j int Stores the loop variable
w String Stores the word of String Tokenizer
w1 String Stores the value of ‘w’ variable
count int Counts the no. of vowels in each word
c char Stores the character of each word
Output :-

Q. A sequence of Fibonacci strings is generated as follows:


S0 = “a”, SF = “b”, Sn = S(n-1) + S(n-2) where ‘+’ denotes concatenation.
Thus the sequence is :-
a, b, ba, bab, babba, babbabab,....... n terms.
Design a class FiboString to generate Fibonacci strings. Some of the
members
of the class are given below:
Class name: FiboString
Data members/instance variables:
• x: to store the first string
• y: to store the second string
• z: to store the concatenation of the previous two strings
• n: to store the number of terms
Member functions/methods:
• FiboString(): Constructor to assign x=”a”, y=”b” and z=”ba”
• void accept(): To accept the number of terms ‘n’
• void generate(): To generate and print the Fibonacci strings. The sum
of (‘+’ i.e. concatenation) first two strings is the third string. Eg. “a” is
first string, “b” is second string then the third will be “ba”, and fourth
will be “bab” and so on.

Specify the class FiboString, giving details of the constructor(), void


accept() and void generate(). Define the main() function to create an object
and call the functions accordingly to enable the task.
Soln:-

import java.util.Scanner;
class FiboString
{
Scanner sc=new Scanner(System.in);
String x,y,z;
int n;
FiboString() //Declaration of the Default Constructor
{
x="a";
y="b";
z="ba";
}
void accept()
{
System.out.println("Enter number of terms");
n=sc.nextInt();
}
void generate()
{
System.out.print(x+","+y);
for(int i=0;i<=n-3;i++)
{
System.out.print(","+z);
x=y;
y=z;
z=y+x;
}
}
void main() //Declaration of the main() method
{
FiboString ob=new FiboString();
ob.accept();
ob.generate();
}
}
VARIABLE LIST

Variable Name Data Type Description


x String Stores the value ‘a’
y String Stores the value ‘b’
z String Stores the value ‘ba’
n int Stores the number of terms t be
displayed entered by the user
i Int Stores the loop variable
Output :-

Q. Write a program to accept a sentence which may be terminated by either


‘.’, ‘?’ or ‘!’ only. The words are to be separated by a single blank space and
are in uppercase.
Perform the following tasks:

• Check for the validity of the accepted sentence.

•Convert the non-palindrome words of the sentence into palindrome


words by concatenating the word by its reverse (excluding the last
character).
Example:

§ The reverse of the word HELP would be LEH (omitting the last
alphabet) and by concatenating both, the new palindrome word is
HELPLEH. Thus, the word HELP becomes HELPLEH.

§ Note: The words which end with repeated alphabets, for example
ABB would become ABBA and not ABBBA and XAZZZ becomes
XAZZZAX.

§ [Palindrome word: Spells same from either side. Example: DAD,


MADAM etc.]

§ Display the original sentence along with the converted sentence


Soln. :-

import java.util.*;
class word_palin_converter
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
s=s.toUpperCase().trim();
String r="",w1="",p="";
char ch=s.charAt(s.length()-1);
if(ch=='.' || ch=='?' || ch=='!')
{
s=s.substring(0,s.length()-1); //removes .,?,!
StringTokenizer st=new StringTokenizer(s);
int l=st.countTokens();//counts the number of tokens i.e. number of
words
int f=0;
for(int i=0;i<l;i++)
{
String w=st.nextToken();//stores individual words
w1=w;
StringBuffer sb=new StringBuffer(w1);//String Buffer Class
String rev=sb.reverse().toString();//stores the reverse of the word
in String data type
if(rev.equals(w1))
{
f=1;
}
if(f==0)
{
if(w1.endsWith(w1.substring(w1.length()-
1))&&(w1.substring(0,w1.length()-1)).endsWith(w1.substring(w1.length()-1)))
{
w1=w1.substring(0,w1.length()-2);
}
else
{
w1=w1.substring(0,w1.length()-1);
}
for(int j=w1.length()-1;j>=0;j--)
{
char c=w1.charAt(j);
r=r+c;
}
}
p=p+w.concat(r)+" ";
r="";
f=0;
}
System.out.println("The Original Sentence entered by the user :-
\n"+s);
System.out.println("The Sentence converted to Palindrom String is :-
\n"+p);
}
else
{
System.out.println("INVALID INPUT !!!!!!!!!!");
}
}
}

VARIABLE LIST

Variable Name Data Type Description


s String Stores the string entered by user
l int Stores the number of words present
in String Tokenizer class
w String Stores the word present in String
Tokenizer class
w1 String Stores the value of variable ‘w’
rev String Stores the reverse of variable ‘w’
with the help of String Buffer Class
ch char Stores the last character of the
string entered by user to determine
the validity of the string
r String Stores the reverse of ‘w’ excluding
the last character pf ‘w’
f int Acts as a flag variable
i int Stores the loop variable
p String Stores the palindrome of non-
palindrome words
Output :-

Q. WAP in java to input a string and display the word where the word starts
and ends with vowel.

Soln. :-

import java.util.*;
class display_word_start_end_with_vowel
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.nextLine();
char ch=s.charAt(s.length()-1);
if(ch=='!'||ch=='.'||ch=='?')
{
s=s.substring(0,s.length()-1);
StringTokenizer st=new StringTokenizer(s);
int l=st.countTokens(),sa=0,en=0,c=0;
String v[]={"a","e","i","o","u"},w2[]=new String[l];
for(int i=0;i<l;i++)
{
String w=st.nextToken();
String w1=w;
w=w.toLowerCase();
for(int j=0;j<v.length;j++)
{
if(w.startsWith(v[j]))
{
sa=1;
}
if(w.endsWith(v[j]))
{
en=1;
}
}
if(sa==1 && en==1)
{
w2[c]=w1;
c++;
}
sa=0;
en=0;
}
if(w2[0]!=null)
{
System.out.println("Word(s) that starts and ends with vowel are :-
");
for(int i=0;i<c;i++)
{
System.out.println(w2[i]);
}
}
else
{
System.out.println("The entered sentence does not contain any
word that starts and ends with vowel !!!!!!!!");
}
}
else
{
System.out.println("INVALID INPUT !!!!!!!!!! PLEASE END THE
SENTENCE WITH VALID PUNCTUATION !!!!!!");
}
}
}
VARIABLE LIST

Variable Name Data Type Description


s String Stores the string entered by the user
ch char Stores the last character of the string
entered by user to determine the validity of
the string
l int Stores the number of words present in
String Tokenizer class
v[] char Array that stores all the vowels
i int Stores the loop variable
w String Stores the word present in String
Tokenizer class
w1 String Stores the value of ‘w’ variable
w2[] String Array to store the word(s) starting and
ending with vowels
sa int Stores ‘1’ if ‘w’ starts with a vowel else
stores ‘0’
en int Stores ‘1’ if ‘w’ ends with a vowel else stores
‘0’
j int Stores the loop variable
c int Used to store the index of array w2[]

Output :

Q. WAP in java to input a sentence and count the number of palindromic


words in the sentence with String Buffer.

Soln. :-

import java.util.*;
class str_palin_buffer
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
char ch=s.charAt(s.length()-1);
if(ch=='.'||ch=='!'||ch=='?')
{
s=s.substring(0,s.length()-1);
}
StringTokenizer st=new StringTokenizer(s);
int l=st.countTokens();
int count=0;
for(int i=0;i<l;i++)
{
String w=st.nextToken();
StringBuffer sb=new StringBuffer(w);
String rev=sb.reverse().toString();
if(rev.equalsIgnoreCase(w))
{
count++;
}
}
if(count!=0)
{
System.out.println("The total number of palindromic string in the
given string is "+count);
System.out.println();
}
else
{
System.out.println("The entered sentence does not contain any
palindromic words !!!!!!!");
System.out.println();
}
}
}
VARIABLE LIST

Variable Name Data Type Description


s String Stores the string entered by the user
l int Stores the number of words present in
String Tokenizer class
count int Stores the number of palindromic word
present in the given string
ch char Stores the last character of the string
entered by user to determine the validity of
the string
i int Stores the loop variable
w String Stores the word present in String Tokenizer
class
rev String Stores the reverse of variable ‘w’ with the
help of String Buffer Class
Output :-

Q. The potential of a word is found by adding the ASCII value of the


alphabets.
(ASCII values of A to Z are 65 to 90).
Example: BALL
Potential = 66 + 65 + 76 + 76 = 283
Write a program to accept a sentence which may be terminated by either “ .
” , “ ? ” or “ ! ” only.
The words of sentence are separated by single blank space and are in
UPPER CASE. Decode the
words according to their potential and arrange them in ascending order of
their potential strength.
Test your program with the following data and some random data :-

Example 1:
INPUT: HOW DO YOU DO?
OUTPUT:
HOW = 238
DO = 147
YOU = 253
DO = 147
DO DO HOW YOU

Example 2:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT:
LOOK = 309
BEFORE = 435
YOU = 253
LEAP = 290
YOU LEAP LOOK BEFORE

Example 3:
INPUT: HOW ARE YOU#
OUTPUT: INVALID INPUT

Soln. :-

import java.util.*;
class str_potential_ascending
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
char ch=s.charAt(s.length()-1);
if(ch=='.'||ch=='?'||ch=='!')
{
s=s.substring(0,s.length()-1).trim().toUpperCase();
StringTokenizer st=new StringTokenizer(s);
int l=st.countTokens();
int p=0,p1[]=new int[l],t1=0;
String t="",s1[]=new String[l],asc_pot="";
for(int i=0;i<l;i++)
{
String w=st.nextToken();
for(int j=0;j<w.length();j++)
{
char c=w.charAt(j);
p=p+c;
}
p1[i]=p;
s1[i]=w;
p=0;
}
for(int i=0;i<l-1;i++)
{
for(int j=0;j<l-i-1;j++)
{
if(p1[j]>p1[j+1])
{
t=s1[j];
t1=p1[j];
s1[j]=s1[j+1];
p1[j]=p1[j+1];
s1[j+1]=t;
p1[j+1]=t1;
}
}
}
for(int i=0;i<l;i++)
{
asc_pot=asc_pot+s1[i]+" ";
}
System.out.println("The original string is :- \n"+s);
System.out.println("The string arranged in ascending order of their
potential of a word is :- \n"+asc_pot);
System.out.println();
}
else
{
System.out.println("INVALID INPUT!!!!!!!!");
}
}
}
VARIABLE LIST

Variable Name Data Type Description


s String Stores the string entered by the user
ch char Stores the last character of the string
entered by user to determine the validity
of the string
l int Stores the number of words present in
String Tokenizer class
p int Stores the potential each word i.e. the
sum of ASCII value of each character of
the word
p1[] String Array to store the potential of all the
words of the given string
t String To temporarily store the value a string for
bubble sorting.
t1 int To temporarily store the value the
potential value of each word for bubble
sorting
s1[] String Array to store all the words of the given
string
asc_pot String Store the words arranged in ascending
order of their potential.
c char Stores the characters of each word
i int Stores the loop variable
w String Stores the word present in String
Tokenizer class
j int Stores the loop variable

Output :-
Q. WAP in java to accept a sentence which may be terminated by either ‘.’, ‘?’
or ‘!’ only. The words are to be separated by a single blank space and are in
UPPER CASE.

Perform the following tasks :-


§ Check for the validity of the accepted sentence only for the
terminating character.
§ Arrange the words in ascending order of their length. If two or more
words have the same length, then sort them alphabetically.
§ Display the original sentence along with the converted sentence.
Test your program for the following data and some random data.

Example 1 :-
INPUT :- SELF HELP IS THE BEST HELP.
OUTPUT :-
SELF HELP IS THE BST HELP.
IS THE BEST HELP HELP SELF

EXAMPLE 2 :-
INPUT :- BE KIND TO OTHERS.
OUTPUT :-
BE KIND TO OTHERS.
BE TO KIND OTHERS

Example 3 :-
INPUT :- NOTHING IS IMPOSSIBLE#
OUTPUT :- INVALID INPUT

Soln :-

import java.util.*;
class str_word_length_ascending
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s1=sc.nextLine();
char ch=s1.charAt(s1.length()-1);
if(ch=='.'||ch=='?'||ch=='!')
{
s1=s1.substring(0,s1.length()-1).trim().toUpperCase();
StringTokenizer st=new StringTokenizer(s1);
int l=st.countTokens();
String s[]=new String[l];
String t="",asc_str="";
for(int i=0;i<l;i++)
{
String w=st.nextToken();
s[i]=w;
}
for(int i=0;i<l-1;i++)
{
for(int j=0;j<l-i-1;j++)
{
if(s[j].length()>s[j+1].length())
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
if(s[j].length()==s[j+1].length())
{
if(s[j].compareTo(s[j+1])>0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
}
for(int i=0;i<l;i++)
{
asc_str=asc_str+s[i]+" ";
}
System.out.println("The Original String entered by the user :-
\n"+s1);
System.out.println("The String arranged in ascending order of its
word length is :- \n"+asc_str);
}
else
{
System.out.println("INVALID INPUT !!!!!!!!");
}
}
}
VARIABLE LIST

Variable List Data Type Description


s1 String Store the string entered by the user

ch char Stores the last character of the string


entered by user to determine the validity of
the string
l int Stores the number of words present in
String Tokenizer class
s[] String Array to store all the words of the given
string
t String To temporarily store the value a string for
bubble sorting.
asc_str String Store the words arranged in ascending
order of their length.
i int Stores the loop variable
w String Stores the word present in String Tokenizer
class
j int Stores the loop variable

Output :-

Q. A Goldbach number is a positive even integer that can be expressed as


the sum of two odd primes.
Note :- All even integer numbers greater than 4 are Goldbach numbers.

Example :-
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime
pairs, i.e. 3 and 7, 5 and 5.
Write a program to accept an even integer 'N' where N > 9 and N < 50. Find
all the odd prime pairs whose sum is equal to the number 'N'.
Test your program with the following data and some random data.

Example 1 :-
INPUT :-
N = 14
OUTPUT :-
PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT :-
N = 30
OUTPUT :-
PRIME PAIRS ARE:
7, 23
11, 19
13, 17

Soln. :-

import java.util.Scanner;
class Goldbach_Number
{
boolean prime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
return true;
else
return false;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Goldbach_Number ob=new Goldbach_Number();
System.out.println("Enter a number greater than 9 but less than 50
!!!!!!!");
int gld_bch=sc.nextInt();
if(gld_bch>9 && gld_bch<50)
{
System.out.println("The Odd Prime Pair(s) of the number
'"+gld_bch+"' is(are) :-");
for(int i=gld_bch;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
if(i+j==gld_bch && ob.prime(i)==true && ob.prime(j)==true)
{
System.out.println(j+"\t"+i);
}
}
}
}
else
{
System.out.println("INVALID INPUT !!!!!!!\nNEXT TIME TRY TO
ENTER A NUMBER GREATER THAN 9 BUT LESS THAN 50 !!!!!!!!");
}
}
}
VARIABLE LIST

Variable Name Data Type Description


n int Stores the parameterized input of the
function ‘prime()’
c int Acts as a counter variable to check the
validity of a prime number
gld_bch int Stores the number (basically Goldbach
Number) entered by the user
i int Stores the loop variable
j int Stores the loop variable
Output :-
Q. Write a program to declare a single-dimensional array a[] and a square
matrix b[][] of size N, where N > 2 and N < 10. Allow the user to input
positive integers into the single dimensional array.

Perform the following tasks on the matrix :-

§ Sort the elements of the single-dimensional array in ascending


order using any standard sorting technique and display the sorted
elements.
§ Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:

1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5

§ Display the filled matrix in the above format.

Soln. :-

import java.util.Scanner;
class matrix_arrangement
{
int[] array_sort(int a[])
{
int l=a.length;
for (int i=0; i<l-1;i++)
{
for (int j=0;j<l-i-1;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
matrix_arrangement ob=new matrix_arrangement();
System.out.println("Enter the number of terms which is greater than 2
but less than 10");
int n=sc.nextInt();
if(n>2 && n<10)
{
int ar[]=new int[n],sq[][]=new int[n][n];
System.out.println("Enter "+n+" integer elements for the Single-
Dimensional Array");
for(int i=0;i<n;i++)
{
ar[i]=sc.nextInt();
}
System.out.println("The Single-Dimensional Array before sorting is
as follows :- ");
for(int i=0;i<n;i++)
{
System.out.print(ar[i]+"\t");
}
System.out.println();
ar=ob.array_sort(ar);
System.out.println("The Single-Dimensional Array after sorting is as
follows :- ");
for(int i=0;i<n;i++)
{
System.out.print(ar[i]+"\t");
}
System.out.println();
for(int i=n-1,r=0;i>=0;i--,r++)
{
for(int c=0;c<=i;c++)
{
sq[r][c]=ar[c];
}
for(int c1=n-1;c1>i;c1--)
{
sq[r][c1]=ar[c1-i-1];
}
}
System.out.println("The filed square matrix in the mentioned format
is as follows :- ");
for(int r=0;r<n;r++)
{
for(int c=0;c<n;c++)
{
System.out.print(sq[r][c]+"\t");
}
System.out.println();
}
System.out.println();
}
else
{
System.out.println("INVALID INPUT !!!!!!! NUMBER OF TERMS OF
MATRIX IS OUT OF MENTIONED RANGE");
}
}
}

VARIABLE LIST

Variable Name Data Type Description


l int Stores the length of array in the
parameterized input of function
array_sort(int a[])
a[] int Parameterized array to store the array ad
return the array sorted in ascending order.
i int Stores the loop variable
j int Stores the loop variable
t int To temporarily store the value a string for
bubble sorting.
n int Stores the number of terms of the matrix
entered by the user
ar[] int Stores single-dimensional array of size ‘n’
sq[][] int Stores the double-dimensional squared
array of dimension ‘n x n’
r int Stores the loop variable
c int Stores the loop variable
c1 int Stores the loop variable

Output :-
Q. A company manufactures packing cartons in four sizes, i.e. cartons to
accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program
to accept the number of boxes to be packed (N) by the user (maximum up to
1000 boxes) and display the break-up of the cartons used in descending
order of capacity (i.e. preference should be given to the highest capacity
available, and if boxes left are less than 6, an extra carton of capacity 6
should be used.)

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

Example 1

INPUT:
N = 726

OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16

Example 2

INPUT:
N = 140

OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3

INPUT:
N = 4296

OUTPUT:
INVALID INPUT

Soln. :-

import java.util.Scanner;
class Boxes
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of boxes which must be less
than 1000 but greater than 1");
int n = in.nextInt();
if (n>=1 && n<=1000)
{
int Sizes[] = {48, 24, 12, 6};
int total = 0;
int t = n;
for (int i = 0; i < Sizes.length; i++)
{
int cartonCount = t/Sizes[i];
t = t%Sizes[i];
total+= cartonCount;
if (cartonCount != 0)
{
System.out.println(Sizes[i] + " * " + cartonCount + " = " +
(Sizes[i] * cartonCount));
}
}
if (t != 0)
{
System.out.println("Remaining boxes = "+t);
total++;
}
else
{
System.out.println("Remaining boxes = 0");
}
System.out.println("Total number of boxes = " + n);
System.out.println("Total number of cartons = " + total);
System.out.println();
}
else
{
System.out.println("INVALID INPUT !!!!!!\nNUMBER OF BOXES IS
OUT OF MENTIONED RANGE");
}
}
}
VARIABLE LIST

Variable Name Data Type Description


n int Stores the number of boxes to be packed
entered by the user
Sizes[] int Stores the four different sizes of cartons
i.e. 48,24,12,6.
total int Stores the total number of cartons
t int Stores the number of remaining boxes
i int Stores the loop variable
cartonCount int Stores the number of cartons and adds
up to give the total number of cartons.

Output :-

You might also like