Computer Practical Project
Computer Practical Project
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
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
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.
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
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.
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.
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
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();
StringTokenizer st=new StringTokenizer(s);
int l=st.countTokens();
String v[]={"a","e","i","o","u"};
System.out.println("Word(s) that starts and ends with vowel are :- ");
for(int i=0;i<l;i++)
{
String w=st.nextToken();
String w1=w;
w=w.toLowerCase();
int sa=0,en=0;
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)
{
System.out.println(w1);
}
}
}
}
VARIABLE LIST
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();
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.equals(w))
{
count++;
}
}
System.out.println("The total number of palindromic string in the given
string is "+count);
}
}
VARIABLE LIST
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
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.
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
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
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