0% found this document useful (0 votes)
56 views

Computer Projectry

Uploaded by

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

Computer Projectry

Uploaded by

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

1

CONTENTS:-

1) Design a class name ShowRoom


2) Using the switch-case statement, write a menu driven program to Display
pattern or Unicode.
3) Write a program to input 15 integer elements in an array and sort them in
ascending order using the bubble sort technique.
4) Design a class to overload a function series( )
5) Write a program to input a sentence and convert it into uppercase and count and
display the total number of words starting with a letter 'A'.
6) Write a program to generate and print all four digits tech numbers.
7) Write a program of the billing system of A private Cab service company
8) Write a program to search for an integer value input by the user in the sorted list
given below using binary search technique
9) Write a program to input a sentence and convert it into uppercase and display each
word in a separate line
10) Design a class to overload a method Number( )
11) Write a menu driven program to find c or pattern.
12) Write a program to input and store integer elements in a double dimensional array of size 3
x 3 and find the sum of elements in the left diagonal.
13) Define a class to perform binary search on a list of integers given
14) Define a class to declare a character array of size ten. accept the characters into the array
and display the characters with highest and lowest ASCII value.
15) Define a class to declare an array of size twenty of double datatype, accept the elements into
the array .Calculate and print the product of all the elements.
16) Define a class to accept a string, and print the characters with the uppercase and lowercase
reversed, but all the other characters should remain the same as before.
17) Define a class to declare an array to accept and store ten words. Display only those words
which begin with the letter ‘A’ or ‘a’ and also end with the letter ‘A’ or ‘a’.
18) Define a class to accept two strings of same length and form a new word in such a way that,
the first character of the first word is followed by the first character of the second word and
so on.
19) Design a class and give student the allocated stream
20) Define a class to accept 10 characters from a user. Using bubble sort technique arrange them
in ascending order. Display the sorted array and original array
21) Define a class to overload the function print().
22) Define a class to accept a String and print the number of digits, alphabets and special
characters in the string.
23) Define a class to accept values into an array of double data type of size 20. Accept a double
value from user and search in the array using linear search method. If value is found
display message “Found” with its position where it is present in the array. Otherwise
display message “Not found”.
24) Define a class to accept values in integer array of size 10. Find sum of one digit number
and sum of two-digit numbers entered. Display them separately.
25) DTDC a courier company charges for the courier based on the weight of the parcel. Define a
class as per the billing method.
26) Define a class to overload the method perform()

2
27) Define a class to accept a number from user and check if it is an EvenPal number or not.
28) Define a class to accept values into an integer array of order 4 × 4 and check whether it is a
diagonal array or not
29) Define a class pincode and store the given pin codes in a single dimensional array. Sort these
pin codes in ascending order
30) Define a class to accept the Gmail ID and check for its validity.
31) Define a class Bank and calculate compound interest.
32) Perform a binary search on a list of numbers.
33) Defining a class to do certain operations on vowels.
34) Define a class to find row-wise sum of a 4 by 4 array.
35) Define a class to check whether a number is superspy number or not.
36) Overloading the function display( ).

1. 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 the mobile number of the customer
double cost - To store the cost of the items purchased
double dis - To store the discount amount
double amount - To store the amount to be paid after discount

Member methods:
ShowRoom( ) - default constructor to initialize data members
void input( ) - To input customer name, mobile number, cost
void calculate( ) - To calculate discount on the cost of purchased items,
based on following criteria:
Cost Discount
(in
percentage)
Less than or equal to ₹10000 5%
More than ₹10000 and less than or equal to 10%
₹20000
More than ₹20000 and less than or equal to 15%
₹35000
More than ₹35000 20%

3
void display( ) - To display customer name , mobile number , amount to
be paid
after discount.
Write a main method to create an object of the class and call the above
member
methods.

Program:

import java.util.*;
class ShowRoom
{
String name;
long mobno;
double cost;
double dis;
double amount;

ShowRoom()
{
name="";
mobno=0;
cost=0.0;
dis=0.0;
amount=0.0;
}
void input()
{
Scanner sc= new Scanner (System.in);
System.out.print("ENTER YOUR NAME:");
name=sc.nextLine();
System.out.print("ENTER YOUR MOBILE NUMBER:");
mobno=sc.nextLong();
System.out.print("ENTER YOUR COST:");
cost=sc.nextDouble();

4
}
void calculate()
{
if(cost <=10000)
dis=(0.05*cost);
else if (cost <=20000)
dis=(0.1*cost);
else if (cost <=35000)
dis=(0.15*cost);
else if (cost >35000)
dis=(0.2*cost);
}
void display()
{
System.out.println("NAME:" +name);
System.out.println("MOBILE NUMBER:" +mobno);
System.out.println("AMOUNT:" +(cost-dis));
}
public static void main(String args[])
{
ShowRoom ob=new ShowRoom();
ob.input();
ob.calculate();
ob.display();
}
}
Output:

5
Variable Description Box:
Variable Data Description
Name Type
name String Stores the name of the customer.
mobno long Stores the mobile number of the customer.
cost double Stores the cost of purchased items.
dis double Stores the discount percentage based on the
cost.
amount double Stores the total amount after applying the
discount.

2. Using the switch-case statement, write a menu driven program to do the


following:
(a) To generate and print Letters from A to Z and their Unicode Letters
Letter Unicode
A 65
B 66
. .
. .
. .
Z 90

(b) Display the following pattern using iteration (looping) statement:


1
1 2
1 2 3
1 2 3 4

6
1 2 3 4 5

Program:
import java.util.*;
class switchcase
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("ENTER 1 FOR UNICODE");
System.out.println("ENTER 2 FOR PATTERN");
System.out.print("ENTER YOUR CHOICE");
int ch=sc.nextInt();

switch (ch)
{
case 1:
System.out.println("LETTERS \t UNICODE");
for (int i=65;i<=90;i++)
{
System.out.println(" "+(char)i+"\t\t"+ " " +i);
}break;

case 2:

7
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}break;

default:
{
System.out.print("WRONG CHOICE");
}
}
}
}

Output:

8
Variable Description Box:

Variable Data Description


Name Type
ch int Stores the user's choice from the
menu
i int Loop variable for iteration
j int Loop variable for nested iteration
3. Write a program to input 15 integer elements in an array and sort them
in ascending order using the bubble sort technique.

Program:
import java.util.*;
class bubble_sort
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int a[]=new int[15];
System.out.print("ENTER YOUR NUMBERS : ");
for (int i=0;i<15;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<15;i++)
{
for(int j=0;j<(14-i);j++)
{
if (a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

9
}
}
System.out.print("SORTED ARRAY: ");
for(int i=0;i<15;i++)
{
System.out.println(a[i]);
}
}
}Output:

10
Variable Description Box:

11
Variable Data Description
Name Type
a int Array to store integers
i int Loop counter for outer loop
j int Loop counter for inner loop
temp int Temporary variable for
swapping

4. Design a class to overload a function series( ) as follows:

(a) void series(int x, int n) – To display the sum of the series

given below:

x1 + x2 + x3 + ................. xn terms

(b) void series(int p) – To display the following

series:

0, 7, 26, 63 ..............p terms

(c) void series( ) – To display the sum of the series given below:

Program:

import java.util.*;
class series
{
Scanner sc=new Scanner (System.in);
void series(int x,int n)
{ double sum=0;
for( int i=1;i<=n;i++)

12
{
sum=sum+(Math.pow(x,n));
}System.out.println("SUM="+sum);
}
void series(int p)
{
for (int i=1;i<=p;i++)
{
int x=(int)Math.pow(i,3)-1;
System.out.print(x +" ");
}
}
void series()
{ double sum=0;
for(double i=2;i<=10;i++)
{
sum=sum+1.0/i;
}System.out.print("SUM="+sum);
}
}Output:

Variable Description Box:


Variable Data Description
Name Type
x int Represents the base value for the series.
n int Indicates the number of terms in the series for
method series(int x, int n).
p int Denotes the upper limit for the series
calculation in method series(int p).
sum double Stores the cumulative sum of series elements.

13
i int Loop variable for iterating through series
elements.

5. Write a program to input a sentence and convert it into uppercase

and count and display the total number of words starting with a letter
'A'.

Example:

Sample Input: ADVANCEMENT AND APPLICATION OF


INFORMATION TECHNOLOGY ARE EVER CHANGING.
Sample Output: Total number of words starting with letter 'A' = 4.

Program:

import java.util.*;
public class WordsWithLetterA
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = sc.nextLine();
str = " " + str;
int c = 0;
int len = str.length();
str = str.toUpperCase();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
c++;
}
System.out.println("Total number of words starting with letter 'A' =
" + c);
}
}

14
Output:

Variable Description Box:


Variable Data Description
Name Type
st String Stores the input sentence.
c int Counter variable to count the number of words
starting with the letter 'A'.
i int Loop variable for iterating through the characters of
the input sentence.
len int Stores length of string

6. A tech number has even number of digits. If the number is split in two

equal halves, then the square of sum of these halves is equal to the
number itself. Write a program to generate and print all four digits tech
numbers.

Example:

Consider the number 3025


Square of sum of the halves of 3025 = (30+25)2

= (55)2
= 3025 is a tech number.

Program:

import java.util.*;

15
class tech_no
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER YOUR FOUR DIGIT NUMBER:");
int n=sc.nextInt();
int f=n/100;
int s=n%100;
if (Math.pow((f+s),2)==n)
{
System.out.print("TECH NUMBER");
}
else
System.out.print("NOT TECH NUMBER");
}
}

Output:

Variable Description Box:


Variable Data Description
Name Type
n int Stores the numbers being checked for being
a tech number.
f int Stores the first half of the digits of the
current number.
s int Stores the second half of the digits of the
current number.

16
7. A private Cab service company provides service within the city at the
following rates:

AC CAR NON AC CAR


UPTO 5 KM ₹ 150 /- ₹ 120 /-
BEYOND 5 KM ₹ 10/-PER KM ₹ 08/- PER KM
Design a class CabService with the following description:
Member variables /data members:

String car_type - To store the type of car (AC or NON AC)


double km - To store the kilometer travelled
double bill - To calculate and store the bill amount
Member methods :

CabService( ) - Default constructor to initialize data members.


String data members to '' '' and

double data members to 0.0.

void accept ( ) - To accept car_type and km (using


Scanner class only).
void calculate ( ) - To calculate the bill as per the rules given above.
void display( ) - To display the bill as per the following format
CAR TYPE:
KILOMETER

TRAVELLED:

TOTAL BILL:

Create an object of the class in the main method and invoke the member
methods.

Program:

17
import java.util.*;
public class CabService
{
String car_type;
double km;
double bill;

public CabService() {
car_type = "";
km = 0.0;
bill = 0.0;
}

public void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter car type: ");
car_type = in.nextLine();
System.out.print("Enter kilometer: ");
km = in.nextDouble();
}
public void calculate() {
if (km <= 5) {
if (car_type.equals("AC" ) ||car_type.equals("ac" ) )
bill = 150;
else
bill = 120;
}
else {
if (car_type.equals("AC")|| car_type.equals("ac" ))
bill = 150 + 10 * (km - 5);
else
bill = 120 + 8 * (km - 5);
}
}

18
public void display() {
System.out.println("Car Type: " + car_type);
System.out.println("Kilometer Travelled: " + km);
System.out.println("Total Bill: " + bill);
}

public static void main(String args[]) {


CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}

Output:

Variable Description Box:

Variable Data Description


Name Type
car_type String Stores the type of car (AC or NON AC)
chosen by the user.
km double Stores the distance traveled in
kilometers.
bill double Stores the calculated bill amount for
the cab service.
8. Write a program to search for an integer value input by the user in the

sorted list given below using binary search technique. If found display

19
''Search Successful'' and print the element, otherwise display ''Search
Unsuccessful''

{31, 36, 45, 50, 60, 75, 86, 90}

Program:

import java.util.*;
public class BinarySearch
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = {31, 36, 45, 50, 60, 75, 86, 90};

System.out.print("Enter number to search: ");


int n = in.nextInt();

int l = 0, h = arr.length - 1, index = -1;


while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
else if (arr[m] > n)
h = m - 1;
else {
index = m;
break;
}

if (index == -1) {
System.out.println("Search element not found");
}

20
else {
System.out.println(n + " found at position " + index);
}
}
}
Output:

Variable Description Box:

Variable Data Description


Name Type
n int Stores the number to be searched.
arr int Array containing the elements to be
searched.
l int Represents the lower index boundary for
binary search.
h int Represents the upper index boundary for
binary search.
index int Counter variable to track if the search was
successful.
m int Stores the middle index during each
iteration of the binary search.
9. Write a program to input a sentence and convert it into uppercase and
display each word in a separate line.
Example:
Input : India is my country
Output : INDIA
IS MY
COUNTRY

Program:

21
import java.util.*;
public class Words
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

str += " ";


int len = str.length();

String word = "";

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (ch != ' ') {
word = word + ch;

}
else {
System.out.println(word + "\t" );
word = "";

}
}
}
}
Output:

22
Variable Description Box:

Variable Data Description


Name Type
str String Stores the input sentence.
len int Stores the length of the input sentence.
word String Displays each word in different lines
ch char Stores the current character being examined.

10. Design a class to overload a method Number( ) as follows:


(i) void Number (int num , int d)-To count and display the frequency
of a digit in a number.
Example:
num = 2565685
d=5
Frequency of digit 5 = 3
(ii) void Number (int n1)-To find and display the sum of even digits of
a number.
Example:
n1 = 29865
Sum of even digits = 16
Write a main method to create an object and invoke the above
methods.

Program:

import java.util.*;
public class NumberOverload
{

23
void Number(int num, int d)
{
int c=0;
while (num>0)
{
if(num%10==d)
c++;
num/=10;
}
System.out.println("Frequency of digit "+ d +" = "+ c);
}
void Number(int n1)
{
int sum=0;
while (n1>0)
{
int d = n1%10;
if (d%2==0)
sum+=d;
n1/=10;
}
System.out.println("Sum of even digits = "+ sum);
}
public static void main(String args[])
{
NumberOverload ob = new NumberOverload();
Scanner sc = new Scanner(System.in);
int num, d;
System.out.print("Enter number and digit: ");
num = sc.nextInt();
d = sc.nextInt();
ob.Number(num, d);
int n1;
System.out.print("Enter number: ");
n1 = sc.nextInt();
ob.Number(n1);

24
}
}

Output:

Variable Description Box:

Variable Data Type Description


Name
num int Stores the number for digit frequency
counting.
d int Stores the digit to count its frequency.
c int Counter variable to count the frequency
of the digit in the first overload method.
n1 int Stores the number for summing even
digits in the second overload method.
sum int Stores the sum of even digits in the
second overload method.

11. Write a menu driven program to perform the following operations as


per user’s choice:
(i) To print the value of c=a2+2ab, where a varies from 1.0 to 20.0
with increment of 2.0 and b=3.0 is a constant.
(ii) To display the following pattern using for loop:
A
AB
ABC

25
ABCD
ABCDE
Display proper message for an invalid choice.
Program:

import java.util.*;

public class Menu


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Value of c");
System.out.println("Type 2 for pattern");
System.out.print("Enter your choice: ");

int choice = in.nextInt();

switch (choice) {
case 1:
final double b = 3.0;
for (int a = 1; a <= 20; a += 2) {
double c = Math.pow(a, 2) + 2 * a * b;
System.out.println("Value of c when a is "
+ a + " = " + c);
}
break;

case 2:
for (char i = 'A'; i <= 'E'; i++) {
for (char j = 'A'; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
break;

26
default:
System.out.println("INVALID CHOICE");
}
}
}
Output:

Variable Description Box:

Variable Data Description


Name Type
ch int Stores the user's choice for menu selection.
a double Stores the value of 'a' used in the calculation
of 'c' in the first menu option.
b double Constant value used in the calculation of 'c' in
the first menu option.
c double Stores the result of the expression 'c = a^2 +
2ab' in the first menu option.
i int Loop variable for the outer loop in the second
menu option.
j int Loop variable for the inner loop in the second
menu option.

27
12. Write a program to input and store integer elements in a double
dimensional array of size 3 x 3 and find the sum of elements in the left
diagonal.
Example:
1 3 5
4 6 8
9 2 4
Output: Sum of the left diagonal elements = (1 + 6 +4) = 11

Program:

import java.util.Scanner;
public class sum_of_left
{
public static void main(String[ ] args)
{
int[ ][ ] array = new int[3][3];
int sumLeft = 0, length = array[0].length;
System.out.println("Enter values in the array row by row");
for (int row = 0; row < length; row++)
for (int column = 0; column < length; column++)
array [row][column] = new Scanner(System.in).nextInt( );
System.out.println( );
for (int[ ] row : array)
{
for (int element : row)
System.out.print(element + " ");
System.out.println( );
}
System.out.println( );
for (int i = 0; i < length; i++)
{
sumLeft += array[i][i];
}
System.out.printf("Left Diagonal sum - %d", sumLeft);
}

28
}Output:

Variable Description Box:

Variable Data Description


Name Type
array int 2D array to store the nine numbers
entered by the user.
sumLeft int Stores the sum of the elements along the
left diagonal of the array.
row int Loop variable for the rows of the 2D
array.
column int Loop variable for the columns of the 2D
array.
length int Stores length of the array
13.
Define a class to perform binary search on a list of integers given
below, to search for an element input by the user, if it is found display

29
the element along with its position, otherwise display the message
“Search element not found”.

2, 5, 7,10,15, 20, 29, 30, 46, 50

Program:

import java.util.*;

public class BinarySearch2


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = {2, 5, 7, 10, 15, 20, 29, 30, 46, 50};

System.out.print("Enter number to search: ");


int n = in.nextInt();

int l = 0, h = arr.length - 1, index = -1;


while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
else if (arr[m] > n)
h = m - 1;
else {
index = m;
break;
}

if (index == -1) {
System.out.println("Search element not found");
}

30
else {
System.out.println(n + " found at position " + index);
}
}
}
Output:

Variable Description Box:

Variable Data Description


Name Type
n int Stores the number to be searched.
arr int[] Array containing the elements to be
searched.
l int Represents the lower index
boundary for binary search.
h int Represents the upper index
boundary for binary search.
index int Counter variable to track if the
search was successful.
m int Stores the middle index during each
iteration of the binary search.
14. Define a class to declare a character array of size ten. accept the
characters into the array and display the characters with highest and
lowest ASCII (American Standard Code for Information Interchange)

31
value.
EXAMPLE :
INPUT:
‘R’, ‘z’, ‘q’, ‘A’, ‘N’, ‘p’, ‘m’, ‘U’, ‘Q’,
‘F’ OUTPUT :
Character with highest ASCII value = z
Character with lowest ASCII value = A
Program:

public class ASCIIVal


{

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
char ch[] = new char[10];
int len = ch.length;

System.out.println("Enter 10 characters:");
for (int i = 0; i < len; i++)
{
ch[i] = in.nextLine().charAt(0);
}

char h = ch[0];
char l = ch[0];

for (int i = 1; i < len; i++)


{
if (ch[i] > h)
{
h = ch[i];
}

32
if (ch[i] < l)
{
l = ch[i];
}
}

System.out.println("Character with highest ASCII value: " + h);


System.out.println("Character with lowest ASCII value: " + l);
}

}
Output:

Variable Description Box:

Variable Data Description


Name Type
ch char[] Array to store ten characters entered by the
user.
h int Stores the ASCII value of the character with
the highest value.

33
l int Stores the ASCII value of the character with
the lowest value.
i int Loop variable for reading characters into the
array.
len int Stores length of array

15. Define a class to declare an array of size twenty of double datatype,


accept the elements into the array and perform the following :

Calculate and print the product of all the elements.

Print the square of each element of the array.


Program:

import java.util.*;
class DoubleArray
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double[] ar = new double[20];
System.out.println("Enter 20 double values:");
for (int i = 0; i < 20; i++)
{
ar[i] = sc.nextDouble();
}

double pro = 1.0;


for (int i = 0; i < 20; i++)
{
pro*= ar[i];
}
System.out.println("Product of all elements: " + pro);

System.out.println("Squares of each element:");

34
for (int i = 0; i < 20; i++)
{
double sq = ar[i] * ar[i];
System.out.println("Square of "+ar[i]+" : " + sq);
}
}
}

Output:

Variable Description Box:

Variable Data Description


Name Type
ar double Array to store 20 double values entered by the
user.

35
pro double Stores the product of all elements in the array.
sq double Stores the square of each element in the array.
i int Loop variable for reading input values and
iterating through the array.

16. Define a class to accept a string, and print the characters with the
uppercase and lowercase reversed, but all the other characters should
remain the same as before.

EXAMPLE:

INPUT : WelCoMe_2022

OUTPUT :

wELcOmE_2022

Program:

import java.util.Scanner;

public class ChangeCase


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();
int len = str.length();
String rev = "";

for (int i = 0; i < len; i++)


{
char ch = str.charAt(i);

36
if (Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
{
rev += Character.toLowerCase(ch);
}
else
{
rev += Character.toUpperCase(ch);
}
}
else
{
rev += ch;
}
}

System.out.println(rev);

}
}
Output:

Variable Description Box:


Variable Name Data type Description
str String To store the string
len Int To store the string
length
rev String To store the reversed
string
ch char to cut the digits from
the string

37
i int Loop variable for
checking.

17. Define a class to declare an array to accept and store ten words. Display
only those words which begin with the letter ‘A’ or ‘a’ and also end with
the letter ‘A’ or ‘a’.
EXAMPLE :
Input : Hari, Anita, Akash, Amrita, Alina, Devi Rishab, John, Farha,
AMITHA
Output:
Anita
Amrita
Alina
AMITHA
Program:

import java.util.Scanner;

public class Words2


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String names[] = new String[10];
int l = names.length;
System.out.println("Enter 10 names : ");

for (int i = 0; i < l; i++)


{
names[i] = in.nextLine();
}

38
System.out.println("Names that begin and end with letter A are:");

for(int i = 0; i < l; i++)


{
String str = names[i];
int len = str.length();
char begin = Character.toUpperCase(str.charAt(0));
char end = Character.toUpperCase(str.charAt(len - 1));
if (begin == 'A' && end == 'A') {
System.out.println(str);
}
}
}
}
Output:

Variable Description Box:


Variable name Data type Description
names String Array to store the
names

39
l int To store the string
length
begin char To store the first
string character
end char To store the last
string character
I int Loop variable for
checking.

18. Define a class to accept two strings of same length and form a new
word in such a way that, the first character of the first word is followed
by the first character of the second word and so on.
Example :
Input string 1 – BALL
Input string 2 – WORD

OUTPUT : BWAOLRLD
Program:

import java.util.*;
public class StringMerge
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter String 1: ");
String s1 = in.nextLine();
System.out.println("Enter String 2: ");
String s2 = in.nextLine();
String str = "";
int len = s1.length();

if(s2.length() == len)
{

40
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same length");
}
}
}
Output:

Variable Description Box:


Variable name Data type Description
s1 String To store the first word
s2 String To store the second
word
str String To store the final string
len int To store the length of
the string 1
ch1 String To cut the characters of
string 1
ch2 String To cut the characters of
string 2

41
19. Design a class with the following specifications:

Class name: Student


Member variables: name – name of student age – age of student
mks –marks obtained
stream – stream allocated
(Declare the variables using appropriate data types)
Member methods:
void accept() – Accept name, age and marks using methods of
Scanner class. void allocation() – Allocate the stream as per following
criteria:

mks stream
> = 300 Science and Computer
> = 200 and < 300 Commerce and Computer
> = 75 and 200 Arts and Animation
< 75 Try Again

void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
Program:

import java.util.*;
public class Student
{
private String name;
private int age;
private double mks;
private String stream;

public void accept()


{

42
Scanner in = new Scanner(System.in);
System.out.print("Enter student name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter marks: ");
mks = in.nextDouble();
}

public void allocation()


{
if (mks < 75)
stream = "Try again";
else if (mks < 200)
stream = "Arts and Animation";
else if (mks < 300)
stream = "Commerce and Computer";
else
stream = "Science and Computer";
}

public void print()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + mks);
System.out.println("Stream allocated: " + stream);
}

public static void main(String args[]) {


Student obj = new Student();
obj.accept();
obj.allocation();
obj.print();
}
}

43
Output:

Variable Description Box:


Variable name Data type Description
name string To store the name
age int To store the age
mks double To store the marks
stream String To store the assigned
stream

20. Define a class to accept 10 characters from a user. Using bubble sort
technique arrange them in ascending order. Display the sorted array
and original array.
Program:

import java.util.*;
public class CharBubbleSort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
char ch[] = new char[10];
System.out.println("Enter 10 characters:");
for (int i = 0; i < ch.length; i++) {
ch[i] = in.nextLine().charAt(0);
}

44
System.out.println("Original Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}

//Bubble Sort
for (int i = 0; i < ch.length - 1; i++) {
for (int j = 0; j < ch.length - 1 - i; j++) {
if (ch[j] > (ch[j + 1])) {
char t = ch[j];
ch[j] = ch[j + 1];
ch[j + 1] = t;
}
}
}

System.out.println("\nSorted Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
}
}

Output:

45
Variable Description Box:
Variable name Data type Description
ch char Helps to input
characters in array
i int For storing each
character in the
array
j int To perform bubble
sort

21. Define a class to overload the function print as follows:

void print() – to print the following format


1 1 1 1
2 2 2 2
3 3 3 3

46
4 4 4 4
5 5 5 5
void print(int n) – To check whether the number is a lead number. A
lead number is the one whose sum of even digits are equal to sum of
odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.

Program:

import java.util.*;

public class MethodOverload2


{
public void print()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}

public void print(int n)


{
int d = 0;
int evenSum = 0;
int oddSum = 0;

47
while( n != 0)
{
d = n % 10;
if (d % 2 == 0)
evenSum += d;
else
oddSum += d;
n = n / 10;
}

if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Not a lead number");
}

public static void main(String args[])


{
MethodOverload2 obj = new MethodOverload2();
Scanner in = new Scanner(System.in);

System.out.println("Pattern: ");
obj.print();

System.out.print("Enter a number: ");


int num = in.nextInt();
obj.print(num);
}
}
Output:

48
Variable Description Box:
Variable name Data type Description
i int To display the rows
j int To display the
columns
d int For digit cutting
evenSum int To store sum of even
digits
oddSum int To store sum of odd
digits

22. Define a class to accept a String and print the number of digits,
alphabets and special characters in the string.
Example:
S = “KAPILDEV@83”
Output: Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1
Program:

import java.util.*;

49
public class Count
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();

int len = str.length();

int ac = 0;
int sc = 0;
int dc = 0;
char ch;

for (int i = 0; i < len; i++) {


ch = str.charAt(i);
if (Character.isLetter(ch))
ac++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
}

System.out.println("No. of Digits = " + dc);


System.out.println("No. of Alphabets = " + ac);
System.out.println("No. of Special Characters = " + sc);

}
}
Output:

50
Variable Description Box:
Variable name Data type description
str String To store the string
len int To store the length
of the string
ac int Stores the number of
alphabets
sc int Stores the number of
special characters
dc int Stores the number of
digits
ch char For character
cutting

23. Define a class to accept values into an array of double data type of size
20. Accept a double value from user and search in the array using linear
search method. If value is found display message “Found” with its
position where it is present in the array. Otherwise display message
“Not found”.

Program:

import java.util.*;

public class LinearSearch


{

51
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

double arr[] = new double[20];


int l = arr.length;
int i = 0;

System.out.println("Enter array elements: ");


for (i = 0; i < l; i++)
{
arr[i] = in.nextDouble();
}

System.out.print("Enter the number to search: ");


double n = in.nextDouble();

for (i = 0; i < l; i++)


{
if (arr[i] == n)
{
break;
}
}

if (i == l)
{
System.out.println("Not found");
}
else
{
System.out.println(n + " found at index " + i);
}
}
}
Output:

52
Variable Description Box:
Variable name Data type Description
arr double To create array of
size 20
l int For array length
i int To store each
element in the array
one by one
n double To store the number
to be searched

53
24. Define a class to accept values in integer array of size 10. Find sum of
one digit number and sum of two digit numbers entered. Display them
separately.
Example:
Input: a[ ] = {2, 12, 4, 9, 18, 25, 3, 32, 20, 1}
Output: Sum of one digit numbers : 2 + 4 + 9 + 3 + 1 = 19
Sum of two digit numbers : 12 + 18 + 25 + 32 + 20 = 107
Program:

import java.util.*;
class DigitSumArray
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("Enter ten numbers: ");
int ar[] = new int[10];
for(int i=0 ; i<10; i++)
{
ar[i] = sc.nextInt();
}
int s1=0, s2=0;
for(int j=0; j<10; j++)
{
if(ar[j]>=0&&ar[j]<10)
s1+=ar[j];
else if(ar[j]>=10&&ar[j]<100)
s2+=ar[j];
}
System.out.println("Sum of one digit number: "+s1);
System.out.println("Sum of two digit number: "+s2);
}
}

54
Output:

Variable Description Box:

Variable name Data type description


ar int To create an array of
size 10
i int To store each element
of array one by one
s1 int To store sum of one
digit numbers
s2 int To store sum of two
digit numbers
j int To check whether
each element is one or
two digit
consecutively

25. DTDC a courier company charges for the courier based on the weight of
the parcel. Define a class with the following specifications:

55
class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – ‘D’ – domestic, ‘I’ international
Member methods:
void accept() – to accept the details using the methods of the Scanner
class only.
void calculate() – to calculate the bill as per the following criteria:

Weight in Kgs Rate per Kg


First 5 Kgs Rs. 800
Next 5 Kgs Rs. 700
Above 10 Kgs Rs. 500

An additional amount of Rs. 1500 is charged if the type of the courier is I


(International).
void print() – To print the details
void main() – to create an object of the class and invoke the methods.
Program:

import java.util.*;
class courier
{
String name;
double weight;

56
String address;
double bill;
char type;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Name of the customer: ");
name = sc.nextLine();
System.out.print("Weight of parcel in kilograms: ");
weight = Double.parseDouble(sc.nextLine());
System.out.print("Address of the recipient: ");
address = sc.nextLine();
System.out.print("Type of parcel: ('D'for Domestic, 'I'for
International)");
type = sc.next().charAt(0);
}
void calculate()
{
if(weight <= 5)
bill = weight * 800;
else if(weight <= 10)
bill = 4000 + (weight - 5) * 700;
else
bill = 7500 + (weight - 10) * 500;

if(type == 'I')
bill += 1500;
}
void print()
{
System.out.println("Name of the customer: "+name);
System.out.println("Weight of parcel in kilograms: "+weight);
System.out.println("Address of the recipient: "+address);
System.out.println("Type of parcel: ");
if(type=='I')
System.out.println("International");

57
else
System.out.println("Domestic");
System.out.println("Amount to be paid: "+ bill);
}
public static void main(String args[])
{
courier obj = new courier();
obj.accept();
obj.calculate();
obj.print();
}
}

Output:

Variable Description Box:


Variable name Data type description
name String To store name
weight double To store weight
address String To store the address
bill double To store the final
amount to be paid
type char To store type i.e
international or
domestic

58
26. Define a class to overload the method perform() as follows:

double perform(double r, double h) – to calculate and return the


value of curved surface area of
cone.

void perform(int r, int c) – use nested for loop to generate the


following format:
r = 4, c = 5
Output:
12345
12345
12345
12345
void perform(int m, int n, char ch) – to print the quotient of the
division of m and n if ch is Q else
print the remainder of the division
of m and n if ch is R.

Program:

class PerformOverload
{
double perform(double r, double h)
{
double l = Math.sqrt(r * r + h * h);
double CSA = (22 * r * l)/7;
return CSA;
}

59
void perform(int r, int c)
{
for(int i = 1; i <= r; i++){
for(int j = 1; j <= 5; j++){
System.out.print(j + " ");
}
System.out.println();
}
}
void perform(int m, int n, char ch)
{
if(ch == 'Q')
System.out.println("Quotient: " + (m / n));
else if(ch == 'R')
System.out.println("Remainder: " + (m % n));
else
System.out.println("CONDITIONS NOT FOLLOWED");
}
}
Output:

Variable Description Box:

Variable name Data type description


r double To store radius
h double To store height
l double To find slant height of
cone
CSA double To store the curved
surface area of cone

60
i int To initiate rows
j int To initiate columns
m int To store the dividend
n int To store the divisor
ch char To store the choice

27. Define a class to accept a number from user and check if it is an EvenPal
number or not. The number is said to be EvenPal number when number
is palindrome number (a number is palindrome if it is equal to its
reverse) and sum of its digits is an even number.
Example: 121 is a palindrome number.
Sum of the digits = 1 + 2 + 1 = 4 which is even number.
Program:

import java.util.*;
class EvenPal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
int d=0, r=0, s=0, l=n;
while(l>0)
{
d=l%10;
r=r*10+d;
s+=d;
l=l/10;
}
if(n==r && s%2==0)
System.out.println("EvenPal number");

61
else
System.out.println("Not an EvenPal number.");
}
}

Output:

Variable Description Box:


Variable name Data type description
n int To store a number
d int For digit cutting
r int For storing the
reversed number
s int To store sum of
digits
l int To produce a copy
of n

28. Define a class to accept values into an integer array of order 4 × 4 and
check whether it is a diagonal array or not. An array is diagonal if the
sum of the left diagonal elements equals the sum of the right diagonal
elements. Print the appropriate message.
Example:
3425
2523
5327
1371
Sum of the left diagonal elements = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal elements = 5 + 2 + 3 + 1 = 11

62
Program:

import java.util.*;
class DiagonalArray
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int k[][]= new int[4][4];
int sl=0, sr=0;
System.out.println("Enter numbers in the array:");
for(int i =0; i<4; i++)
{
for(int j=0; j<4; j++)
{
k[i][j]=sc.nextInt();
if(i==j)
sl+=k[i][j];
if((i+j)==3)
sr+=k[i][j];
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
System.out.print(k[i][j]);
System.out.println();
}
if(sl==sr)
System.out.println("DIAGONAL Array");
else
System.out.println("Not DIAGONAL Array");
}
}

63
Output:

Variable Description Box:

Variable name Data type description


k int To create a double
dimensional array
of size 4 by 4
sl int To store the sum of
the left diagonal
sr int To store the sum of
right diagonal

64
i int To store elements in
rows
j int To store elements in
columns

29. Define a class pincode and store the given pin codes in a single
dimensional array. Sort these pin codes in ascending order using the
selection sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
Program:

import java.util.*;
class pincode
{
public static void main(String args[])
{
int a[] = {110061, 110001, 110029, 110023, 110055, 110006,
110019, 110033};
for(int i=0; i<8; i++)
{
for(int j=i+1;j<8; j++)
{
if(a[i]>a[j])
{
int k=a[j];
a[j]=a[i];
a[i]=k;
}
}
}
for(int i=0; i<8; i++)
System.out.println(a[i]);

65
}
}

Output:

Variable Description Box:

Variable name Data type description


a int To create the array
i int To access the
elements of array
j int To extract array
elements and
compare to the
other elements
k int Helps in sorting

30. Define a class to accept the Gmail ID and check for its validity.

A Gmail ID is valid only if it has:


→@
→ . (dot)
→ gmail
→ com

66
Example: [email protected] is a valid Gmail ID.

Program:

import java.util.*;
class Gmail{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Gmail ID: ");
String st = sc.nextLine();
String st1 = "@gmail.com", st2;
int k = st.length();
st2= st.substring(k-10);
boolean n = st1.equals(st2);
if(n && st.charAt(0)!='@')
System.out.println("Valid gmail id");
else
System.out.println("Invalid gmail id");
}
}

Output:

Variable Description Box:


Variable name Data type description
st String To store the gmail id
st1 String To store
“@gmail.com”
k int To store length of
gmail

67
st2 String To store the
substring
n boolean To check whether
st1 is equal to st2

31. Define a class with the following specifications:


Class name: Bank
Member variables:
double p – stores the principal amount
double n – stores the time period in years
double r – stores the rate of interest
double a – stores the amount

Member methods:
void accept() – input values for p and n using Scanner class methods only.
void calculate() – calculate the amount based on the following conditions:
Time in (Years) Rate %
Up to 1/2 9
> 1/2 to 1 year 10

68
> 1 to 3 years 11
> 3 years 12

void display() – displays the details in the given format:


Principal Time Rate Amount
xxx xxx xxx xxx
Write the main() method to create an object and call the above methods.

Program:
import java.util.*;
class Bank
{ Scanner sc=new Scanner (System.in);
double p,n,r,a;
void accept()
{
System.out.println("ENTER THE PRINCIPAL");
p=sc.nextDouble();
System.out.println("ENTER THE TIME PERIOD");
n=sc.nextDouble();
}

void calculate()
{
if(n<=0.5)
{ r=9;

}
if(n>=0.5&&n<=1)
{ r=10;

}
if(n>=0&&n<=3)
{ r=11;

69
}
if(n>3)
{ r=12;

}
a=p*(Math.pow((1+ r/100),n));
}

void display()
{
System.out.println("Principal\t Time\t Rate\t Amount \t");
System.out.println(p+"\t\t"+n+"\t"+r+"\t"+a);
}
public static void main()
{
Bank ob=new Bank();
ob.accept();
ob.calculate();
ob.display();
}
}

Output:

Variable Description Box:


Variable name Data type Description
p double stores the principal
amount

70
n double stores the time period
in years
r double stores the rate of
interest
a double stores the amount

32. Define a class to search for a value input by the user from the list of
values given below. If it is found display the message “Search successful”,
otherwise display the message “Search element not found” using Binary
search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.

Program:
class Binary_Search
{
public static void main(double n)
{
double a[]={5.6,11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5};
int l =0;
int u=9;

int flag=0;
while(l<=u)
{int mid=l+u-1 /2;
if (a[mid]==n)
{
flag=1;
break;

}
else if (a[mid]<n)
{
l=mid+1;
}
else if(a[mid]>n)
{

71
u=mid-1;
}
}

if (flag==1)
{
System.out.println("Search successful");
}
else
System.out.println("Search element not found");

}
}

Output:

Variable Description box:


Variable Name Data Type Description
n double To store the number
to be searched
a double To store the array of
numbers
l int To store upper limit
for binary search
u int To store lower limit
for binary search
flag int To detect the
presence of the
entered number

72
mid int To store the mid value
of the upper and
lower limits

33. Define a class to accept a string and convert the same to uppercase.
Create and display the new string by replacing each vowel by immediate
next character and every consonant by the previous character. The other
characters remain the same.
Example:
Input: #IMAGINATION@2024
Output: #JLBFJMBSJPM@2024

Program:
import java.util.*;
class vowel
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE STRING");
String s=sc.next();
String s1=s.toUpperCase();
String finale="";

for(int i=0;i<s1.length();i++)
{ char ch=s1.charAt(i);
if(Character.isLetter(ch))
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
finale=finale+(char)(int)(ch+1);
}
else
finale=finale+(char)(int)(ch-1);
}
else
finale=finale+ch;

73
}
System.out.print(finale);
}
}

Output:

Variable Description Box:


Variable Name Data type Description
s String Stores the string
s1 String Stores the string in
upper case
finale String Stores the final string
i int Looping variable for
conversion
ch char Extracts each
character of the string

34. Define a class to accept values into 4 × 4 array and find and display the
sum of each row.
Example:
A[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {1, 3, 5, 7}, {2, 5, 3, 1}};
Output:
sum of row 1 = 10 (1 + 2 + 3 + 4)
sum of row 2 = 26 (5 + 6 + 7 + 8)
sum of row 3 = 16 (1 + 3 + 5 + 7)
sum of row 4 = 11 (2 + 5 + 3 + 1)

Program:

74
import java.util.*;
class fourbyfour
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A 4 BY 4 ARRAY");
int a[][]=new int[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}

for(int i=0;i<4;i++)
{ int sum=0;
for(int j=0;j<4;j++)
{
sum=sum+a[i][j];
}
System.out.println("SUM OF"+(i+1)+" ROW IS=" +sum);
}

}
}

Output:

75
Variable Description Box:
Variable name Data type Description
a int Array to accept
numbers
i int Outer looping variable
to access array
j int Inner looping variable
to access array
sum int To store the sum of
rows

76
35. Define a class to accept a number and check whether it is a SUPERSPY
number or not. A number is called SUPERSPY if the sum of the digits equals
the number of the digits.
Example 1:
Input: 1021
Output: SUPERSPY number [SUM OF THE DIGITS = 1 + 0 + 2 + 1 = 4, NUMBER
OF DIGITS = 4]
Example 2:
Input: 125
Output: Not a SUPERSPY number [1 + 2 + 5 is not equal to 3]

Program:
import java.util.*;
class Superspy
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int n=sc.nextInt();
int m=n;
int sum=0;
int count=0;
while(m!=0)
{
int d=m%10;
sum=sum+d;
count++;
m=m/10;
}
if(count ==sum)
System.out.println("SUPERSPY NUMBER");
else
System.out.println("NOT A SUPERSPY NUMBER");
}
}

77
Output:

Variable Description Box:

Variable name Data type Output


n int To store the number
from user
m int To create a copy of n
sum int For finding sum of
digits
count int For counting number
of digits
d int For digit cutting

36. Define a class to overload the method display() as follows:


void display(): To print the following format using nested loop.
12121
12121
12121
void display(int n, int m): To print the quotient of the division of m and n if m
is greater than n otherwise print the sum of twice n and thrice m.
double display(double a, double b, double c): to print the value of z where z =
p×q
p = (a + b) / c
q=a+b+c

Program:
class overload

78
{
void display()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=5;j++)
{
if(j%2==0)
System.out.print("2 ");
else
System.out.print("1 ");

}System.out.println();
}
}

void display(int n,int m)


{
if(m>n)
{
System.out.println((m/n));

}
else
System.out.println((2*n +3*m));
}

double display(double a,double b,double c)


{
double p=((a+b)/c);
double q=a+b+c;
double z=p*q;
return z;
}
}

79
Output:

Variable Description Box:


Variable name Data type Description
i int Outer looping variable
j int Inner looping variable
n int Stores the value of n
m int Stores the value of m
a double Stores value of a
b double Stores value of b
c double Stores value of c
p double Stores the sum of a
and b divided by c
q double Stores sum of a, b, c
z double Stores the product of
p and q

SIGNATURES:-

80
EXTERNAL EXAMINER
--------------------------------------------------

INTERNAL EXAMINER
--------------------------------------------------

81
82

You might also like