You are on page 1of 20

Q1.

Write a program in Java to accept 10 city names in a


single dimensional array, rearrange the city name in the array
using Bubble Sort technique.

A. import java.util.*;
public class city
{
public static void main()
{
Scanner sc=new Scanner (System.in);
String a[]=new String[10];
for (int i=0;i<=9;i++) //Accept and store the names of the
cities in an array
{
System.out.print ("Enter the name of the city: ");
a[i]=sc.nextLine();
}
for (int j=0;j<10;j++) //Bubble Sort Technique
{
for (int i=j+1;i<10;i++)
{
if (a[i].compareTo(a[j]) < 0)
{
String t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
1 | Computer Practical Work
System.out.println();
System.out.println ("The cities in ascending order are: ");
System.out.println ();
for (int i=0;i<=9;i++) //Printing the sorted array
{
System.out.println (a[i]);
}
}
}

2 | Computer Practical Work


Q2. A class “sentence” has been defined with the following
specifications:

Class name: sentence

Data Members:
String sent : To store a sentence
int len : To sore the length of ‘sent’

Member functions:
sentence(): Default constructor to initialize the data
xxxxxxxxxxxxximembers with legal variables

void read(): to accept a sentence and store it in ‘sent’

String reverse(String x): to return the reverse of the word


xxxxxxxxxxxxxxxxxxxxxxxxx‘x’

void display(): to display the original sentence and the


xxxxxxxxxxxxxxxxsentence carrying the reverse of each word
xxxxxxxxxxxxxxxxof it
Write a program in Java to perform the above mentioned
tasks using object creation.

A. import java.util.*;
public class sentence
{
String sent;
int len;
3 | Computer Practical Work
public sentence() //Constructor to initialize the variables to
their default value
{
sent="";
len=0;
}
public void read() //This function accepts and stores a string in
the variable 'sent' and also finds its length
{
Scanner sc=new Scanner (System.in);
System.out.print ("Enter a string: ");
sent=sc.nextLine();
len=sent.length();
}
public String reverse(String x) //This function accepts a word
and returns it in its reversed form
{
String s="";
for (int i=x.length()-1;i>=0;i--)
{
char ch=x.charAt(i);
s=s+ch;
}
return s;
}
public void display()
{
System.out.println ();
System.out.println ("Original String: "+sent);
4 | Computer Practical Work
System.out.print ("Reversed String: ");
String w[]=new String[100];
int c=0;
String str="";
for (int i=0;i<len;i++) //This part breaks the string into its
constituent words
{
char ch=sent.charAt(i);
if (ch==' ')
{
w[c]=str;
c++;
str="";
}
else
if (Character.isLetter(ch)==true)
str=str+ch;
else
str=ch+str;
}
w[c]=str;
for (int i=c;i>=0;i--) //This part prints the reversed words
{
String s=reverse(w[i]);
System.out.print (s+" ");
}
}
public void main() //Main method to call the secondary
methods
5 | Computer Practical Work
{
sentence ob=new sentence();
ob.read();
ob.display();
}
}

6 | Computer Practical Work


Q3. Write a program in Java to accept a text from the user
and encrypt each wordof it as follows:

Sample input: Hello! How are you? I am fine.


Output: Khoor! Krz dug brx? L dp ilqh.

A. import java.util.*;
public class encrypt
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print ("Enter the text to be encryted: ");
String s=sc.nextLine();
String str="";
for (int i=0;i<s.length();i++) //Main loop to control the
encryption of the sentence
{
char ch=s.charAt(i);
if (Character.isLetter(ch)==true)//Progressing further only
if the character is a letter
{
if ((int)ch==88) //Special condition
ch=(char)65;
else if ((int)ch==89) //Special condition
ch=(char)66;
else if ((int)ch==90) //Special condition
ch=(char)67;
else if ((int)ch==120) //Special condition
7 | Computer Practical Work
ch=(char)97;
else if ((int)ch==121) //Special condition
ch=(char)98;
else if ((int)ch==122) //Special condition
ch=(char)99;
else //Regular condition
ch=(char)(ch+3);
}
str=str+ch; //Storing the encrypted text
}
System.out.print ("The encrypted text is: "+str);
}
}

8 | Computer Practical Work


Q4. Write a menu-driven program to accepta number from
the user and check the following on user’s choice.

1. Checking of palinprime number


2. Checking of Armstrong number

A. import java.util.*;
public class number
{
public void arm(int n)//To check if the no. is an armstrong one
{
int s=0;
int c=0;
int m=n;
while (m!=0)
{
int d=m%10;
m=m/10;
s=s+(int)(Math.pow(d,3));
}
if (s==n)
System.out.println ("The number "+n+" is an Armstrong
number.");
else
System.out.println ("The number "+n+" is not an Armstrong
number.");
}
public void palin(int n)//To check if the no. is palindromic
{
9 | Computer Practical Work
int s=0;
int m=n;
while (m!=0)
{
int d=m%10;
m=m/10;
s=(s*10)+d;
}
if (s==n)
System.out.println ("The number "+n+" is a Palprime
number.");
else
System.out.println ("The number "+n+" is prime but NOT a
Palprime number.");
}
public boolean prime(int n)//To check if the number is prime
{
int c=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
if (c==2)
return true;
else
return false;
}
public void main()//Main method
10 | C o m p u t e r P r a c t i c a l W o r k
{
Scanner sc=new Scanner (System.in);
boolean chk=true;
while (chk==true)
{
System.out.println ("1. Check for an Armstrong number:
");
System.out.println ("2. Check for a Palindrome number: ");
System.out.println ("3. Exit the program");
System.out.print ("Enter your choice: ");//Taking the
user's choice
int ch=sc.nextInt();
if (ch==3)//Checking if the user wants to end the program
{
System.out.print ("Thank you for using this software.");
chk=false;
continue;
}
System.out.print ("Enter a number: ");
int n=sc.nextInt();
switch (ch)//Progressing according to the choice of the
user
{
case 1:arm(n);//calling the arm() function
break;
case 2:boolean c=prime(n);//calling the prime() function
if (c==true)
palin(n);//calling the palin() function
else
11 | C o m p u t e r P r a c t i c a l W o r k
System.out.print (" The number "+n+" is not prime
and NOT Palprime.");
break;
default: System.out.println ("You entered a wrong
choice.");
}
System.out.println();
}
}
}

12 | C o m p u t e r P r a c t i c a l W o r k
Q5. Write a menu-driven program to find the volume of the
following geometrical shapes using function overloading
approach:

 Volume of cube = x3, where x is the side of the cube


 Volume of cuboid = lxbxh ; l,b,h are the length, breadth
xxxxxxxxxxxxxxxxxxxxxxxxiand height
𝟏
 Volume of cylinder = 𝝅𝒓𝒉 , r is the radius and h is the
𝟑
xxxxxxxxxxxxxxxxxxxxxxxxxxheight

A. import java.util.*;
public class volume
{
public double vol(double x)//Volume of cube
{
return (Math.pow(x,3));
}
public double vol(double l,double b,double h)//Volume of
cuboid
{
return (l*b*h);
}
public double vol(double r,double h)//Volume of cylinder
{
return ((3.14*r*r*h)/3);
}
public void main()
{
13 | C o m p u t e r P r a c t i c a l W o r k
Scanner sc=new Scanner(System.in);
boolean chk=true;
while(chk==true)
{
System.out.println ("1. Find volume of cube: ");
System.out.println ("2. Find volume of cuboid: ");
System.out.println ("3. Find volume of cylinder: ");
System.out.println ("4. Exit: ");
System.out.print ("Enter your choice: ");
int ch=sc.nextInt();
System.out.println();
switch (ch)//Progressing according to the choice of the
user
{
case 1: System.out.print ("Enter the length of one side:
");
double x=sc.nextDouble();
double cube=vol(x);
System.out.println ("The volume of the cube is:
"+cube+" sq. units");
System.out.println ();
break;
case 2: System.out.print ("Enter the length: ");
double l=sc.nextDouble();
System.out.print ("Enter the breadth: ");
double b=sc.nextDouble();
System.out.print ("Enter the height: ");
double h=sc.nextDouble();
double cuboid=vol(l,b,h);
14 | C o m p u t e r P r a c t i c a l W o r k
System.out.println ("The volume of the cuboid is:
"+cuboid+" sq. units");
System.out.println ();
break;
case 3: System.out.print ("Enter the radius: ");
double r=sc.nextDouble();
System.out.print ("Enter the height: ");
double k=sc.nextDouble();
double cylin=vol(r,k);
System.out.println ("The volume of the cylinder is:
"+cylin+" sq. units");
System.out.println ();
break;
case 4: System.out.print ("Thank you for using this
software.");
chk=false;
break;
default:System.out.println ("You entered a wrong
choice. Please try again.");
}
}
}
}

15 | C o m p u t e r P r a c t i c a l W o r k
Q6. Write a program in Java with the following class
specifications:

Class name: Magic

Data Members: n – to store an integer

Member Functions:
Magic(): Default constructor to initialize the data members
xxxxxxxxwith legal initial values

int Sum_of_digits(int x): returns the sum of digits of ‘x’

void check() : to checkwhether the no. is magic or not by


calling the function Sum_of_digits()

A. import java.util.*;
public class magic
{
int n;
public magic()
{
n=0;
}
public int Sum_of_digits(int n)
{
int s=0;
while (n!=0)
{
16 | C o m p u t e r P r a c t i c a l W o r k
int d=n%10;
s+=d;
n=n/10;
}
return s;
}
public void check()
{
magic ob=new magic();
int s=ob.Sum_of_digits(n);
while (s>=10)
{
s=ob.Sum_of_digits(s);
}
if (s==1)
System.out.println ("The number "+n+" is a Magic
number.");
else
System.out.println ("The number "+n+" is NOT a Magic
number.");
}
public void main()
{
Scanner sc=new Scanner (System.in);
System.out.print ("Enter a number: ");
n=sc.nextInt();
check();
}
}
17 | C o m p u t e r P r a c t i c a l W o r k
Q7. Write a program in Java to enter a text which may
contain upto 100 semtemces where each sentence ends with
‘?’ or ‘,’ or ‘.’ Or ‘!’. Rearrange the sentences of the text in
ascending order on the basis of the no. of words present in it.

A. import java.util.*;
public class arrange
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print ("Enter your text: ");
String text=sc.nextLine();
String s[]=new String[100];
int l[]=new int [100];
String str="";
int c=0;
for (int i=0;i<text.length();i++) //Breaking the text into
senteces
{
char ch=text.charAt(i);

18 | C o m p u t e r P r a c t i c a l W o r k
if (ch=='!'||ch=='.'||ch=='?'||ch==',') //Defining when to
break into a string
{
s[c]=str.trim();
l[c]=str.trim().length();
c++;
str="";
}
else
str=str+ch;
}
s[c]=str.trim();
l[c]=str.trim().length();
for (int i=0;i<c;i++) //Sorting the senteces according to
number of words
{
for (int j=0;j<c-i;j++)
{
if (l[j]>l[j+1])
{
int t=l[j+1]; //Arranging the length of the sentences in
ascending order
l[j+1]=l[j];
l[j]=t;
String temp=s[j+1]; //Substituting the senteces
according to their lengths
s[j+1]=s[j];
s[j]=temp;
}
19 | C o m p u t e r P r a c t i c a l W o r k
}
}
System.out.println ("The sentences in sorted order from
least no. of words to most are: ");
for (int i=0;i<=c;i++)
System.out.println (s[i]);
}
}

20 | C o m p u t e r P r a c t i c a l W o r k

You might also like