0% found this document useful (0 votes)
14 views52 pages

Record Final X San

The document is a record for Computer Applications at St. Patrick's High School for the academic year 2024-2025, detailing various programming tasks and their implementations in Java. It includes programs for calculating electricity bills, checking for prime palindromes, automorphic numbers, special numbers, disarium numbers, Armstrong numbers, pattern generation, and series generation. Each program is accompanied by coding examples, input/output scenarios, and variable descriptions.

Uploaded by

sansanchit41
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)
14 views52 pages

Record Final X San

The document is a record for Computer Applications at St. Patrick's High School for the academic year 2024-2025, detailing various programming tasks and their implementations in Java. It includes programs for calculating electricity bills, checking for prime palindromes, automorphic numbers, special numbers, disarium numbers, Armstrong numbers, pattern generation, and series generation. Each program is accompanied by coding examples, input/output scenarios, and variable descriptions.

Uploaded by

sansanchit41
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/ 52

ST.

PATRICK’S HIGH SCHOOL (CISCE)

COMPUTER APPLICATIONS RECORD


2024 – 2025

NAME: SANCHIT
CLASS: X
SEC: B
1
S.NO TITLE PAGE NUMBER

1 Electricity bill 4

2 Prime palindrome number 7

3 Automorphic number 10

4 Special number 12

5 Disarium number 14

6 Armstrong number 16

7 Pattern generation 18

8 Series generation 23

9 Selection Sort 27

Arranging Strings in alphabetical order using


10 29
Bubble sort

11 Function 31

12 Function overloading 34

13 Constructor 36

14 Parametrized constructor 38

15 Constructor overloading 40

2
16 Strings 43

17 Strings 45

18 Two-Dimensional Array 47

19 Two-Dimensional Array 49

20 Binary Search 51

3
PROGRAM:1 ELECTRICITY BILL

AIM:

Write a java program to input consumer number, name and units

consumed. Calculate the electricity bill based on the following:

Units Consumed Charges

Up to 100 units Rs.1.80/unit

More than 100 units up to 300 units Rs.2.30/unit

More than 300 units up to 500 units Rs.2.80/unit

More than 500 units Rs.3.50/unit

PROGRAM CODING:

import java.util.*;

class ElectricityBill

public static void main(String args[])

int units,cno;

String cnm;

Scanner sc=new Scanner(System.in);

System.out.println("enter consumer no, name & number of units");

cno= sc.nextInt();

cnm= sc.next();

units=sc.nextInt();

double billpay=0;

if(units<=100)

billpay=units*1.80;

else if(units>=101 && units<=300)


4
billpay=100*1.80+(units-100)*2.30;

else if(units>=301 && units<=500)

billpay=100*1.80+200 *2.30+(units-300)*2.80;

else

billpay=100*1.80+200 *2.30+200*2.80+(units-500)*3.50;

System.out.println(“consumer no: ”+cno+ “\n consumer name: ”+cnm+

“\n units consumed: ”+units+ “\nBill to pay :" + billpay);

5
INPUT/OUTPUT:

Enter consumer no, name & number of units

101

ANU

250

consumer no:101

consumer name: ANU

units consumed: 250

Bill to pay : 525.0

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

cno int to store customer number

cnm String to store the consumer name

units int units consumed

billpay double Amount to be paid

6
PROGRAM:2 PRIME PALINDROME NUMBER

AIM:

Write a java program to input a number and check whether the


number is prime palindrome or not. If a number is simultaneously palindromic and prime then it
is said to be palPrime number or primePalindrome number.

PROGRAM CODING:

import java.util.*;

class PrimePalin

public static void main(String args[ ])

Scanner in= new Scanner(System.in);

int n,p,rev,s=0,i,c=0;

System.out.println("Enter No.");

n= in.nextInt();

p=n;

for(i=1;i<=p;i++)

if(p%i==0)

c++;

while(n>0)

rev=n%10;

s=s*10+rev;

n=n/10; }
7
if(p==s && c==2)

System.out.println("Number is PrimePalindrome : "+p);

else

System.out.println("Number is not PrimePalindrome : "+p);

8
INPUT/OUTPUT:

Enter No. 313

Number is PrimePalindrome : 313

Enter No. 200

Number is not PrimePalindrome : 200

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

n int to accept a number from the user.

p int Temporary variable

rev int to store the result

s int to find the sum

c int counter variable

i int loop variable

9
PROGRAM:3 AUTOMORPHIC NUMBER

AIM:

Write a program to check whether an entered number is an automorphic number or


not. An automorphic number is a number whose square ends in the same digits as the number
itself.

PROGRAM CODING:
import java.util.*;
class automorphic
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=sc.nextInt( );
int m,b,p,q;
m=n;
p=m*m;
int c=0;
while(m>0)
{
c++;
m=m/10;
}
q=p%(int)Math.pow(10,c);
if(n==q)
{
System.out.println("It is an automorphic number");
}
else
{
System.out.println("It is not an automorphic number");
}
}
}

10
INPUT/OUTPUT:
Enter a number
25[25*25=625]
It is an automorphic number

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

n int to accept a number from the user.

m int to store the accepted number without change.

p int to find the square of the accepted number

q int to extract the last digits of an entered number

11
PROGRAM:4 SPECIAL NUMBER
AIM:
Write a program to check whether the entered number is a special number or not.
A number is said to be special number when the sum of the factorial of its digits are equal to the
number itself.

PROGRAM CODING:
import java.util.*;
class special
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,num;
System.out.println("Enter a number");
num=sc.nextInt();
int a,s=0;
int fact;
n=num;
while(n>0)
{
a=n%10;
fact=1;
int i=1;
while(i<=a)
{
fact*=i;
i++;
}
s=s+fact;
n=n/10;
}
if(s==num)
{System.out.println("It is a special number");
}
else
{
System.out.println("It is not a special number");
}
}
}

12
INPUT/OUTPUT:
Enter a number
145[1!+4!+5!=1+24+120=145]
It is a special number

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

num int to accept a number from the user.

n int to store the accepted number without change.

a int to store the last digit of the number

i int to store the digits which make up the factorial of a


number

fact int to find the factorial of a digit

s int to find the sum of factorial of digits

13
PROGRAM:5 DISARIUM NUMBER
AIM:
Write a program to accept a number and check whether it is a disarium number or
not. A number is called Disarium if sum of its digits powered with their respective positions is
equal to the number itself.

PROGRAM CODING:
import java.util.*;
class disarium
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=sc.nextInt();
int a,d,sum=0;
a=n;
String s=Integer.toString(n);
int len=s.length();
while(n>0)
{
d=n%10;
sum=sum+(int)Math.pow(d,len);
len--;
n=n/10;
}

if(sum==a)
{
System.out.println("It is a disarium number");
}
else
{
System.out.println("It is not a disarium number");
}
}
}

14
INPUT/OUTPUT:
Enter a number
135[11+32+53=1+9+125=135]
It is a disarium number

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

n int to accept a number from the user.

a int to store the accepted number without change.

string to convert the accepted number to String


s

len int to find the length of the string

d int to find the last digit of the number

sum int to find the sum of the digits powered to their


respective positions

15
PROGRAM: 6 ARMSTRONG NUMBER
AIM:
Write a program to accept a number and check whether the number is Armstrong
number or not by using the function name check(int n). The function returns 1, if the number is
Armstrong , otherwise 0. [Armstrong number is a number which equals to the sum of the cubes of
its individual digits.]

PROGRAM CODING:
import java.util.Scanner;
public class armstrong
{
public int check(int n)
{
int d, s=0;
int num=n;
while(n>0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==num)
return 1;
else
return 0;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
armstrong obj=new armstrong();
int n;
System.out.println("Enter a number");
n=sc.nextInt();
int k=obj.check(n);
if(k==1)
System.out.println("Armstrong number");
else
System.out.println("Not a armstrong number");
}
}

16
INPUT/OUTPUT:
Enter a number
153[13+53+33=1+125+27=153]
Armstrong Number

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

n int to accept a number from the user

k int Flag value

d int to extract the last digit

s int to find the sum

num int to store the original value

17
PROGRAM: 7 PATTERN GENERATION
AIM:
Write a program to print the following patterns:
i.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i+"\t");
}
System.out.println();
}
}
}

ii.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}

18
iii.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
class pattern
{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
iv.
1 1 1
2 2
3
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=3;j>=i;j--)
{
System.out.print(i+"\t");
}
System.out.println();
}
}
}
v.
3
2 2
1 1 1
class pattern
{
public static void main(String args[])
{
for(int i=3;i>=1;i--)
{
for(int j=3;j>=i;j--)
{
19
System.out.print(i+"\t");
}
System.out.println();
}
}
}
vi.
1
2 1
3 2 1
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=3;i++)
{
for(int j=i;j>=1;j--)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
vii.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
class pattern
{
public static void main(String args[])
{
int k=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(k+"\t");
k++;
}
System.out.println();
}
}
}

20
viii.
*
* #
* # *
* # * #
* # * # *

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

for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==0)
{
System.out.print("#"+"\t");
}
else
{
System.out.print("*"+"\t");
}
}
System.out.println();
}
}
ix.
1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

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

for(int i=1;i<=9;i+=2)
{
for(int j=1;j<=i;j+=2)
{

System.out.print(j+"\t");
}
System.out.println();
21
}
}
}
x.
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
class pattern
{
public static void main(String args[])
{

for(int i=5;i>=1;i--)
{
for(int j=5;j>=1;j--)
{

System.out.print(i+"\t");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

i int to execute the loop for the number of rows

j int to execute the loop for the number of columns

22
PROGRAM:8 SERIES GENERATION
AIM:
Write a program to generate the following series:

i.Sum=1/2 +3/4+5/6+7/8+….+19/20

class series
{
public static void main(String args[])
{
double term=0,sum=0;
for(int i=1;i<=19;i+=2)
{
term=(double)i /i+1;
sum+=term;
}
System.out.println(“Sum of the series=”+sum);
}
}

Output:
sum=20

ii.sum=2-4+6-8+…..-20

class series
{
public static void main(String args[])
{
int sum=0;
for(int i=2;i<=20;i+=2)
{
if(i%4==0)
sum=sum-i;
else
sum=sum+i;
}
System.out.println(“Sum=”+sum);
}
}

Output:
sum=-10

iii.1,-3,5,-7,9

class series
{
public static void main(String args[])
{
23
int i,j=-3;
for(i=1;i<=20;i+=4)
{
System.out.print(i+”,”);
System.out.print(j+”,”);
j=j-4;
}
}

Output:
1,-3,5,-7……-19

iv.Sum=1!+2!+3!+….n

import java.util.*;
class series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println(“Enter a number”);
n=sc.nextInt( );
int f=1;
int sum=0;
for(i=n;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
f=f*j;
}
sum=sum+f;
f=1;
}
System.out.println(“Sum=”+sum);
}
}

Output:
Enter a number
5
sum=153

v.1,12,123,1234,….n

class series
{
public static void main(String args[])
{
24
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.print(“,”);
}
}
}

Output:
1,12,123,1234,….12345678910
vi. s=(1*2)+(2*3)+….+(19*20)

class series
{
public static void main(String args[])
{
int sum=0;
for(int i=1;i<=19;i++)
{
sum=sum+(i*(i+1));
}
System.out.println(“Sum=”+sum);}}

Output:
sum=2660

vii. s=1+(1+2)+(1+2+3)+….+(1+2+3+…+n);

import java.util.*;
class series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int s=0,sum=0,n;
System.out.println(“Enter a number”);
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
s=s+i;
sum=sum+s;
}
System.out.println(“Sum=”+sum);
}
}
25
Input/Output:
Enter a number
5
sum =35

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

i int to execute the outer loop to generate the series

j int to execute the inner loop


s int to compute the sum of the digits
sum int to compute the sum of the series
term int to compute the sum of each term

26
PROGRAM:9 SELECTION SORT
AIM:
Write a program to accept 10 numbers from the user. Sort the elements of the array in
ascending order using selection sort technique.

PROGRAM CODING:
import java.util.*;
class selectionSort
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
System.out.println("ENTER 10 NOS.");
for(int i=0;i<10;i++)
{
m[i]=sc.nextInt();
}
int temp,pos;

for(int i=0;i<10;i++)
{
pos=i;
for(int j=i+1;j<10;j++)
{
if(m[j]>pos)
{
pos=j;
}
}
temp=m[i];
m[i]=m[pos];
m[pos]=temp;
}
System.out.println("FINAL SORTED ARRAY");
for(int i=0;i<10;i++)
{
System.out.println(m[i]);
}
}
}

27
INPUT/OUTPUT:
Enter 10 numbers
20
14
16
18
10
2
4
6
8
12

Sorted Array:

4
6
8
10
12
14
16
18
20

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

m int to accept 10 numbers from the user.

i int Loop variable for the outer loop


j int Loop variable for the inner loop

pos int To find the index position of the number

temp int Temporary variable to swap two numbers

28
PROGRAM:10 BUBBLE SORT

AIM:

Write a program to arrange Strings in an Alphabetical Order using bubble sort technique.

PROGRAM CODING:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str[] = new String[5];
String temp;
System.out.println("Enter the Strings one by one:");
for(int i = 0; i < 5; i++)
{
str[i] = sc.nextLine();
}
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
} } }
System.out.print("Strings in Sorted Order:");
for (int i = 0; i <5; i++)
{
System.out.print(str[i] + ", ");
} } }

29
INPUT/OUTPUT:
Enter String one by one:
Anu
Jason
Rithik
Benny
Kevin

Strings in sorted order:


Anu
Benny
Jason
Kevin
Rithik

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

str String to accept 5 names from the user

i int to represent and execute loop for the position


of an entered number in array str

j int to store the position of a value from array str


for comparison

temp String to store a number temporarily during


exchange of positions

30
PROGRAM:11 FUNCTION

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

PROGRAM CODING:
import java.util.*;
class parkingLot
{
private int vno;
private int hours;
private double bill;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the vehicle number");
vno=sc.nextInt();
System.out.println("Enter the number of hours");
hours=sc.nextInt();
}
void calculate()
{
if(hours>0&& hours<=1)
{
bill=hours*3.0;
System.out.println("Parking charges="+bill);
}
else
{
bill=3.0+(hours-1)*1.50;
System.out.println("Parking charges="+bill);
}
}
void display()
{
System.out.println("Vehicle number"+vno);
System.out.println("Total hours"+hours);
System.out.println("Total bill"+bill);
}

31
public static void main(String args[])
{
parking obj=new parking();
obj.input();
obj.calculate();
obj.display();
}
}

32
INPUT/Output:
Enter the vehicle number
7345
Enter the number of hours
5
Parking charges=9.0
Vehicle number7345
Total hours5
Total bill9.0

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

vno int to store the vehicle number

hours int to store the number of hours the vehicle is parked in


the parking lot.
bill double to store the bill amount

33
PROGRAM:12 FUNCTION OVERLOADING
AIM:
Design a class to overload a function compare( ) as follows:
a. void compare(int,int)-to compare two integer values and print the greater of the two
integers.
b. void compare(char,char)-to compare the numeric value of two integers and print the
character with higher numeric value.
c. void compare(String,String)-to compare the two strings and print the longer of the two.

PROGRAM CODING:
class overload
{
public void compare(int a,int b)
{
int c=0;
if(a>b)
c=a;
else
c=b;
System.out.println("Greater number is" +c);
}
public void compare(char a,char b)
{
char c=' ';
int m=(int)a;
int n=(int)b;
if(m>n)
c=a;
else
c=b;
System.out.println("Character with higher value is" +c);
}
public void compare(String a,String b)
{
String c="";
int m=a.length( );
int n=b.length( );
if(m>n)
c=a;
else
c=b;
System.out.println("Longest string is" +c);
}
public static void main(String args[])
{
overload obj=new overload();
obj.compare(5,3);
obj.compare('D','N');
obj.compare("Computer","Science");
} }
34
Input/Output:

Greater number is5


Character with higher value is N
Longest string is Computer

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

a int to accept an integer value from the user

b int to accept an integer value from the user

a char to accept a character from the user

b char to accept a character from the user

a String to accept a word from the user

b String to accept a word from the user

35
PROGRAM:13 CONSTRUCTOR
AIM:
Write a complete java program with the following members of its class:

Members Member name Description

Private data ac_num Stores account number

members: ac_name Stores account holders name

bal Stores balance amount in the account

member getValues() To store data


methods
display() To display data

deposit() To modify balance according to the amount

deposited

withdrawl() To modify balance according to the withdrawn


au amount

PROGRAM CODING:
class Account
{
private long ac_num;
private String ac_name;
private double bal;
Account( )
{
ac_num=0;
ac_name=" ";
bal=0.0;
}
public void getValues(long acNumber,String acName)
{
ac_num= acNumber;
ac_name= acName;
}
public void deposit(double amt)
{
bal+=amt;
}
public void withdrawl(double wamt)
{
bal-=wamt;
}

36
public void display( )
{
System.out.println("Account Number:"+ac_num);
System.out.println("Account Holder’s Name:"+ac_name);
System.out.println("Account Holder’s Balance:"+bal);
}
public static void main(String args[])
{
Account obj=new Account();
obj.getValues(20041961,"Sumith Arora");
System.out.println("After Depositing Money");
obj.deposit(5000);
obj.display();
System.out.println("After withdrawing Money");
obj.withdrawl(1000);
obj.display();
}
}

Input/Output:
After Depositing Money
Account Number:20041961
Account Holder’s Name:Sumith Arora
Account Holder’s Balance:5000.0
After withdrawing Money
Account Number:20041961
Account Holder’s Name:Sumith Arora
Account Holder’s Balance:4000.0

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

ac_number long Stores account number

ac_Name String Stores account holder’s name

bal double Stores balance amount in the account

37
PROGRAM:14 PARAMETRIZED CONSTRUCTOR

AIM:

Define a class Employee described below:

Data members:

String name: to store the name of the employee

String empno: to store employee number

int basic: to store basic salary of an employee

Member methods:

i. A parametrized constructor to initialize the data members.


ii. To compute the gross and net salary as:
da=30% of basic
hra=15% of basic
pf=12% of basic
gross=basic+da+hra
net=gross-pf
iii. To display the name, empno, gross salary and net salary.

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

PROGRAM CODING:

import java.util.*;

public class employee {

String name, empno;


int basic;
double da, hra, pf, gs, ns;

employee(String n, String en, int b)


{
name=n;
empno=en;
basic=b;
}

public void compute( )


{
da=basic*30.0/100.0;
hra=basic*15.0/100.0;
pf=basic*12.0/100.0;
38
gs=basic+da+hra;
ns=gs-pf;
}
public void display( )
{
System.out.println("Name:"+name);
System.out.println("Employee number="+empno);
System.out.println("Gross salary="+gs);
System.out.println("Net salary="+ns);
}
public static void main(String args[ ])
{
employee obj=new employee("Aravind","emp0876", 25000);
obj.compute( );
obj.display();
}
}

Input/Output:

Name: Aravind
Employee number=emp0876
Gross salary=36250.0
Net salary=33250.0

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

name String to store the name of the employee

empno String to store the employee number

basic int to store the basic salary of an employee

gs double to display the gross salary

ns double to display the net salary

39
PROGRAM:15 CONSTRUCTOR OVERLOADING

AIM:

Define a class ‘Cabservice’ described below:

Data members:

String taxino: to store taxi number

String name: to store name of the passenger

int dis: to store the distance travelled in km.

Member methods:

i. Cabservice( ): A default constructor to initialize :


taxino=0, name=” ”, d=0;
ii. A parametrized constructor to initialize the data members.
iii. To compute the bill as follows:
Distance travelled Rate/Km

Upto 1 km Rs.25

more than 1 km and upto 5 km Rs.30

more than 5 km and upto 10 km Rs.35

more than 10 km and upto 20 km Rs.45

more than 20 km Rs.50

iv. To display the bill.

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

PROGRAM CODING:

import java.util.*;
public class Cabservice {
String taxino, name;
int dis, amt;
Cabservice( )
{
taxino=" ";
name="";
dis=0;
40
}
Cabservice(String tno, String n, int d)
{
taxino=tno;
name=n;
dis=d;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter taxi number");
taxino=sc.nextLine();
System.out.println("Enter name");
name=sc.next();
System.out.println("Enter distance travelled");
dis=sc.nextInt();
}
public void compute( )
{
if(dis<=1)
amt=25;
if(dis>1 && dis<=5)
amt=dis*30;
if(dis>5 && dis<=10)
amt=dis*35;
if(dis>10 && dis<=20)
amt=dis*45;
if(dis>20)
amt=dis*50;
}
public void display( )
{
System.out.println("Taxi no:"+taxino);
System.out.println("Name:"+name);
System.out.println("Distance travlled="+dis);
System.out.println("Bill amount="+amt);
}
public static void main(String args[ ])
{
Cabservice obj =new Cabservice();
//Default constructor
obj.input();
obj.compute( );
obj.display();
Cabservice obj1 =new Cabservice("Tn07 AH54","Reni",12);
// Parametrized constructor
obj1.compute( );
obj1.display();
}
}
41
Input/Output:

Enter taxi number :Tn07 CB89


Enter name :Jevin
Enter distance travelled :27
Taxi no:Tn07 CB89
Name:Jevin
Distance travelled=27
Bill amount=1350
Taxi no:Tn07 AH54
Name:Reni
Distance travelled=12
Bill amount=540

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

name String to store the name of the passenger

taxino String to store the taxi number

dis int to store the distance travelled

amt int to display the bill amount

42
PROGRAM 16: STRINGS

AIM:

Write a program to accept a word and display the new word by replacing only the vowels with
the character following it.

PROGRAM CODING:

import java.util.*;

class replace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string");
s=sc.next();

int i,l;
int c;
char ch;
l=s.length();
char d;
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
c=(int)(ch + 1);
d=(char)c;
System.out.print(d);
}
else
System.out.print(s.charAt(i));

}
}
}

43
Input/Output:
Enter a string
computer
cpmpvtfr

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

s String to accept a word from the user

l int to find the length of the entered string

i int to represent the position of each letter in the


word
ch char
to take each character of the word and check if it
is a vowel
c int
for getting the character following the vowel

d char to replace the vowel with the character following


it

44
PROGRAM 17: STRINGS

AIM:

Write a program to arrange the entered word/string in alphabetical order.

PROGRAM CODING:

import java.util.*;
class alphabetical
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string");
s=sc.next();
int l=s.length();
char ch;
int a=0;
for(int i=65;i<=90;i++)
{
for(int j=0;j<l;j++)
{
ch=s.charAt(j);
a=ch;
if((a==i)||(a==i+32))
System.out.print(ch);
}
}
}
}

45
Input/ Output:
Enter a string
computer
cemoprtu

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

s String to accept a word from the user

len int to find the length of the accepted string

ch char to take each character from the word

a int to convert the character to its ASCII value

i int to represent ASCII value and check with each


character

46
PROGRAM 18: TWO DIMENSIONAL ARRAY

AIM:

Write a program to input values in a two dimensional array with 3 rows and 3 columns. Compute and
print the sum of elements at the left diagonal.

PROGRAM CODING:

import java.util.*;
class sumLeftDiagonal
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int a[ ][ ]=new int[3][3];
System.out.println(“Enter the values”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt( );
}
}
System.out.println(“Matrix format”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println( );
}
int s=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
s=s+a[i][j];
}
}
System.out.println(“Sum of elements at the left diagonal is” +s);
}
}

47
OUTPUT:

Enter the values:


1
2
3
4
5
6
7
8
9
Matrix format:
1 2 3
4 5 6
7 8 9

Sum of elements at the left diagonal is 15(i.e 1+5+9=15)

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

a int to accept numbers from the user.

i int to represent row

j int to represent column

s int to find the sum of left diagonal

48
PROGRAM 19: TWO DIMENSIONAL ARRAY
AIM:

Write a program to input values in a two dimensional array with 3 rows and 3 columns.
Compute and print the transpose of the given matrix.

PROGRAM CODING:

import java.util.*;
class transpose
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int a[ ][ ]=new int[3][3];
System.out.println(“Enter the values ”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt( );
} }

System.out.println(“Matrix format”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println( );
}
int b[ ][ ]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=a[j][i];
} }
System.out.println(“Transpose matrix”);

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+”\t”);
}
System.out.println( );
}
} }
49
INPUT/OUTPUT:

Enter the values:


1
2
3
4
5
6
7
8
9

Matrix format:
1 2 3
4 5 6
7 8 9

Transpose matrix:
1 4 7
2 5 8
3 6 9

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

a int to accept numbers from the user.

i int Loop variable for the outer loop

j int Loop variable for the inner loop

b int Transpose matrix

50
PROGRAM 20: BINARY SEARCH

AIM:

Write a java program to search an element in an array using binary search technique.

PROGRAM CODING:

import java.util.*;
class search
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
System.out.println("Enter 10 numbers in ascending or descending order");
for(int i=0;i<10;i++)
{
m[i]=sc.nextInt();
}
int ns;
int k=0;
int beg=0,last=9,mid=0;
System.out.println("Enter the number to be searched");
ns=sc.nextInt();
while(beg<=last)
{
mid=(beg+last)/2;
if(m[mid]<ns)
beg=mid+1;
if(m[mid]>ns)
last=mid-1;
if(m[mid]==ns)
{
k=1;
break;
}
}
if(k==1)
System.out.println("Search successful");
else
System.out.println("Search unsuccessful");
}
}

51
INPUT/OUTPUT:
Enter 10 numbers in ascending or descending order
1
4
6
8
9
13
16
18
20
31
Enter the number to be searched
4
Search successful

VARIABLE DESCRIPTION:

VARIABLE DATATYPE DESCRIPTION

m int to accept 10 numbers from the user.

i int to represent and execute loop for the position


of an entered number in an array m

ns int to store the number which needs to searched


by the user.

k int flag value

beg int the starting position of the array after the array
splits where the search begins

last int the ending position of the array after the array
splits where the search ends

mid int to find and store the middle position of the


array

52

You might also like