CMPTR PRJCT

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

1. Write a program to take 10 cities names in an array and their respective capitals in another array.

Sort the cities using


bubble sort technique. Display the sorted list of cities and their respective capitals.

Algorithm:
Step 1 – Declare and Initialize required variables such as x[], y[].
Step 2 – Start loop as, for i from 0 to less than 10 and input strings in the matrix at x[i] and y[i] and convert them to
Upper Case;
Step 3 – Declare and Initialize required variables such as temp,temp1 .
Step 4 – Using the bubble sort technique to arrange elements of x[] and y[] in ascending order.
Step 5 - Start loop as, for i from 0 to less than 10 and display the two arrays x[] and y[].

Code:
import java.util.*;
class Sort{
public static void main(){
Scanner sc=new Scanner(System.in);
String x[]=new String[10];
String y[]=new String[10];
System.out.println("Enter city name along with their respective capitals");
for(int i=0;i<10;i++){
x[i]=sc.nextLine().toUpperCase();
y[i]=sc.nextLine().toUpperCase();
}
String temp="",temp1="";
for(int i=0;i<10;i++){
for(int j=0;j<10-i-1;j++){
if(x[j].compareTo(x[j+1])>0){
temp=x[j];
temp1=y[j];
x[j]=x[j+1];
y[j]=y[j+1];
x[j+1]=temp;
y[j+1]=temp1;
}
}
}
System.out.println("Cities"+" "+"Capitals");
for(int i=0;i<10;i++){
System.out.println(x[i]+" "+y[i]);
}
}
}
Variable Description Table:
Variables Data Types Purpose
x[] String to input and store the elements in array.
y[] String to input and store the elements in array.
temp String used as backup.
temp1 String used as backup.
i int loop control variable.
j int loop control variable.

Output:
Input:

Output:
2. Design a class 'Add_Dist' with the following specifications:
class Add_Dist
Data members/Instance variables
int km, mts
Member methods:
Add_Dist(): to assign default values to data members.
void get_dist( ) : to accept a distance in km and mts .
Add_Dist calculate(Add_Dist d1, Add_Dist d2):to add both the distances using the object d1 and d2 , where 1
km = 1000 mts
void show_dist( ) : to display the distance in km, mts.
Write a main() to create an object of class 'Add_Dist' and call the member methods to display the input
distance and the resultant distance.

Algorithm:
Step 1 – Declare the Data members/Instance variables such as km,mts.
Step 2 – Create a default constructor and initialize with default values to data members.
Step 3 – Create member method such as void get_dist() to accept a distance in kilometres and metres.
Step 4 – Create another member method such as Add_Dist calculate(Add_Dist d1, Add_Dist d2) to add both
the distances using the object d1 and d2 , where 1 km = 1000 mts.
Step 5 – Create void show_dist( ) to display the distance in km, mts.
Step 6 – Create a main() method to create an to create an object of class 'Add_Dist' and call the member methods to
display the input distance and the resultant distance.

Code:
import java.util.*;
class Add_Dist{
int km,mts;
Add_Dist(){
km=0;
mts=0;
}
void get_dist(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter kilometres and metres");
km=sc.nextInt();
mts=sc.nextInt();
}
Add_Dist calculate(Add_Dist d1, Add_Dist d2){
this.km=d1.km+d2.km;
this.mts=d1.mts+d2.mts;
this.km+=this.mts/1000;
this.mts=mts%1000;
return this;
}
void show_dist( ){
System.out.println(km+" "+mts);
}
public static void main(){
Add_Dist d1=new Add_Dist();
Add_Dist d2=new Add_Dist();
d1.get_dist();
d2.get_dist();
Add_Dist d3=new Add_Dist();
d3.calculate(d1,d2);
d1.show_dist();
d2.show_dist();
d3.show_dist();
}
}

Variable Description Table:


Variables Data Types Purpose
km int to input and store the value in kilometers.
mts int to input and store the value in metres.

Output:

3. Write a program in Java to accept two strings. Display the new string by taking each character of the first string from
left to right and of the second string from right to left. The letters should be taken alternatively from each string. Assume
that the length of both the strings are the same.
Sample Input:
String 1: HISTORY
String 2: SCIENCE
Sample Output:
HEICSNTEOIRCYS

Algorithm:
Step 1 – Declare and Initialize required variables such as strings x, y.
Step 2 – Input two strings in variables x and y and convert them to Upper Case;
Step 3 – Start loop as, for i from 0 to less than the length of the string x(Assuming that the length of both the strings
are the same) and display the resultant string.

Code:
import java.util.*;
class Same{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 Strings");
String x=sc.next().toUpperCase();
String y=sc.next().toUpperCase();
for(int i=0;i<x.length();i++){
System.out.print(x.charAt(i)+""+y.charAt(y.length()-1-i));
}
}
}

Variable Description Table:


Variables Data Types Purpose
x String to input and store the string.
y String to input and store the string.
i int loop control variable.

Output:

4. Input a single sentence which terminates with a full stop (.). The words should be in lower case. Arrange the words
contained in the sentence according to the length of the words in ascending order. If two words are of the same length
then the word occurring first in the input sentence should come first. For both, input and output the sentence must
begin in upper case.
Test your program for given data and also some random data:
Input:
The lines are printed in reverse order.
Output:
In the are lines order printed reverse.
Input:
Print the sentence in ascending order.
Output:
In the print order sentence ascending.
Input:
I love my Country.
Output:
I my love Country.

Algorithm:
Step 1 – Declare and Initialize required variables such as b and t .
Step 2 – Declare a and input and store the sentence and convert it to Lower Case.
Step 3 – Create an object such as ob of String Tokenizer.
Step 4 – Declare and Initialize the Array z[].
Step 5 – Create a while loop and store each word of the sentence in respective places in the array.
Step 5 – Create a while loop and store each word of the sentence in respective places in the array.
Step 6 – Using the bubble sort technique to arrange elements of z[] in ascending order.
Step 7 – Initialize and the create a sentence by storing the elements of the sorted array into another
variable b.
Step 8 – Print the resultant sentence.
Code:
import java.util.*;
class Sentnce{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String a=sc.nextLine().toLowerCase(),b="";
StringTokenizer ob=new StringTokenizer(a,". ");
String z[]=new String[ob.countTokens()];
int t=0;
while(ob.hasMoreTokens()){
z[t++]=ob.nextToken().toLowerCase();
}
for(int i=0;i<z.length;i++){
for(int j=0;j<z.length-1-i;j++){
if(z[j].length()>z[j+1].length()){
b=z[j];
z[j]=z[j+1];
z[j+1]=b;
}
}
}
b="";
for(int i=0;i<z.length;i++){
b+=z[i]+" ";
}
b=b.trim();
System.out.println(Character.toUpperCase(b.charAt(0))+b.substring(1)+".");
}
}

Variable Description Table:


Variables Data Types Purpose
a String to input and store the sentence.
b String to input and store the sentence.
t int used as counter.
z[] String to input and store the each word in array.
i int loop control variable.
j int loop control variable.

Output:
5. Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be
separated by more than one blank space and are in UPPER CASE.
Perform the following tasks:
Find the number of words beginning and ending with a vowel.
Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in
the sentence.
Test your program with the sample data and some random data:
Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT

Algorithm:
Step 1 – Declare and Input a sentence and convert to Upper Case in a variable a.
Step 2 – Checks whether a sentence which may be terminated by either '.', '?' or '!' only.
Step 3 – Create 2 objects such as x, x1 of String Tokenizer.
Step 4 – Declare and Initialize variables such as b and f .
Step 5 – Checks the number of words beginning and ending with a vowel.
Step 6 – Creates a sentence first by adding the words beginning and ending with a vowel and then by adding the rest
of the sentence.
Step 7 – Display both the number of vowels and the resultant sentence.

Code:
import java.util.*;
class UK{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String a=sc.nextLine().toUpperCase();
if(a.charAt(a.length()-1)=='.'||a.charAt(a.length()-1)==':'||a.charAt(a.length()-1)=='?'||a.charAt(a.length()-1)=='!'){
StringTokenizer x=new StringTokenizer(a,".:!? ");
StringTokenizer x1=new StringTokenizer(a,".:!? ");
String b="";
int f=0;
while(x.hasMoreTokens()){
a=x.nextToken();
if("AEIOU".indexOf(a.charAt(0))>=0&&"AEIOU".indexOf(a.charAt(a.length()-1))>=0){
f++;
b+=a+" ";
}
}
while(x1.hasMoreTokens()){
a=x1.nextToken();
if("AEIOU".indexOf(a.charAt(0))<0||"AEIOU".indexOf(a.charAt(a.length()-1))<0){
b+=a+" ";
}
}
System.out.println("No. of words starting and ending with vowel = "+f);
System.out.println(b.trim());
}
else
System.out.println("Invalid Input");
}
}

Variable Description Table:


Variables Data Types Purpose
a String to input and store the sentence.
b String to store the sentence.
f int used as counter.

Output:
6.

Algorithm:
Step 1 – Declare the Data members/Instance variables such as wrd, len.
Step 2 – Create a default constructor and initialize with default values to data members.
Step 3 – Create member method such as void feedword() to accept the word in Upper Case.
Step 4 – Create another member method such as void mix_word(Mix P, Mix Q) to mix the words of objects P
and Q and stores the resultant word in the current object.
Step 5 – Create void display( ) to display the Resultant Word.
Step 6 – Create a main() method to create objects of class 'Mix' and call the functions accordingly.

Code:
import java.util.*;
class Mix{
String wrd;
int len;
Mix(){
wrd="";
len=0;
}
void feedword(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
wrd=sc.next().toUpperCase();
}
void mix_word(Mix P,Mix Q){
int i;
for(i=0;i<P.wrd.length()&&i<Q.wrd.length();i++){
this.wrd+=P.wrd.charAt(i)+""+Q.wrd.charAt(i);
}
if(P.wrd.length()>Q.wrd.length()){
this.wrd+=P.wrd.substring(i);
}
if(P.wrd.length()<Q.wrd.length()){
this.wrd+=Q.wrd.substring(i);
}
}
void display(){
System.out.println(wrd);
}
public static void main(){
Mix P=new Mix();
Mix Q=new Mix();
P.feedword();
Q.feedword();
Mix R=new Mix();
R.mix_word(P,Q);
R.display();
}
}

Variable Description Table:


Variables Data Types Purpose
wrd String to input and store the word.
len int to store the length of the word.
i int loop control variable.

Output:

You might also like