Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 58

TRIBENI TISSUES VIDYAPITH

12/8/2022 COMPUTER
APPLICATION
JAVA PROGRAMMING
NAME: Babloo Singh
CLASS: 10
SECTION: C
ROLLNUMBER:13
U.I.D. NUMBER: 7970586
SUBJECT: Computer Application
SUBJECT: 2022-2023

1
ACKNOWLEDGEMENT

Primarily I would thank God for being able to complete this with success.
Then I would like to express my special thanks of gratitude to my computer
teacher Mr. Avishek Paul as well as our principal Mrs. Sanghamitra
Chatterjee who gave me the golden opportunity to do this wonderful project
on the given Topic. Which also helped me in doing a lot of research and I
came to know about so many new things, I am really thankful to them.

I would like to thank my parents who helped me a lot in finalizing this project
within the limited time frame.

Thank you
Name: Babloo Singh
Date: 08.12.2022

2
CONTENTS

Design a class name ShowRoom with the following description:


Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store customer’s mobile number.
double cost: to store the cost of the item
purchased. double dis: to store the discount
amount.
double amount: to store the amount to be paid after discount.
Methods:
ShowRoom(): default constructor to initialize the data members
void input(): to input customer name, mobile number, cost

Cost Discount (In percentage)

Less than or equal to ₹ 10000 5%


More than ₹ 10000 and less than 10%
or equal to ₹ 20000
More than ₹ 20000 and less than 15%
or equal to ₹ 35000
More than ₹ 35000 20%
void calculate(): to calculate the discount on the cost of purchased
items, based on the following criteria.
void display()- To display customer, mobile number, amount to be paid
after discount.
Write a main() method to create an object of the class and call the
above methods.

Design a class to overload a function volume() as follows:


(i) double volume(double r) – with radius ‘r’ as an argument,
returns the volume of sphere using the formula:
v = 4 / 3 × 22 / 7 × r3
(ii) double volume(double h, double r) – with height ‘h’ and radius ‘r’
as the arguments, returns the volume of a cylinder using the formula:
v = 22 / 7 × r2 × h
(iii) double volume(double l, double b, double h) – with length ‘l’,
breadth ‘b’ and height ‘h’ as the arguments, returns the volume of
a
3
cuboid using the formula: v = l × b × h
Write main() method to call any method by passing values properly.

Design a class to overload a function area() as follows:


(i) double area(double a, double b, double c) with three double
arguments, returns the area of a scalene triangle.
area = √(s(s – a)(s – b)(s – c))
where s = (a + b + c) / 2.
(ii) double area(int a, int b, int height) with three integer
arguments, returns the area of a trapezium
area = 1/2 × height × (a + b)
(iii) double area(double diagonal1, double diagonal2) with two
double arguments, returns the area of a rhombus
area = 1/2 × (diagonal1 × diagonal2)
Write main() to call above all method by passing values appropriately.

Define a class Student described as below:


Data members/instance variables : name,age,m1,m2,m3 (marks in 3
subjects), maximum, average
Member methods :
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and
average.
Write a main method to create an object of a class and call the above
member methods accordingly.

Write a menu driven program to accept a number and check


and display whether it is a Prime Number or not OR an
Automorphic Number or not. (Use switch-case statement).
(a) Prime number: A number is said to be a prime number if it is
divisible only by 1 and itself and not by any other number.
Example: 3,5,7,11,13 etc.
(b) Automorphic number: An automorphic number is the
number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is
present as the last two digits.

4
Using switch statement, write a menu driven program for the
following:
To find and display the sum of the series given below:
S = x^1 – x^2 + x^3 – x^4 + x^5 … – x^20 (where x=2)
To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be
displayed.

A tech number has an even number of digits if the number is split in


two equal halves, then the square of sum of these two halves is equal
to the number itself. Write a program to generate and print all 4-digit
tech numbers:
Example:
 Consider the number 3025
 Square of sum of the halves of 3025 = (30 + 25) ²
= (55)²,it is a tech number.
Write a program to input a number and check and print whether it is a
Pronic number or not. Pronic number is the number which is the
product of two consecutive integers.
Examples:
 12 = 3 × 4
 20 = 4 × 5
 42 = 6 × 7

Using the switch statement, write a menu driven program for the
following:
To print the Floyd’s triangle [Given below]
1
23
456
7 8 9 10
11 12 13 14 15
To display the following pattern
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be
displayed.

5
Using the switch statement. write a menu driven program to calculate
the maturity amount of a Bank Deposit. The user is given the
following options:
For option (i) accept principal(P), rate of interest(r) and time period in
years(n). Calculate and output the maturity amount(A) receivable
using the Formula: P(1+r/100) n
For option (ii) accept Monthly Instalment (P), rate of interest(r) and
time period in months (n). Calculate and output the maturity
amount(A) receivable using the Formula:
P*n+(P*(n*(n+1))/2) *(r/100) *(1/12)
For an incorrect option, an appropriate error message should be
displayed.

Write a program to input 15 integer elements in an array and sort


them in ascending order using the bubble sort technique.

Write a program to input twenty names in an array. Arrange these


names in descending order of alphabets, using the bubble sort
technique.

Write a program to accept the names of 10 cities in a single dimension


string array and their STD (Subscribers Trunk Dialling) codes in another
single dimension integer array. Search for a name of a city input by the
user in the list. If found, display “Search Successful” and print the
name of the city along with its STD code, or else display the message
“Search Unsuccessful, No such city in the list’.

Write a program to accept the year of graduation from school as an


integer value from the user. Using the Binary Search technique on the
sorted array of integers given below, output the message ‘Record
exists’ if the value input is located in the array. If not, output the
message Record does not exist”.
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)

Write a program to accept name and total marks of N number


of students in two single subscripts array name[] and
totalmarks[]. Calculate and print:
The average of the total marks obtained by N number of
students. [average = (sum of total marks of all the students) / N]

6
Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]

Write a program to print frequency of vowels in the given sentence.

Write a program to print each word in a given sentence.

Write a program to check whether a word is palindrome or not.

Write a program to replace lower case character to upper case and vice
versa in a given sentence.

Write a program in Java to accept a name and display the initials of


first and middle name and full last name.
Example:
 Input: Laxmi Narayan
Sharma Output: L N Sharma
Example:
 Input: Akash Sharma
Output: A Sharma

7
Design a class name ShowRoom with the following description:
Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store customer’s mobile number.
double cost: to store the cost of the item purchased.
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Methods:
ShowRoom(): default constructor to initialize the data members
void input(): to input customer name, mobile number, cost
void calculate(): to calculate the discount on the cost of purchased items,
based on the following criteria.
Cost Discount(In
percentage)
Less than or equal to ₹ 10000 5%

More than ₹ 10000 and less than or equal to ₹ 20000 10%

More than ₹ 20000 and less than or equal to ₹ 35000 15%

More than ₹ 35000 20%

void display()- To display customer, mobile number, amount to be paid


after discount.
Write a main() method to create an object of the class and call the above
methods.

import java.util.*;
class ShowRoom
{//class definition starts here
String name;
long mobno;
double cost;
double dis;
double amount;
ShowRoom()
{
name = " ";
cost=0;
mobno=0L;

8
dis = 0.0;
amount=0.0;
}
void input()
{
Scanner sc = new Scanner(System.in);//creating object of scanner class
System.out.println("Enter the customer name");
name=sc.next();
System.out.println("Enter the customer mobile number");
mobno=sc.nextLong();
System.out.println("Enter the cost of the items purchased");
cost=sc.nextDouble();
}
void calculate()
{
if(cost <=10000)
{
dis=0.05*cost;
}
else if(cost >10000 && cost <=20000)
{
dis=0.1*cost;
}
else if(cost >20000 && cost <=35000)
{
dis=0.15*cost;
}
else
{
dis=0.2*cost;
}
amount = cost - dis;
}
void display()
{
System.out.println("Customer Name :"+name);
System.out.println("Customer Mobile Number :"+mobno);
System.out.println("Amount :"+ amount);
}
public static void main (String arg[])

9
{//main method starts
ShowRoom ob =new ShowRoom();//creating an object of the class
ob.input();
ob.calculate();
ob.display();
}//main method ends here
}//class definition ends here

10
Name Type Description
name String To store the name of
the customer.
mobno long To store the mobile
number of the
customer.
cost double To store the cost of the
item purchased.
dis double To store the discount on
the item purchased.
amount double To store the final
amount to be paid.

11
Design a class to overload a function volume() as follows:
(i) double volume(double r) – with radius ‘r’ as an argument, returns the
volume of sphere using the formula:
v = 4 / 3 × 22 / 7 × r3
(ii)double volume(double h, double r) – with height ‘h’ and radius ‘r’ as the
arguments, returns the volume of a cylinder using the formula:
v = 22 / 7 × r2 × h
(iii) double volume(double l, double b, double h) – with length ‘l’, breadth
‘b’ and height ‘h’ as the arguments, returns the volume of a cuboid using
the formula:
v=l×b×h
Write main() method to call any method by passing values properly.

class Overload
{//class beginning

double volume(double r)
{
double v=(4.0/3)*(22.0/7)*Math.pow(r,3);//calculating the volume of
sphere
return(v);
}

double volume( double h,double r)


{
double v=(22.0/7)*Math.pow(r,2)*h;//calculating the volume of cylinder
return(v);
}

double volume( double l, double b, double h)


{
double v=l*b*h;//calculating the volume of cuboid
return(v);//returning the value of v
}
}//class ends

12
Name Type Description
v double To store volume.
r double To store radius.
h double To store height.
l double To store length.
b double To store breadth.

13
Design a class to overload a function area() as follows:
(i) double area(double a, double b, double c) with three double arguments,
returns the area of a scalene triangle.
area = √(s(s – a)(s – b)(s – c))
where s = (a + b + c) / 2.
(ii) double area(int a, int b, int height) with three integer arguments,
returns the area of a trapezium
area = 1/2 × height × (a + b)
(iii) double area(double diagonal1, double diagonal2) with two double
arguments, returns the area of a rhombus
area = 1/2 × (diagonal1 × diagonal2)
Write main() to call above all method by passing values appropriately.

class program
{
double ar;
double area(double a, double b, double c)
{
double s=(a+b+c)/2;
ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));//calculating the area of a scalene
triangle
return(ar);//returning the value of the area
}

double area(int a, int b, int height)


{
ar=0.5*height*(a+b);// calculating the area of a trapezium
return(ar);//returning the value of the area

double area(double diagonal1, double diagonal2)


{
ar=0.5*diagonal1*diagonal2;// calculating the area of a rhombus
return(ar);//returning the value of the area

}
}

14
Name Type Description
a double To store a double
argument.
b double To store a double
argument.
c double To store a double
argument.
ar double To calculate area.
s double As a variable used to
calculate area of a
scalene triangle.
height int To store the height of
the trapezium.
diagonal1 double To store the value of the
diagonal of a rhombus.
diagonal2 double To store the value of the
diagonal of a rhombus.

15
Define a class Student described as below:
Data members/instance variables : name,age,m1,m2,m3 (marks in 3
subjects), maximum, average
Member methods :

(i) A parameterized constructor to initialize the data members.


(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and
average.

Write a main method to create an object of a class and call the above
member methods accordingly.

import java.util.*;
class Student
{
String name;
int age;
int m1;//declaring as data members
int m2;
int m3;
int maximum;
double average;
Student()
{
name ="";
age = 0;//initializing the variables
m1 = 0;
m2 = 0;
m3 = 0;
maximum = 0;
average = 0.0;
}
void accept()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();

16
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter Subject 1 Marks: ");
m1 = in.nextInt();
System.out.print("Enter Subject 2 Marks: ");
m2 = in.nextInt();
System.out.print("Enter Subject 3 Marks: ");
m3 = in.nextInt();
}
void compute()
{
if (m1 > m2 && m1 > m3)
maximum = m1;
else if (m2 > m1 && m2 > m3)
maximum = m2;
else
maximum = m3;

average = (m1 + m2 + m3) / 3.0;


}

void display()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Subject 1 Marks: " + m1);
System.out.println("Subject 2 Marks: " + m2);
System.out.println("Subject 3 Marks: " + m3);
System.out.println("Maximum Marks: " + maximum);
System.out.println("Average Marks: " + average);
}

public static void main(String args[])


{
Student obj = new Student();
obj.accept();//calling the member method
obj.compute();
obj.display();
}
}//end of class

17
Name Type Description
m1 int To store the marks of
subject1.
m2 int To store the marks of
subject2.
m3 int To store the marks of
subject3.
age int To store the age of the
student.
maximum int To store the maximum
marks of the student.
average double To store the average
marks of the student.
name String To store the name of
the student.

18
Write a menu driven program to accept a number and check and display
whether it is a Prime Number or not OR an Automorphic Number or not.
(Use switch-case statement).

(a) Prime number: A number is said to be a prime number if it is divisible


only by 1 and itself and not by any other number. Example: 3,5,7,11,13 etc.

(b) Automorphic number: An automorphic number is the number which


is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present
as the last two digits

import java.util.Scanner;
class Menu
{//start of class
public static void main(String args[])
{//start of main method
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();

switch (choice)
{
case 1:
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else

19
System.out.println(num + " is not Prime");
break;

case 2:
int num2= num;
int sq = num * num;
int d = 0;
while(num > 0)
{
d++;
num /= 10;
}
int ld = (int)(sq % Math.pow(10, d));//type casting to integer

if (ld == num2)
System.out.println(num2 + " is automorphic");
else
System.out.println(num2 + " is not automorphic");
break;

default:
System.out.println("Incorrect Choice");
break;//to prevent fall through
}
}//end of main method
}//end of class

20
Name Type Description
choice int To store the choice of
the user.
num int To store the number
given by the user.
c int Counter variable.
i int Loop variable.
num2 int Temporary variable.
sq int To store the square of
the number.
d int Loop variable.
ld int Integer.

21
Using switch statement, write a menu driven program for the following:
(i) To find and display the sum of the series given below:
S = x^1 – x^2 + x^3 – x^4 + x^5 … – x^20 (where
x=2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be displayed.

import java.util.*;
class XP6
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int choice=0,i=0,j=0;
double sum=0;
System.out.println("Press 1 for Sum of
series"); System.out.println("Press 2 to Display
Series"); System.out.print("Enter your choice:
");
Scanner sc=new Scanner(System.in);//creating an object for scanner class
choice=sc.nextInt();
switch(choice)
{
case 1:
sum=0;
for(i=1;i<=20;i++)
{
if(i%2==0)//checking whether i is even or
not sum=sum-Math.pow(2,i);
else
sum=sum+Math.pow(2,i);
}
System.out.println("Sum = "+sum);
break;//to prevent fall through
case 2:
int temp=0;
for(i=1;i<=5;i++)
{
temp=temp*10+1;
System.out.print(temp+" ");
}
22
break;
default:
System.out.println("Invalid choice");
}
}//main method ends here
}//class definition ends here

23
Name Type Description
choice int To store the choice of
the user.
i int Loop variable.
sum double To store the sum of the
expression.
temp int To display the pattern.

24
A tech number has an even number of digits if the number is split in two
equal halves, then the square of sum of these two halves is equal to the
number itself. Write a program to generate and print all 4-digit tech
numbers:
Example:
Consider the number 3025
Square of sum of the halves of 3025
= (30 + 25) ²
= (55) ², it is a tech number.

class XP7
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int firstHalf=0,secondHalf=0,sumOfSquare=0,i=0;
for(i=1000;i<=9999;i++)
{
/* dividing number
into two half*/
firstHalf=i/100;
secondHalf=i%100;
sumOfSquare=(int)Math.pow((firstHalf+secondHalf),2);
if(sumOfSquare==i)//checking whether the number is a tech number or
not
{
System.out.print(i+" ");
}
}
}//main method ends here
}//class definition ends here

25
Name Type Description
firstHalf int To store the first half of
the number.
secondHalf int To store the second half
of the number.
sumOfSquare int To store the sum of the
two halves of the
number.
i int Loop variable.

26
Write a program to input a number and check and print whether it is a Pronic
number or not. Pronic number is the number which is the product of two
consecutive integers.
Examples:
12 = 3 × 4
20 = 4 × 5
42 = 6 × 7

import java.util.*;
class XP8
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int num=0, i=0;//initializing num and i as int
boolean flag=false;//initializing flag as boolean value
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter a number");
num=sc.nextInt();
for(i=0;i<=num;i++)
{
if((i*(i+1))==num)//checking whether the number is pronic or not
{
flag=true;
break;
}
}
if(flag==true)
{
System.out.print(num + " is a pronic number ");
}
else
{
System.out.print(num + " is not a pronic number ");
}
}//main method ends here
}//class definition ends here

27
Name Type Description
num int To store the input from
the user.
i int Loop variable.
flag boolean To check whether
conditions are true or
false.

28
Using the switch statement, write a menu driven program for the following:
(i) To print the Floyd’s triangle [Given
below] 1
23
456
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.

import java.util.*;
class XP9
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int choice=0,i=0,j=0,n=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Press 1 for Floyd's triangle");
System.out.println("Press 2 to print ICSE Pattern");
System.out.print("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter number of lines: ");
n=sc.nextInt();
int number=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(number+" ");
number++;//incrementing the value of the number
}

29
System.out.println();
}
break;//to prevent fall through
case 2:
System.out.print("Enter a word: ");
String word=sc.next();
for(i=0;i<word.length();i++)
{
for(j=0;j<=i;j++)
{
System.out.print(word.charAt(j)+" ");
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice");
}
}//main method ends here
}//class definition ends here

30
Name Type Description
i int Loop variable.
j int Loop variable.
n int To store the number of
lines for the pattern.
choice int To store the choice of
the user.
number int To increment and
display the pattern.
word String To store the word for
the pattern.

31
Using the switch statement. write a menu driven program to calculate the
maturity amount of a Bank Deposit. The user is given the following options:
Option (i) accept principal(P), rate of interest(r) and time period in years(n).
Calculate and output the maturity amount(A) receivable using the
Formula: P(1+r/100) n
Option (ii) accept Monthly Instalment (P), rate of interest(r) and time period
in months (n). Calculate and output the maturity amount(A) receivable
using the Formula: P*n+(P*(n*(n+1))/2) *(r/100) *(1/12)
For an incorrect option, an appropriate error message should be displayed.

import java.util.*;
class XP10{//class definition starts here
public static void main()//program commands here
{//main method starts here
double maturityAmount=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Press 1 for Term Deposit");
System.out.println("Press 2 for Recurring Deposit");
System.out.println("Enter your choice: ");
Int choice=sc.nextInt();
switch(choice) {
case 1:
System.out.println("Enter principal: ");
double P1=sc.nextDouble();
System.out.println("Enter rate of interest:
"); double r1=sc.nextDouble();
System.out.println("Enter period in years: ");
double n1=sc.nextDouble();
maturityAmount=P1*Math.pow(1+r1/100.0,n1);//calculating the
maturity amount
System.out.println("Maturity Amount is "+maturityAmount);
break;//to prevent fall through
case 2:
System.out.println("Enter monthly installment: ");
double P2=sc.nextDouble();
System.out.println("Enter rate of interest: ");
double r2=sc.nextDouble();
System.out.println("Enter period in months: ");
double n2=sc.nextDouble();

32
maturityAmount=P2*n2+P2*(n2*(n2+1)/2.0)*(r2/100.0)*(1.0/12);
System.out.println("Maturity Amount is "+maturityAmount);
break;
default:
System.out.println("Invalid choice");}
}//main method ends here
}//class definition ends here

33
Name Type Description
i int Loop variable.
choice int To store the choice of
the user.
maturityAmount double To store the calculated
maturity amount.
P1 double To store the principal in
case 1.
r1 double To store the rate of
interest in case 1.
n1 double To store the periods in
years in case 1.
P2 double To store the principal in
case 2.
r2 double To store the rate of
interest in case 2.
n2 double To store the periods in
months in case 2.

34
Write a program to input 15 integer elements in an array and sort them in
ascending order using the bubble sort technique.

import java.util.*;
class XP11
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int i=0,temp=0,j=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
int a[]=new int[15];//declaring an array a of base type int and can hold
15 elements
System.out.println("Enter 15 elements in an array ");
for(i=0;i<15;i++)
{
a[i]=sc.nextInt();//taking input of the elements of the array
}
/*sorting integers
using the bubble sort technique*/
for(i=0;i<a.length-1;i++)
{
for(j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])//two adjacent elements are being compared
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");//printing the sorted array
}
}//main method ends here
}//class definition ends here

35
Name Type Description
i int Loop variable.
temp int Temporary variable.
j int Loop variable.

36
Write a program to input twenty names in an array. Arrange these names in
descending order of alphabets, using the bubble sort technique.

import java.util.*;
class XP12
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int i=0,j=0;
String temp="";
Scanner sc=new Scanner(System.in);//creating an object for scanner class
String names[]=new String[20];//declaring an array names of base type
String and can hold 20 elements
System.out.println("Enter 20 names");
for(i=0;i<names.length;i++){
names[i]=sc.nextLine();//taking input of the elements of the array
}
/*sorting names
using the bubble sort technique*/
for(i=0;i<names.length-1;i++)
{
for(j=0;j<names.length-i-1;j++){
if(names[j].compareTo(names[j+1])<0)//two adjacent elements are
being compared
{
temp=names[j];
names[j]=names[j+1];
names[j+1]=temp;
}
}
}
System.out.println("Sorted names: ");
for(i=0;i<names.length;i++)
{
System.out.print(names[i]+" ");//printing the sorted array in
descending order
}
}//main method ends here
}//class definition ends here

37
Name Type Description
i int Loop variable.
temp int Temporary variable.
j int Loop variable.

38
Write a program to accept the names of 10 cities in a single dimension string
array and their STD (Subscribers Trunk Dialling) codes in another single
dimension integer array. Search for a name of a city input by the user in the
list. If found, display “Search Successful” and print the name of the city along
with its STD code, or else display the message “Search Unsuccessful, No such
city in the list’.

import java.util.*;
class XP13
{//class definition starts here
public static void main()//program commands here
{//main method starts here
Scanner sc=new Scanner(System.in);//creating an object for scanner class
String city[]=new String[10];//declaring an array city of base type String
and can hold 10 elements
int std[]=new int[10];//declaring an array std of base type int and can hold
10 elements
int i=0;
System.out.print("Enter city: ");
for(i=0;i<city.length;i++)
{
city[i]=sc.next();//taking input of the elements of the array
}
System.out.print("Enter STD: ");
for(i=0;i<std.length;i++)
{
std[i]=sc.nextInt();//taking input of the elements of the array
}
System.out.print("Enter city name to search: ");
String target=sc.next();
boolean flag=false;
/*searching the city
using the linear search technique*/
for(i=0;i<10;i++)
{
if(city[i].equalsIgnoreCase(target)==true)//two adjacent elements are
being compared
{
System.out.println("Search successful");

39
System.out.println("City : " + city[i]);
System.out.println("STD code : " +
std[i]); flag=true;
break;//to terminate the loop as soon as the search-item is found}
}
if(flag==false)
System.out.println("Search Unsuccessful, No such city in the list");
}//main method ends here
}//class definition ends here

40
Name Type Description
target String To store the input from
the user.
i int Loop variable.
flag boolean To check whether
conditions are true or
false.

41
Write a program to accept the year of graduation from school as an integer
value from the user. Using the Binary Search technique on the sorted array
of integers given below, output the message ‘Record exists’ if the value input
is located in the array. If not, output the message Record does not exist”.
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)

import java.util.*;
class XP14
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int left=0,right=0,mid=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner class
System.out.print("Enter year of graduation: ");
int graduation=sc.nextInt();
int years[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009,
2010};//declaring and initializing array
boolean found=false;
right=years.length - 1;
/*searching the year of graduation
using the binary search technique*/
while(left<=right)//the loop repeats as long the lower limit(left) of the
segment is <= the upper limit(right)
{
mid=(left+right)/2;
if(years[mid]==graduation){
found=true;
break;
}
else if(years[mid]<graduation)//search item(graduation) compared
with element at middle position(year[mid])
left=mid+1;
else
right=mid-1; }
if(found==true)
System.out.println(graduation+" Record exists");
else
System.out.println(graduation+" Record does not exist");
}//main method ends here
}//class definition ends here

42
Name Type Description
graduation int To store the input from
the user.
i int Loop variable.
flag boolean To check whether
conditions are true or
false.
left int To store the lower limit
of the array.
right int To store the upper limit
of the array.

43
Write a program to accept name and total marks of N number of students in
two single subscripts array name[] and totalmarks[].
Calculate and print:
(i)The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students) / N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]

import java.util.*;
class XP15
{//class definition starts here
public static void main()//program commands here
{//main method starts here
int sum=0,i=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.print("Enter number of students: ");
int n=sc.nextInt();
String name[]=new String[n];//declaring an array names of base type
String and and can hold n no. of elements
int totalmarks[]=new int[n];//declaring an array totalmarks of base type
int and can hold n no. of elements
for(i=0;i<n;i++)
{
System.out.println("Student "+(i+1));
System.out.print("Enter name of student: ");
name[i]=sc.next();
System.out.print("Enter marks: ");
totalmarks[i]=sc.nextInt();
}
for(i=0;i<n;i++) {
sum=sum+totalmarks[i];//calculating the sum of the marks }
double average=(double)sum/n;//calculating the average of the marks
System.out.println("Average of all marks "+average);
for(i=0;i<n;i++) {
double deviation=totalmarks[i] - average;//calculating the deviation
using the formula
System.out.println("Deviation of "+name[i]+" is "+deviation); }
}//main method ends here
}//class definition ends here

44
Name Type Description
n int To store the input from
the user.
I int Loop variable.
sum int To store the sum of
total marks.
average int To store the average.
deviation int To store the deviation.

45
Write a program to print frequency of vowels in the given sentence.

import java.util.*;
class XP16
{//class definition starts here
public static void main()//program commands here
{//main method starts here
String sen="";//initializing sen as String
char ch=' ';//initializing ch as char
int i=0,len=0,count=0;//initializing i,len,count as int
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter a sentence");
sen=sc.nextLine();
sen=sen.toLowerCase();//converting the String to lower case
len=sen.length();//storing the length of the String sen into len
for(i=0;i<len;i++)
{
ch=sen.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')//checking whether ch
is a vowel
{
count++;
}
}
System.out.println("Frequency of vowels in sentence is:"+count);
}//main method ends here
}//class definition ends here

46
Name Type Description
sen String To store the input from
the user.
i int Loop variable.
count int To store total
occurrence of vowels.
len int To store the length of
the String.
ch char To pick up and store a
character from the
String.

47
Write a program to print each word in a given sentence.

import java.util.*;
class XP17
{//class definition starts here
public static void main()//program commands here
{//main method starts here
String sen="",wd="";//intializing sen and wd as String
char ch=' ';
int i=0,len=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter a sentence");
sen=sc.nextLine();
sen=sen+" ";//adding a space in the beginning of the String
sen=sen.toLowerCase();
len=sen.length();
for(i=0;i<len;i++)
{
ch=sen.charAt(i);
if(ch==' ')//checking whether ch is a space or not
{
System.out.print(wd+" ");
wd="";
}
else
{
wd=wd+ch;
}
}
}//main method ends here
}//class definition ends here

48
Name Type Description
sen String To store the input from
the user.
i int Loop variable.
wd String To store characters of a
word.
len int To store the length of
the String.
ch char To pick up and store a
character from the
String.

49
Write a program to check whether a word is palindrome or not.

import java.util.*;
class XP18
{//class definition starts here
public static void main()//program commands here
{//main method starts here
String wd="",wd1="";//intializing wd and wd1 as String
char ch=' ';
int i=0,len=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter a word");
wd=sc.next();
System.out.println("Given word:"+wd);
len=wd.length();//storing the length of the String wd into len
for(i=0;i<len;i++)
{
ch=wd.charAt(i);
wd1=ch+wd1;
}
if(wd1.equals(wd)==true)//checking whether the String is palindrome or
not
{
System.out.println(wd+" is a palindrome word");
}
else
{
System.out.println(wd+" is not a palindrome word");
}
}//main method ends here
}//class definition ends here

50
Name Type Description
wd String To store the input from
the user.
i int Loop variable.
wd1 String To store palindrome of
the word.
len int To store the length of
the String.
ch char To pick up and store a
character from the
String.

51
Write a program to replace lower case character to upper case and vice
versa in a given sentence.

import java.util.*;
class XP19
{//class definition starts here
public static void main()//program commands here
{//main method starts here
String sen="",wd="";//initializing sen and wd as String
char ch=' ';
int i=0,len=0;//initializing i and len as int
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter a sentence");
sen=sc.nextLine();
sen=sen+" ";
len=sen.length();
for(i=0;i<len;i++)
{
ch=sen.charAt(i);
if(ch==' ')//checking whether ch is a space
{
System.out.print(wd+" ");
wd="";
}
else
{
if(Character.isLowerCase(ch)==true)//checking whether ch is a lower
case
{
wd=wd+Character.toUpperCase(ch);
}
else
wd=wd+Character.toLowerCase(ch);
}
}
}//main method ends here
}//class definition ends here

52
Name Type Description
sen String To store the input from
the user.
i int Loop variable.
wd String To store characters of a
word.
len int To store the length of
the String.
ch char To pick up and store a
character from the
String.

53
Write a program in Java to accept a name and display the initials of first and
middle name and full last name.
Example:
Input: Laxmi Narayan Sharma
Output: L N Sharma
Example:
Input: Akash Sharma
Output: A Sharma

import java.util.*;
class XP20
{//class definition starts here
public static void main()//program commands here{//main method starts
String name="",wd="",newname="";
char ch=' ';
int i=0,len=0;
Scanner sc=new Scanner(System.in);//creating an object for scanner
class
System.out.println("Enter full name:");
name=sc.nextLine();
name=name+" ";//adding a space in the beginning of the String
len=name.length();//storing the length of the String name into len
for(i=0;i<len;i++)
{
ch=name.charAt(i);
if(ch==' ')//checking whether ch is a space
{
if(i==(len-1))
newname=newname+wd;
else
newname=newname+wd.charAt(0)+" ";
wd="";
}
else
wd=wd+ch;
}
System.out.println("Name with first and middle name with initials and
full last name:"+ newname);
}//main method ends here
}//class definition ends here

54
Name Type Description
name String To store the input from
the user.
i int Loop variable.
wd String To store characters of a
word.
len int To store the length of
the String.
ch char To pick up and store a
character from the
String.
newname String To store the new name.

55
BIBLIOGRAPHY

I have taken all these information from my teacher Mr. Avishek Paul and my
classmate Aritra Chatterjee.

Following links has been used in the completion of this project are as follows:
 https://www.ilovepdf.com/word_to_pdf
 https://youtu.be/GWWMKnxdyss
 https://youtu.be/FDqR2yYd_MI

Following book is used to have an idea about the computer project:


 ICSE Computers Application Class 10

56
CONCLUSION

At last, I would like to share my experience while doing this project. I learnt
many new things about the given topic, Java Programming. The best thing
which I can share is that I developed more interest in this subject. This
project gave me real insight into the coding world.

A very special thanks to my dear Principal for setting such targets for us. I
enjoyed and every bit work I had put into this project.

Thank You.

57

You might also like