Computer Practical Project Work
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
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
§ 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.
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
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
Output :
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
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
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.
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
Output :-
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
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
Output :-