0% found this document useful (0 votes)
100 views44 pages

Computer Project

The document is an assessment sheet for a student named Vanshika Sachar in class 11 section A with roll number 20. It contains the student's details, signatures of examiners, an acknowledgement section thanking various individuals for their support, and an index listing 15 Java programs with their page numbers and remarks. The programs include finding the greatest common divisor of two numbers, checking if a number is a duck number, checking if a number is a disarium number, converting a decimal number to octal, printing Floyd's triangle, and other algorithms.

Uploaded by

Vanshika Sachar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views44 pages

Computer Project

The document is an assessment sheet for a student named Vanshika Sachar in class 11 section A with roll number 20. It contains the student's details, signatures of examiners, an acknowledgement section thanking various individuals for their support, and an index listing 15 Java programs with their page numbers and remarks. The programs include finding the greatest common divisor of two numbers, checking if a number is a duck number, checking if a number is a disarium number, converting a decimal number to octal, printing Floyd's triangle, and other algorithms.

Uploaded by

Vanshika Sachar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

ASSESSMENT SHEET

NAME: VANSHIKA SACHAR

CLASS: XI SECTION: A

ROLL NUMBER: 20

SUBJECT: COMPUTER SCIENCE

SUBJECT TEACHER: MR. SARFARAZ


KHAN

INTERNAL EXAMINER’S SIGN.:

EXTERNAL EXAMINER’S SIGN.:


ACKNOWLEDGEMENT
First and foremost, praises and thanks to the God, the Almighty, for
His showers of blessings throughout my research work to complete
it successfully.
Secondly, I wish to express my deep gratitude and sincere thanks to
the school Principal, Ms. Jyoti Kashyap ma’am for her
encouragement and provided facilities for this school project. I
sincerely appreciate her generosity by taking me into her fold for
which I shall remain indebted to her. I extend my appreciation to Mr.
Sarfaraz Khan sir, our computer teacher who guided me to the
successful completion of this project. I take this opportunity to
express my deep sense of gratitude to his invaluable guidance,
ongoing encouragement, enormous motivation, which has sustained
my efforts at every stage of project development.
Thirdly, I want to thank my parents for their immense support and
motivation throughout this phase. My friends, including my younger
brother, have been by my side in this whole journey and encouraged
me to submit this assignment in the allotted time span.

INDEX
S.No Title Page No. Remarks
.
1. Greatest Common divisor
2. Duck number
3. Disarium number
4. Decimal to octal number system
5. Floyd’s triangle
6. Bubble sort
7. Insertion sort
8. Selection sort
9. Binary search
10. Solving an operation
11. Double dimensional array
12. Alphabet frequency
13. Constructor
14. Piglatin word
15. String Fibonacci series
JAVA PROGRAMS
1. Write a program in Java to input two numbers and find their Greatest
Common Divisor (GCD).

ALGORITHM:
STEP 1: Start
STEP 2: Input two numbers from the user using the scanner class.
STEP 3: Check whether the two numbers are equal or not in while loop.
STEP 4: Find the greater number of the two and the subtract the smaller one
from it.
STEP 5: Repeat the same procedure until the while condition happens to be
false.
STEP 6: Print the first number.
STEP 7: End.

PROGRAM:
import java.util.*;
class Gcd
{
public static void main(String args[])throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the First no: ");
int n1=sc.nextInt();
System.out.print("Enter the Second no: ");
int n2=sc.nextInt();
while(n1 != n2)
{
if(n1 > n2)
n1 = n1-n2;
else
n2 = n2-n1;
}
System.out.print("GCD = "+n1);
} // main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n1, n2 int To accept two numbers from the
user.

OUTPUT:
Enter the First no: 24
Enter the Second no: 2
GCD = 2

2. Write a program in java to accept a number and check whether it is a Duck


number or not.
Note: A Duck number is a number which has zeroes present in it, but there
should be no zero present in the beginning of the number. For example: 3210,
7056, 8430709 are all duck numbers whereas 08237, 04309 are not .

ALGORITHM:
STEP 1: Start.
STEP 2: Accept a number from the user and store it in a string.
STEP 3: Initialise a counter variable for counting the number of integers in the
string (number).
STEP 4: Extract each digit and check whether it is zero or not. NOTE: The first
digit won’t count even if its 0.
STEP 5: If the value of the counter variable is greater than 0, the number is a
duck number.
STEP 6: End.

PROGRAM:
import java.io.*;
class Duck_No
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter any number: ");
String n=br.readLine(); //inputting the number and storing it in a String
int l=n.length(); //finding the length (number of digit) of the number
int c=0; //variable for counting number of zero digits
char ch;
for(int i=1;i<l;i++)
{
ch=n.charAt(i); //extracting each digit and checking whether it is a '0' or not
if(ch=='0')
c++;
}
char f=n.charAt(0); //taking out the first digit of the inputted number
if(c>0 && f!= '0')
System.out.println("It is a duck number");
else
System.out.println("It is not a duck number");
}// main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n String To accept and store the number.
ch char To store the extracted character.
C int To store the number of zero.
f char To store the first character.

OUTPUT:
Enter any number: 30065
It is a duck number.

3. Write a program in java to input a number and check whether it is a


Disarium number or not.
Note: A number will be called DISARIUM if sum of its digits powered with their
respective position is equal to the original number.

ALGORITHM:
STEP 1: Start.
STEP 2: Accept a number and store it in a temporary variable. Change the
original number to a String.
STEP 3: Store the length of the string and use it is a counter variable.
STEP 4: Extract each digit of the String and raise it the to the value of l (position
of the digit).
STEP 5: Store the values in the sum and check whether it is equal to the copy of
the number or not,
STEP 6: End.

PRORGAM:
import java.io.*;
class Disarium
    {
    public static void main(String[] args)throws IOException
        {
            BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
            System.out.print("Enter a number: ");
            int n = Integer.parseInt(br.readLine());
            int copy = n, d = 0, sum = 0;
            String s = Integer.toString(n); //converting the number into a String
            int l= s.length(); //finding the length of the number i.e. no.of digits
             
            while(copy>0)
            {
                d = copy % 10; //extracting the last digit
                sum = sum + (int)Math.pow(d,l);
                l--;
                copy = copy / 10;
            }
             
            if(sum == n)
                System.out.println(n+" is a Disarium Number.");
            
else
                System.out.println(n+" is not a Disarium Number.");
        }// main closed
    }// class closed

VARIABLE DESCRIPTION TABLE:


VARABLE NAME DATA TYPE PURPOSE
n int To accept a number from the user.
copy int To temporarily store the number.
d int To store the extracted digit.
sum int To store the required sum.
S String To store the number as a string.
l int To calculate the length of the string.

OUTPUT:
Enter a number: 89
89 is a Disarium Number.

4. Write a Program in Java to input a number in Decimal number system and


convert it into its equivalent number in the Octal number system.
Note: Octal Number system is a number system which can represent a number
in any other number system in terms of digits ranging from 0 to 7 only. This
number system consists of only eight basic digits i.e. 0, 1, 2, 3, 4, 5, 6 and 7. Eg:

ALGORITHM:
STEP 1: Start.
STEP 2: Accept a number from the user and declare a character array
(consisting of the digits of the octal number system).
STEP 3: Use the while loop with the condition being that the number is greater
than 0.
STEP 4: Calculate the remainder when the number is divided by 8.
STEP 5: Store the remainder in an empty string and divide the original number
by 8.
STEP 6: Print the result calculated in the octal number system.
STEP 7: End.

PROGRAM:
import java.io.*;
class Dec2Oct
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(newInputStreamReader(System.in));
System.out.print("Enter a decimal number: ");
int n=Integer.parseInt(br.readLine()); int r;
String s="";
char dig[]={'0','1','2','3','4','5','6','7'}; //array storing the digits (as characters)
in the octal number system
while(n>0)
{
r=n%8; //finding remainder by dividing the number by 8
s=dig[r]+s; //adding the remainder to the result and reversing at the
same time
n=n/8;
}
System.out.println("Its equivalent in octal number system= "+s);
}// main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To store the number accepted from the
user.
s String To store the number in octal number
system.
r int To store the remainder.

OUTPUT:
Enter a decimal number: 299
Its equivalent in octal number system= 453

5. Write a program in Java to print Floyd’s triangle.


ALGORITHM:
STEP 1: Start.
STEP 2: Accept the number of rows to be displayed from the user and initialise
the first element as 1.
STEP 3: Initiate the outer loop from 1 to the number of rows.
STEP 4: The inner loop will run from 1 to the outer loop. It determines the
number of columns.
STEP 5: Print the number and increase its value by 1 in the inner loop itself.
STEP 6: Use the println() function, in the outer loop, to form a triangle.
STEP 7: End.

PROGRAM:
import java.util.Scanner;
class FloydTriangle
{
public static void main(String args[])
{
int rows, num= 1, c, j;//To get the user's input
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows for Floyd's
triangle:");//Copying user input into an integer variable named rows
rows = sc.nextInt();
System.out.println("Floyd's triangle");
System.out.println("______________");
for ( c = 1 ; c <= rows ; c++ )
{
for ( j = 1 ; j <= c; j++ )
{
System.out.print(num+" ");//Incrementing the number value
num++;
}
System.out.println();//For new line
}
}// main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To store the value of each element.
rows int To accept the number of rows from the
user.
c int For the outer loop.
j int For the inner loop.

OUTPUT:
Enter the number of rows for Floyd's triangle:
5
Floyd's triangle
______________
1
23
456
7 8 9 10
11 12 13 14 15
6. Write a program in Java to accept the number of integers to be sorted along
with its elements. Use bubble sort technique for sorting the array in
descending order.

ALGORITHM:
STEP 1: Start.
STEP 2: Accept the required information from the user. Declare and initialise
the array.
STEP 3: Initiate the outer loop from 0 to the last cell (num-1).
STEP 4: Initiate the inner loop from 1 to (num-i).
STEP 5: Compare the neighbouring values and put the greater/smaller sign
accordingly.
STEP 6: Interchange /swap the values, if required, using a temporary variable.
STEP 7: Print the sorted array.
STEP 8: End.

PROGRAM:
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int num, i, j, temp;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = sc.nextInt();
int array[] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
array[i] = sc.nextInt();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num – i-1 ; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
} //main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
num int To accept the number of integers that are
to be sorted.
temp int For temporarily storing an integer.
i int For outer loop/ printing the sorted array.
j int For inner loop.

OUTPUT:
Enter 6 integers:
54
65
77
11
43
98
Sorted list of integers:
98
77
65
54
43
11

7. Write a program in Java to input the number of students and their marks in
Mathematics. Sort the marks using Insertion sort and print the final merit list.
ALGORITHM:
STEP 1: Start.
STEP 2: Input the number of students and marks of each student.
STEP 3: Initiate the outer loop from 1 to the number of the students(n) and
store the marks of first student in the temporary variable.
STEP 4: Begin the inner loop from (j=i-1) and continue the iteration until (j>=0
&& marks[j]<temp). NOTE: There will be certain sign changes for sorting of the
array in ascending order.
STEP 5: Interchange/ swap the marks, if required. Using the temporary
variable.
STEP 6: Print the final merit list.
STEP 7: End.

PROGRAM:
import java.util.Scanner;
class InsertionSort
{
public static void main(String args[])
{
int n, i, j, temp;
int marks[] = new int[];
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students : ");
n = sc.nextInt();
for(i=0; i<n; i++) {
System.out.println("Enter marks of the student with roll no.:"+(i+1));
marks[i] = sc.nextInt();
}
System.out.print("Sorting marks using Insertion Sort Technique\n");
for(i=1; i<n; i++){
temp=marks[i];
for(j=i-1; j>=0 && marks[j]<temp; j--)
marks[j+1]=marks[j];
marks[j+1]=temp;
}

System.out.print("The final merit list: \n");


for(i=0; i<n; i++)
System.out.print(marks[i] + " ");
}// main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To accept the number of students.
temp int For temporarily storing the marks.
i int For outer loop/ printing the sorted array.
j int For inner loop.

OUTPUT:
Enter the number of students: 5
Enter marks of the student with roll no.:1
99
Enter marks of the student with roll no.:2
89
Enter marks of the student with roll no.:3
91
Enter marks of the student with roll no.:4
85
Enter marks of the student with roll no.:5
96
Sorting marks using Insertion Sort Technique
The final merit list:
99 96 91 89 85

8. Write a program in java to accept the size of the array A[] from the user.
Input the names of cities into that array and sort it in alphabetical order using
the selection sort technique.
ALGORITHM:
STEP 1: Start.
STEP 2: Create the function void selectionSort() with the required formal
parameters.
STEP 3: Initiate a loop from 0 to less than(n-1).
STEP 4: Declare variables int pos and String s.
STEP 5: pos=x and s=arr[x];
STEP 6: Initiate another loop such that y = x + 1 up to y < n.
STEP 7: Compare s with arr[y] using the compareTo() function.
STEP 8: if(arr[y].compareTo(s) < 0), s=arr[y] and pos=y.
STEP 9: if(pos != x), swap the words using a temporary variable.
STEP 10: Noe, int the main() function, accept the size of the array and names of
the cities.
STEP 11: Call the function selectionSort().
STEP 12: Print the sorted array.
STEP 13: End.

PROGRAM:
import java.util.*;
import java.io.*;
class sorting
{
void selectionSort(String arr[],int n)
{
for(int x = 0; x < n - 1; x++)
{
int pos= x;// Find the minimum element in unsorted array
String s = arr[x];
for(int y = x + 1; y < n; y++)
{
if(arr[y].compareTo(s) < 0)
{
s=arr[y];
pos = y;
}
}
if(pos != x)
{
String temp = arr[pos];// Swapping the minimum element
arr[pos] = arr[x];
arr[x] = temp;
}
}
}
void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("enter the size of the array");
int n=sc.nextInt();
String A[]= new String[n];
for(int i=0; i<n; i++)
{
System.out.println("enter the city name at cell no."+i);
A[i]=sc.next();
}
selectionSort(A, n);
System.out.println("Sorted array is");// Printing the array after sorting
for(int i = 0; i < n; i++)
{
System.out.println(i+": "+A[i]);
}
}//main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To store the size of the array.
pos int To store the location of the smallest
wor(alphabetically).
x int For inner loop
y int For outer loop
i int For printing the sorted array.
temp String For swapping of the elements.

OUTPUT:
enter the size of the array
5
enter the city name at cell no.0
lucknow
enter the city name at cell no.1
delhi
enter the city name at cell no.2
chennai
enter the city name at cell no.3
goa
enter the city name at cell no.4
chandigarh
Sorted array is
0: chandigarh
1: chennai
2: delhi
3: goa
4: lucknow

9. Write a program in Java to accept the size and elements of an array arr[].
Ask the user to input the number to be searched and use binary search
technique for the same. Print appropriate message if the item isn’t found.
NOTE: The array is pre-sorted in ascending order.
ALGORITHM:
STEP 1: Start.
STEP 2: Create an array as per the size entered by the user and accept integers
into it.
STEP 3: Declare and initialise the variables first and last as 0 and (num-1)
respectively.
STEP 4: initialize m as (first+last)/2.
STEP 5: if ( arr[m] < item ), first = m + 1;
STEP 6: else if ( arr[m] == item )
STEP 7: Print that the item was found at location(m+1)and break.
STEP 8: else last=m-1
STEP 9: Repeat the steps from 4 to 7 until (first<=last).
STEP 10: If (first>last), print that the item is not found.
STEP 11: End.

PROGRAM:
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, num, item, arr[], first, last, m;//To capture user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = sc.nextInt(); //Creating array to store the all the numbers
arr= new int[num];
System.out.println(""Enter " + num + " integers sorted in ascending
order”);//Loop to store each number in array
for (c = 0; c < num; c++)
arr[c] = sc.nextInt();
System.out.println("Enter the search value:");
item = sc.nextInt();
first = 0;
last = num - 1;
while( first <= last )
{
m = (first + last)/2;
if ( arr[m] < item )
first = m + 1;
else if ( arr[m] == item )
{
System.out.println(item + " found at location " + (m + 1) + ".");
break;
}
else
last = m - 1;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}//main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
num int To store the size of array.
c int To initialize values to the array.
item int For storing the number to be searched.
first int For storing the first location of the array.
last int For storing the last location.
m int For storing the middle location.

OUTPUT:
Enter number of elements:
6
Enter 6 integers sorted in ascending order
2
3
4
55
67
81
Enter the search value:
3
3 found at location 2.
10. Write a program in Java to create two arrays- a[] and b[] and accept the
size of the first array. Ask the user to input integers/operands in the array a[]
and operators in the array b[]. Provide necessary details to the user and print
the final result of the expression. For example:
a= [8, 6, 4, 2] & b= [+, -, *], then answer will be (((8 + 6) - 4) * 2) = 20.
NOTE: Precedence table is not to be followed while executing the expression.

ALGORITHM:
STEP 1: Start.
STEP 2: Accept the size of the array and then declare both the arrays
accordingly (size of array b is n-1).
STEP 3: Initialise array a[] and array b[] according to the preferred data types,
provide certain operators to choose from.
STEP 4: Declare a variable s for storing the sum and initialise it with the first
operand.
STEP 5: Using multiple if statements, provide the condition for each operator
and perform the desired calculations.
STEP 6: Since the operators are stored in a String array, it would be preferred
to use the equals() function for comparing the inputted operators in the
conditional statement.
STEP 7: Print the final result.
STEP 8: End.

PROGRAM:
import java.util.Scanner;
import java.io.IOException;
public class Operation
{
public static void main()throws IOException
{
Scanner sc = new Scanner(System.in);
int i, n; double s = 0.0;
System.out.println("Enter the number of integers to be inputted:");
n= sc.nextInt();
int a[] = new int[n];
String b[] = new String[n-1];
for(i = 0;i<n;i++)
{
System.out.print ("Enter a Number: ");
a[i] = sc.nextInt();
}
for(i = 0;i<n-1;i++)
{
System.out.print ("Enter an Operator[+,-,*,/]: ");
b[i] = sc.nextLine();
}
s = a[0];
for(i = 0;i<n-1;i++)
{
if(b[i].equals("+"))
s = s+a[i+1];
if(b[i].equals("*"))
s = s*a[i+1];
if(b[i].equals("-"))
s = s-a[i+1];
if(b[i].equals("/"))
s = s/a[i+1];
}
System.out.println ("Result is -->"+s);
}// main closed
}// class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To accept the total number of operands
from the user.
i int For initialising and evaluating each
element.
s double For storing the final result.

OUTPUT:
Enter the number of integers to be inputted:
3
Enter a Number:
8
Enter a Number:
6
Enter a Number:
2
Enter an Operator[+,-,*,/]:
+
Enter an Operator[+,-,*,/]:
-
Result is -->12.0

11. Write a program to accept numbers into a 4X4 matrix and perform the
following operations:
 Calculate and print the sum of each diagonal.
 Display the elements present in the upper portion of the left diagonal.
 Print the array in matrix form.

ALGORITHM:
STEP 1: Start.
STEP 2: Initialise the array and accept its elements from the user.
STEP 3: For calculating the sum of elements of the left diagonal, number
of rows must be equal to the number of columns.
STEP 4: For finding the sum of elements of the left diagonal, sum of row
and column number must be 2.
STEP 5: For finding the upper portion of the left diagonal, the sum of row
and column must be less than or equal to 2.
STEP 6: Print the calculated results.
STEP 7: Create another nested for loop for printing the array in matrix
form.
STEP 8: Use an empty println() statement for the desired output.
STEP 9: End.

PROGRAM:
import java.util.*;
class DD
{
public static void main()
{
Scanner sc= new Scanner(System.in);
int a[][] = new int[3][3];
int i,j;
int s=0, sum=0;
for(i = 0;i<3;i++)
{
for(j = 0;j<3;j++)
{
System.out.print("Enter a number:");// accepting elements into the
array
a[i][j]= sc.nextInt();
}
} System.out.println("upper portion of the left diagonal");
for(i = 0;i<3;i++)
{
for(j = 0;j<3;j++)
{
if(i == j)
sum+= a[i][j];// calculating the sum of the left diagonal
if((i+j)==2)
s+= a[i][j];// calculatng the sum of right diagonal
if(i+j<= 2)//finding the upper portion of the left diagonal
System.out.print (a[i][j]+" ");
}
System.out.println ();
}
System.out.println("sum of elements of the left diagonal:"+sum);
System.out.println("sum of elements of the right diagonal:"+s);
System.out.println("Array in matrix form");
for(i = 0;i<3;i++)
{
for(j = 0;j<3;j++)
System.out.print (a[i][j]+" ");
System.out.println();
}
}//main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
i int For outer loop/number of rows.
j int For inner loop/ number of columns.
sum int For calculating the sum of elements
of the left diagonal.
s int For calculating the sum of elements
of the right diagonal.

OUTPUT:
Enter a number:2
Enter a number:3
Enter a number:4
Enter a number:8
Enter a number:9
Enter a number:1
Enter a number:0
Enter a number:5
Enter a number:7
upper portion of the left diagonal
234
89
0
sum of elements of the left diagonal:18
sum of elements of the right diagonal:13
Array in matrix form
234
891
057

12. Write a program in java to input a String from the user. Find and print the
frequency of each alphabet in the String, assuming that there aren’t any white
spaces in the String.

ALGORITHM:
STEP 1: Start.
STEP 2: Accept a string from the user and change it into lowercase for reducing
complexity.
STEP 3: Find and store the length of the provided string.
STEP 4: Declare two arrays char alph[] and int freq[] for storing each alphabet
and its frequency respectively.
STEP 5: Initiate a loop from 0 to less than 26.
STEP 6: Initialize the frequency of each corresponding alphabet as 0.
STEP 7: Initiate the outer loop from 0 to less than 26 and inner loop from 0 to
less than the length of the String.
STEP 8: Extract each alphabet from the string and store it in a char variable.
STEP 9: Compare the alphabet with the elements of array alph[] and increase
the frequency at the corresponding location in the array freq[].
STEP 10: Repeat step 5.
STEP 11: Check all values of the array freq[].
STEP 12: Print the alphabet only if its frequency is not 0.
STEP 13: End.

PROGRAM:
import java.util.*;
class Alphabet_Freq
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any word: ");
String s = sc.next();
s=s.toLowerCase(); //converting the string into lowercase
int l=s.length(); //finding the length of the string
char alph[] = new char[26]; //array for storing alphabets from 'a' to 'z'
int freq[] = new int[26]; //array for storing frequency of all alphabets
char c = 'a';
for(int i=0; i<26; i++)
{
alph[i]=c; //storing all alphabets from 'a' till 'z' in alph[] array
freq[i]=0; //intializing the count of every alphabet with '0'
c++;
}
System.out.println("Alphabet\tFrequency");
System.out.println("==========================");
char ch;
for(int i=0; i<26; i++)
{
for(int j=0; j<l; j++)
{
ch = s.charAt(j); //extracting characters of the string one by one
if(ch == alph[i]) //first checking the whole string for 'a', then 'b' and so
on
freq[i]++; //increasing count of those alphabets which are present
in the string
}
}
for(int i=0; i<26; i++)
{
if(freq[i]!=0) //printing only those alphabets whose count is not '0'
System.out.println(" "+alph[i]+"\t\t "+freq[i]);
}
}// main closed
}// class closed
VARIABLE NAME DATA TYPE PURPOSE
s String To store the string accepted from the
user.
l int To store the length of the string.
i int For initializing/ printing.
c int Counter variable
j int For inner loop.
ch char For storing the extracted character.
VARIABLE DESCRIPTION TABLE:

OUTPUT:
Enter any word: Interrogate

Alphabet Frequency

==========================
a 1
e 2
g 1
i 1
n 1
o 1
r 2
t 2

13. Define a class ParkingLot with the following description :


Instance variables/data members :
 int vno – to store the vehicle number
 int hours – to store the number of hours the vehicle is parked in the
parking lot
 double bill – to store the bill amount
Member methods :
 void input() – To input and store vno and hours
 void calculate() – To compute the parking charge at the rate of Rs.3 for the
first hour or part thereof, and Rs.1.50 for each additional hour or part
thereof.
 void display() – To display the detail.
Write a main method to create an object of the class and call the above
methods.

ALGORITHM:
STEP 1: Start.
STEP 2: Declare the instance variables/data members.
STEP 3: Create function void input() and input the formal parameters into the
instance variables.
STEP 4: Create function void calculate() and calculate the bill as per the
question.
STEP 5: Create void display() to display all the data members.
STEP 6: Create an object for the class ParkingLot and call each function.
STEP 7: End.

PROGRAM:
class ParkingLot
{
int vno,hours;
double bill;
void input(int v,int hr)
{
vno=v;
hours=hr;
}
void calculate()
{
if(hours<=1)
bill=3;
else
bill=3+((hours-1)*1.5);
}
void display()
{
System.out.println("Vehicle Number:"+vno);
System.out.println("No. Of hours :"+hours);
System.out.println("Bill :" +bill);
}
void main(int v1,int h1)
{
ParkingLot p=new ParkingLot();
p.input(v1,h1);
p.calculate();
p.display();
}//main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
vno int To store the vehicle number.
hours int to store the number of hours the vehicle
is parked in the parking lot.
bill double To calculate and store the amount of bill.
v int To accept the vehicle number.
hr double To accept the number of hours from the
user.

OUTPUT:
Vehicle Number:657847
No. Of hours :65
Bill :99.0

14. Write a program that encodes each word of the String into Piglatin. To
translate a word into a Piglatin word, convert the word into uppercase and
then place the first vowel of the original word as the start of the new word
along with the remaining alphabets. The alphabets present before the vowel
being shifted towards the end followed by “AY”.

Sample Input: London,


Sample Output: ONDONLAY

ALGORITHM:
STEP 1: Start.
STEP 2: Accept the string from the user and change it into uppercase.
STEP 3: Initiate a loop from 0 to less than the length of the String.
STEP 4: Extract each character from the String and separate words from it.
STEP 5: Call the function void isPiglatin(), when the word is made.
STEP 6: Declare and initialize the String piglatin and int flag as null.
STEP 7: Repeat step 3 with the length of the word.
STEP 8: Extract each character and check whether it is a vowel or not.
STEP 9: If the condition happens to be true form the piglatin word.
STEP 10: piglatin=word.substring(i)+word.substring(0,i)+"AY"
STEP 11: Increase the flag(counter variable).
STEP 12: Break.
STEP 13: If flag is still 0, add AY to the word.
STEP 14: Print the piglatin word.
STEP 15: Repeat the steps from 5 to 14.
STEP 16: End.

PROGRAM:
import java.util.*;
class PiglatinWord
{
Scanner ob=new Scanner(System.in);
void main(String args[])
{
System.out.println("Enter the String to be converted.");
String s=ob.nextLine();
s=s.toUpperCase();// converting the whole string into uppercase.
String word=" ";
for(int i=0;i<s.length();i++)
{
char ch= s.charAt(i);
if(ch!=' ')//extraction of each word
word= word+ch;
else
isPiglatin(word);
}
}//main closed
void isPiglatin(String word)
{
String piglatin="";
int flag=0;
for(int i=0;i<word.length();i++)
{
char x=word.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' ||x=='U')
{
piglatin=word.substring(i)+word.substring(0,i)+"AY";//forming the
piglatin word
flag=1;
break;
}
}
if(flag==0)
{
piglatin=word+"AY";
}
System.out.println(word+" in Piglatin format is "+piglatin);
}//isPiglatin closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
s String For accepting the string from the user.
word String To store each word
ch, x char To store the extracted character
piglatin String To store he piglatin word.
flag int Counter variable
i int For initiating the loop.

OUTPUT:
Enter the String to be converted.
How are you
HOW in Piglatin format is OWHAY
ARE in Piglatin format is AREAY
YOU in Piglatin format is OUYAY
15. A sequence of Fibonacci Strings is generated as follows:

S0 = “a”, S1 = “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”, 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 string 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.

ALGORITHM:
STEP 1: Start.
STEP 2: Declare the data members and initialize their values as per the
question in the non-parameterized constructor,
STEP 3: Create the function void accept() for accepting the number of terms
from the user.
STEP 4: Create the function void generate().
STEP 5: if(n <= 1), print the first term only.
STEP 6: Print the first two terms.
STEP 7: Initiate a loop from 3 to less than equal to n.
STEP 8: z = y+x; print the third term, if required.
STEP 9: Swap the variables x-y and y-z.
STEP 10: Repeat the steps 8 and 9.
STEP 11: Create the main method for calling the functions.
STEP 12: End.

PROGRAM:
import java.util.*;
class FiboString
{
String x,y,z;
int n;
FiboString()
{
x = "a";
y = "b";
z = "ba"; // mentioned in the question otherwise not required. z = "" is
sufficient
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the number of terms : ");
n = sc.nextInt();
}
void generate()
{
System.out.print("The Fibonacci String Series is : ");
if(n <= 1) // If no of terms is less than or equal to 1
System.out.print(x);
else
{
System.out.print(x+", "+y);
for(int i=3; i<=n; i++)
{
z = y+x;
System.out.print(", "+z);
x = y;
y = z;
}
}
}
public static void main(String args[])
{
FiboString ob = new FiboString();
ob.accept();
ob.generate();
}//main closed
}//class closed

VARIABLE DESCRIPTION TABLE:


VARIABLE NAME DATA TYPE PURPOSE
n int To accept the number of terms from the
user.
x String To store the first string.
y String To store the second string.
z string To store the concatenation of previous two
strings.

OUTPUT:
Enter the number of terms : 5
The Fibonacci String Series is : a, b, ba, bab, babba

You might also like