Computer Projectry
Computer Projectry
CONTENTS:-
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( ).
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.
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:
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
given below:
x1 + x2 + x3 + ................. xn terms
series:
(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:
13
i int Loop variable for iterating through series
elements.
and count and display the total number of words starting with a letter
'A'.
Example:
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:
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:
= (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:
16
7. A private Cab service company provides service within the city at the
following rates:
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;
}
18
public void display() {
System.out.println("Car Type: " + car_type);
System.out.println("Kilometer Travelled: " + km);
System.out.println("Total Bill: " + bill);
}
Output:
sorted list given below using binary search technique. If found display
19
''Search Successful'' and print the element, otherwise display ''Search
Unsuccessful''
Program:
import java.util.*;
public class BinarySearch
{
public static void main(String args[]) {
if (index == -1) {
System.out.println("Search element not found");
}
20
else {
System.out.println(n + " found at position " + index);
}
}
}
Output:
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();
}
else {
System.out.println(word + "\t" );
word = "";
}
}
}
}
Output:
22
Variable Description Box:
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:
25
ABCD
ABCDE
Display proper message for an invalid choice.
Program:
import java.util.*;
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:
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:
29
the element along with its position, otherwise display the message
“Search element not found”.
Program:
import java.util.*;
if (index == -1) {
System.out.println("Search element not found");
}
30
else {
System.out.println(n + " found at position " + index);
}
}
}
Output:
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:
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];
32
if (ch[i] < l)
{
l = ch[i];
}
}
}
Output:
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
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();
}
34
for (int i = 0; i < 20; i++)
{
double sq = ar[i] * ar[i];
System.out.println("Square of "+ar[i]+" : " + sq);
}
}
}
Output:
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;
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:
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;
38
System.out.println("Names that begin and end with letter A are:");
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:
41
19. Design a class with the following specifications:
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;
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();
}
43
Output:
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
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.*;
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");
}
System.out.println("Pattern: ");
obj.print();
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 ac = 0;
int sc = 0;
int dc = 0;
char ch;
}
}
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.*;
51
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
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:
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:
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:
58
26. Define a class to overload the method perform() as follows:
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:
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:
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:
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:
30. Define a class to accept the Gmail ID and check for its validity.
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:
67
st2 String To store the
substring
n boolean To check whether
st1 is equal to st2
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
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:
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:
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:
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:
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();
}
}
}
else
System.out.println((2*n +3*m));
}
79
Output:
SIGNATURES:-
80
EXTERNAL EXAMINER
--------------------------------------------------
INTERNAL EXAMINER
--------------------------------------------------
81
82