SPICER HIGHER SECONDARY SCHOOL(ICSE/ISC)
Aundh Road, Pune – 67
“JAVA PROGRAMS”
This project is submitted in the partial fulfilment of
the requirement subject computer applications
Submitted to : Mrs. Shakila Thorne
Submitted by : Arjun R. Dhar
STD :X
SEC :H
Submitted On : August 30, 2024
Internal Examiner External Examiner
__________________ ___________________
Index
Sr No. Topic Pg No.
I if Statement 1
(a) if 1
(b) if-else 2
(c) if-else ladder 3
II Switch(Loops) 4
(a) for loop 5
(b) while loop 6
(c) do-while loop 8
III Methods/Functions - Function Overloading 9
IV Constructors 11
(a) Parameterized Constructors 11
(b) Non-Parameterized Constructors 11
V Strings 15
VI Arrays 20
(a) Single Dimensional Arrays 20
(b) Double Dimensional Arrays 20
(c) Linear Search 22
(d) Binary Search 22
(e) Sorting 24
VII Special Numbers 29
VIII Bibliography 31
1.if else if (if else if ladder)
The selection statements allowed to choose the set of instructions for execution depending upon
expressions, truth value. Java provides two types of selection statements and one of them is the if
statement.
1.if
An if statement tests a particular condition ; if the condition evaluates to true, a course of action is
followed i.e, A statement or set of statements is executed. Otherwise the course of action is
ignored.
Format:
if(expression)
Statement;
2.if- else
Another form of if that allows for this kind of either or condition by providing an else clause is known
as if-else statement. The if-else statement tests an expression and depending upon it’s truth value
one of the two sets of action is executed.
Format:
if(expression)
Statement 1;
Else
Statement 2;
3.if-else-if
A common programming construct in java is the if-else-if ladder, which is often also called the if-
else-if staircase because of it’s appearance.
Format:
if(expression1)
Statement 1;
else if(expression2)
Statement 2;
else if(expression3)
Statement 3;
:
:
else
Statement n;
Page 1 of 31
Program 1:
The Electricity board charges from their consumers according to the units consumed per month. The
amount is calculated as per the tariff given below.
Units consumed: charges:
Up to 100 units rs5.50/unit
For next 200 units rs6.50/unit
For next 300 units rs7.50/unit
More than 600 units rs8.50/unit
Write a program to input consumer’s name, consumer’s number and the units consumed. The
program displays the following information after calculating the amount.
Money Receipt
Consumer’s Number:
Consumer’s Name:
Units Consumed:
Amount to be paid:
import java.util.*;
public class Electricity_Bill
public static void main(String args[])
Scanner in=new Scanner(System.in);
int n,u;
String name;
double amt=0,total=0;
System.out.println("Enter consumer's name");
name=in.nextLine();
System.out.println("Enter consumer's number");
n=in.nextInt();
System.out.println("Enter units consumed");
u=in.nextInt();
if(u<=100)
Page 2 of 31
amt=u*5.50;
if((u>100)&&(u<=300))
amt=100*5.50+(u-100)*6.50;
if((u>300)&&(u<=600))
amt=100*5.50+200*6.50+(u-300)*7.50;
if((u>300)&&(u<=600))
amt=100*5.50+200*6.50+300*7.50+(u-600)*8.50;
System.out.println("Consumer's number:"+n);
System.out.println("Consumer's name:"+name);
System.out.println("Units consumed:"+u);
System.out.println("Amount to be paid:Rs."+amt);
Input:
Enter consumer's name
Arjun
Enter consumer's number
1235
Enter units consumed
150
Output:
Consumer's number:1235
Consumer's name:Arjun
Units consumed:150
Amount to be paid:Rs.875.0
Enter consumer's name
Page 3 of 31
2.SWITCH
Java provides a multiple branch selection statement known as switch. This selection statement
successively tests the value of an expression against a list of integer or character constants, for
equality.
Format:
switch(expression)
case constant1 : statement -sequence1;
break;
case constant : statement -sequence2;
break;
case constant : statement -sequence3;
break;
case constant -1 :statement – sequence-1;
break;
[default : statement – sequence n ];
Program 2:
Write a menu driven program to find the area of an Equilateral triangle,
an Isosceles triangle and a scalene triangle as per the user’s choice.
√3 2
1. Equilateral triangle = s ,s =side of an equilateral triangle
4
1
2. Isosceles triangle= 4b*√4𝑎2 − 𝑏2
𝑚+𝑛+𝑝
3. Scalene triangle = √𝑠(𝑠 − 𝑚)(𝑠 − 𝑛)(𝑠 − 𝑝),s = 2
(where m, n and p are three sides of a scalene triangle)
import java.util.*;
public class Menu
public static void main (String args[])
Scanner in=new Scanner(System.in);
int c,s,a,b,m,n,p;
Page 4 of 31
double k, area=0;
System.out.println("1. Area of an Equilateral triangle");
System.out.println("2. Area of an isosceles triangle");
System.out.println("3. Area of a Scalene triangle");
System.out.println("Enter your choice");
c=in.nextInt();
switch(c)
case 1:
System.out.println("Enter side of an Equilateral triangle");
s=in.nextInt();
area= (Math.sqrt(3)*s*s)/4.0;
System.out.println("Area="+area);
break;
case 2:
System.out.println("Enter side and base of Isosceles triangle");
a=in.nextInt();
b=in.nextInt();
area= (Math.sqrt(4*a*a-b*b))/4.0;
System.out.println("Area="+area);
break;
case 3:
System.out.println("Enter sides of scalene triangle");
m=in.nextInt();
n=in.nextInt();
p=in.nextInt();
k=(m+n+p)/2.0;
area=Math.sqrt(k*(k-m)*(k-n)*(k-p));
System.out.println("Area="+area);
break;
default:
Page 5 of 31
System.out.println("Wrong choice!!");
Input:
1. Area of an Equilateral triangle
2. Area of an isosceles triangle
3. Area of a Scalene triangle
Enter your choice
Enter side and base of Isosceles triangle
23
40
Output:
Area=5.678908345800274
Program 3:
Write a menu driven program to perform the following tasks:
A) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a
number is formed by adding the three previous numbers. Write a program to display the first twenty
Tribonacci numbers.
For example;
1,1,2,4,7,13, …………………………………..
B) write a program to display all Sunny numbers in the range from 1 to n.
For example;
3,8,15,24,35, …………………………………. Are sunny numbers.
For an incorrect option, an appropriate error message should be displayed.
import java.util.*;
public class Menu_2
public static void main(String args [])
Scanner sc = new Scanner(System.in);
Page 6 of 31
int ch;
System.out.println("Enter 1. for Tribonucci numbers.");
System.out.println("Enter 2. for Sunny numbers.");
System.out.println("Enter your choice for 2.");
ch=sc.nextInt();
switch(ch)
case 1:
int a=1,b=1,c=2,d=0;
System.out.print(a+","+b+","+c);
for(int i=4; i<=20; i++)
d=a+b+c;
System.out.print(","+d);
a=b;
b=c;
c=d;
break;
case 2:
System.out.print("Enter n: ");
int n = sc.nextInt();
double sqRoot, temp;
for (int i=3; i<=n; i++)
sqRoot= Math.sqrt(i+1);
temp =sqRoot - Math.floor(sqRoot);
if(temp==0)
Page 7 of 31
System.out.print(i+ " ");
break;
default:
System.out.println("Incorrect choice");
Input:
Enter 1. for Tribonucci numbers.
Enter 2. for Sunny numbers.
Enter your choice for 2.
Output:
1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136,5768,10609,19513,35890,66012
Page 8 of 31
3.FUNCTION OVERLOADING
A function name having several definitions in the same scope that are differentiable by the number
or types of arguments , is set to be an overloading function. Process creating over loaded function is
called function over loading.
Program 4:
write a class with the name volume using function overloading that computes the volume of a cube,
a shape and a cuboid.
Formula; Volume of s cube (vc)= s*s*s
4
Volume of a sphere(vs)=3*π*r*r*r
Volume of a cuboid(vcd)= l*b*h
import java.util.*;
public class Compute
double vc=0.0D,vs=0.0D,vcd=0.0D;
void volume(int s)
vc=s*s*s;
System.out.println("The volume of a cube=" +vc);
void volume(float r)
vs=4/3*22/7*r*r*r;
System.out.println("The volume of a sphere=" +vs);
void volume(int l,int b, int h)
vcd=l*b*h;
System.out.println("The volume of a cuboid=" +vcd);
public static void main(String args[])
Page 9 of 31
Scanner in= new Scanner(System.in);
int s,l,b,h;
float r;
System.out.println("Enter the value of side of a cube, radius of sphere, sides os cuboid");
s=in.nextInt();
r=in.nextFloat();
l=in.nextInt();
b=in.nextInt();
h=in.nextInt();
Compute ob= new Compute();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
Input:
Enter the value of side of a cube, radius of sphere, sides os cuboid
25
35
23
143
123
Output:
The volume of a cube=15625.0
The volume of a sphere=128625.0
The volume of a cuboid=404547.0
Page 10 of 31
4.Constructor
A member with the same name as it’s class is called constructor and it is used to initialize the
objects of the class type with a legal initial value
Type of constructors:
1. non-parameterized constructors
A constructor that accepts no parameter is called the non-parameterized constructor. Non-
parameterized constructors are considered default constructors.
Format:
Class A
Int I;
Public void getval (){…}
Public void prnval(){…}
: //member functions definitions
Class B
Public void Test()
A 01 = new A();
01.getval();
01.prnval();
2. parameterized constructors:
Parameterized constructors are ones that receive parameters and initialize objects with
received value.
Format:
Class ABC
Int I;
Float j;
Char k;
Page 11 of 31
Public ABC (int a ,float b ,char c)
I = a;
J = b;
K = c;
Program 5:
Define a class salary with the following specifications:
Class name :Salary
Data members :Name,Address,Phone,Subject,Specialization,Monthly salary, income tax.
Member methods:
1) to accept the details of a teacher including the monthly salary
2) to compute the annual income tax at 5% of the annual salary exceeding rs.175000
3) to display the details of the teacher.
Write a main method to create object of the class and call the above member methods.*/
import java.util.*;
class Salary
String name,add,sub;
int ph;
double sal,tax;
void accept()
Scanner in= new Scanner(System.in);
System.out.println("Enter name");
name=in.nextLine();
System.out.println("Enter address");
add=in.next();
System.out.println("Enter Phone number");
Page 12 of 31
ph=in.nextInt();
System.out.println("Enter Subject Specialization");
sub=in.next();
System.out.println("Enter Monthly salary");
sal=in.nextDouble();
void calculate()
if(12*sal>175000)
tax=((sal*12)-175000)*5/100;
else tax=0;
void display()
System.out.println("Name :"+name);
System.out.println("Address :"+add);
System.out.println("Phone number :"+ph);
System.out.println("Subject specialization :"+sub);
System.out.println("Monthly salary :Rs"+sal);
System.out.println("Income tax :Rs"+tax);
public static void main(String args[])
Salary ob=new Salary();
ob.accept();
ob.calculate();
ob.display();
Page 13 of 31
Input:
Enter name
Arjun
Enter address
Sant Tukaram nagar
Enter Phone number
82084006
Enter Subject Specialization
Computer
Enter Monthly salary
20000
Output:
Name : Arjun
Address : Sant Tukaram nagar
Phone number :82084006
Subject specialization : Computer
Monthly salary : Rs 20000.0
Income tax :Rs 3250.0
Page 14 of 31
5.String functions
Method Prototype Description
Returns the length of a string ( number of characters
int length() present in the string ).
Returns a character from the given index ( position
char charAt(int index)
number ) of the string.
Returns the index ( position number ) of first occurrence
int indexOf(char ch)
of a character in the string.
String substring(int start_index, int Used to extract a set of characters simultaneously from a
last_index) given start index to a given last index.
String toLowerCase() Converts all the characters in the string to lowercase.
String toUpperCase() Converts all the characters in the string to uppercase.
String replace(char old_char, char Used to replace a character by another character in the
new_char) given string.
String concat(String s) It is used to concatenate ( join ) two strings together.
Compares two strings together to check whether they are
boolean equals(String s)
identical or not.
Used to compare two strings to check whether both are
identical or not after ignoring the case ( i.e,
boolean equalsIgnoreCase(String s)
corresponding upper and lowercase characters are treated
to be the same ).
Compares two strings based on the unicode value of each
int compareTo(String s) character in the strings and returns the difference.
String trim() Removes beginning and ending spaces from a string.
Tests whether the string begins with specified string, if
boolean startsWith(String s) yes then it returns true else false.
boolean endsWith(String s) Checks whether the string ends with the specified string.
String valueOf(a_primitive_datatype Converts any primitive value (char, int, float, etc.) into
n) string.
primitive_datatype valueOf(String s) Converts a string into a primitive data type.
Page 15 of 31
Program 6:
Define a class Employee_Sal described below:
Class name :Employee_Sal
Data members :
String name : to store name of the employee
String empno : to store employee number
Int basic : to store basic salary of the employee
Member methods:
1. A parameterized constructor to initialize the data members
2. To accept the details of an employee
3. 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
4. To display the name, empno, gross salary, net salary.
Write a main method to create an object of s class and call the above member methods.
import java.util.*;
public class Employee_Sal
String name, empno;
int basic;
double da,hra,pf,gs,net;
Employee_Sal(String n, String en, int bs)
name=n;
empno=en;
basic=bs;
void compute()
da=basic*30.0/100.0;
hra=basic*15.0/100.0;
Page 16 of 31
pf=basic*12.0/100.0;
gs=basic+da+hra;
net=gs-pf;
void display()
System.out.println("Name :"+name);
System.out.println("Employee Number :"+empno);
System.out.println("Gross salary :"+gs);
System.out.println("Net salary :"+net);
public static void main (String args[])
Scanner in = new Scanner(System.in);
String nm, enm;
int bsal;
System.out.println("Enter Employee's Name,Employee no.,basic salary:");
nm=in.nextLine();
enm=in.next();
bsal=in.nextInt();
Employee_Sal ob= new Employee_Sal(nm,enm,bsal);
ob.compute();
ob.display();
Input:
Enter Employee's Name,Employee no.,basic salary:
Page 17 of 31
arjun
212
30000
Output:
Name :arjun
Employee Number :212
Gross salary :43500.0
Net salary :39900.0
Program 7:
Write a program in java to accept a word and display the same in pig Latin form. A word is framed In
Pig Latin form by using the following steps:
1. Enter a word.
2. Shift all the letters which are available before the first vowel, towards the end of the word.
3. Finally add ‘A Y’ at the end of the word sequence.
Sample input : TROUBLE
Sample output: OUBLETRAY
public class Piglatin
public static void main(String str)// Enter a string
int x,y;
String str1,str2;
char b;b=0;
x=str.length();
System.out.println("The Pig Latin form of thr given string.");
for(y=0;y<x;y++)
b=(str.charAt(y));
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||b=='I'||b=='O'||b=='U')
break;
Page 18 of 31
str1=str.substring(x,y);
str2=str.substring(0,y);
System.out.println(str1+str2+"ay");
Program 8:
Write a program in java to enter a String. Print the string in alphabetical order of its letters.
Sample input: COMPUTER
Sample output:CEMOPRTU
public class Alphabet
public static void main(String str)//Enter a String
int i,j,p;
char ch;
p=str.length();
System.out.println("The letters arranged in alphabetical order:");
for(i=65;i<90;i++)
for(j=0;j<p;j++)
ch=str.charAt(j);
if(ch==(char)i||ch==(char)(i+32))
System.out.print(ch);
Output:
The letters arranged in alphabetical order:
CEMOPRTU
Page 19 of 31
6.Array
An array is a collection of variables of the same type that are referenced by a common name.
Arrays are of different types:
1) One dimensional arrays : comprised of finite homogenous elements.
Format:
Type array-name[]= new type[ size];
2) Multi dimensional arrays : comprised of elements , each of which is itself an array.
Format:
Page 20 of 31
Type array-name [] [] = new type [row][columns];
Program 9:
Write a program to accept 10 different numbers in a single dimensional array (SDA).
Display the greatest and the smallest numbers of the array elements.
import java.util.*;
public class Max_Min
public static void main(String args[])
Scanner in=new Scanner(System.in);
int i,min,max;
int m[]=new int[10];
for(i=0;i<10;i++)
System.out.print("Enter the no. in the cell :");
m[i]=in.nextInt();
max=m[0];min=m[0];
for(i=0;i<10;i++)
if(m[i]>max)
max=m[i];
if(m[i]<min)
min=m[i];
System.out.println("The greatest of the array elements ="+max);
System.out.println("The smallest of the array elements ="+min);
Input:
Enter the no. in the cell :3
Page 21 of 31
Enter the no. in the cell :6
Enter the no. in the cell :8
Enter the no. in the cell :7
Enter the no. in the cell :6
Enter the no. in the cell :5
Enter the no. in the cell :5
Enter the no. in the cell :5
Enter the no. in the cell :6
Enter the no. in the cell :1
Output:
The greatest of the array elements =8
The smallest of the array elements =1
Searching:
Sometimes you need to search for an element in an array. To accomplish this task, you can use
different searching techniques. It consists of two very common search techniques viz., linear search
and binary search.
1) Linear search : Linear search refers to the searching technique in which each element of an
array is compared with the search item, one by one, until the search- item is found or all
elements have been compared.
2) Binary search : Binary search is a search technique that works for sorted arrays. Here search
item is compared with the middle element of the array. If the search item matches with the
element, search finishes. If the search item is less than the middle perform, perform binary
search in the first half of the array , otherwise perform binary search in the latter half of the
array.
Program 10:
Write a program to accept 10 different numbers in a single dimensional array (SDA). Now, enter a
number and by using binary search technique, check whether or not the number is present in the list
of array elements. If the number is present then display the message “Search successful” otherwise,
display “Search unsuccessful”.
import java.util.*;
public class B_Search
public static void main(String args[])
Scanner in=new Scanner(System.in);
Page 22 of 31
int i,k=0,p=0,ns,lb=0,ub=9;
int m[]=new int[10];
for(i=0;i<10;i++)
System.out.print("Enter the number in ascending order :");
m[i]=in.nextInt();
System.out.print("Enter the number to be searched :");
ns=in.nextInt();
while (lb<=ub)
p=(lb+ub)/2;
if(m[p]<ns)
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
k=1;
break;
if(k==1)
System.out.println("The search successful and the number is present");
else
System.out.println("The search unsuccessful and the number is not present");
Input:
Enter the number in ascending order :0
Enter the number in ascending order :1
Page 23 of 31
Enter the number in ascending order :2
Enter the number in ascending order :3
Enter the number in ascending order :4
Enter the number in ascending order :5
Enter the number in ascending order :6
Enter the number in ascending order :7
Enter the number in ascending order :8
Enter the number in ascending order :9
Enter the number to be searched :5
Output:
The search successful an
Sorting:
Sorting of an array means arranging the array elements in a specified order i.e, either ascending or
descending order. There are several sorting techniques available e.g., shell sort, shuttle sort, bubble
sort, selection sort, quick sort, heap sort, etc.
Program 11:
Write a program to accept 10 different numbers in a single dimensional array (SDA). Arrange the
numbers in ascending order by using ‘Selection Sort’ technique and display them.
import java.util.*;
public class Selection_Sort
public static void main(String args[])
Scanner in=new Scanner(System.in);
int i,j,t,min;
int m[]=new int[10];
for(i=0;i<10;i++)
System.out.print("Enter the number in the cell :");
m[i]=in.nextInt();
for(i=0;i<9;i++)
Page 24 of 31
{
min=i;
for(j=i+1;j<10;j++)
if(m[j]<m[min])
min=j;
t=m[i];
m[i]=m[min];
m[min]=t;
System.out.println("The numbers arranged in ascending order are:");
for(i=0;i<10;i++)
System.out.println(m[i]);
Input:
Enter the number in the cell :6
Enter the number in the cell :2
Enter the number in the cell :3
Enter the number in the cell :4
Enter the number in the cell :5
Enter the number in the cell :7
Enter the number in the cell :8
Enter the number in the cell :9
Enter the number in the cell :1
Enter the number in the cell :0
Output:
The numbers arranged in ascending order are:
Page 25 of 31
2
Program 12:
Write a program in java to store the numbers in a 4*4 matrix in a double dimensional array. Find the
highest and the lowest numbers of the matrix by using an input statement.
import java.util.*;
public class ddMax_Min
public static void main(String args [])
Scanner in=new Scanner(System.in);
int i,j,min,max;
min=0;max=0;
int m[][]=new int[4][4];
System.out.println("Enter the numbers of the matrix");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
m[i][j]=in.nextInt();
System.out.println("The numbers of the matrix are:");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
Page 26 of 31
System.out.print(m[i][j]+" ");
System.out.println();
min=m[0][0];
max=m[0][0];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(min>m[i][j])
min=m[i][j];
if(max<m[i][j])
max=m[i][j];
System.out.println("The lowest number in the array is "+min);
System.out.println("The highest number in the array is "+max);
Input:
Enter the numbers of the matrix
23
18
14
10
11
Page 27 of 31
99
445
65
Output:
The numbers of the matrix are:
23 18 14 10
8 2 1 11
7 99 445 4
4 3 2 65
The lowest number in the array is 1
The highest number in the array is 445
Page 28 of 31
7.Special number program
Program 13:
Write a program to input a number and print whether the number is a special number or not. A
number is said to be special if the sum of the factorial of the digits of the number is the same as the
original number.
import java.util.*;
public class Special
public static void main (String args[])
int m,n,i,d,f=1,s=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number :");
n=in.nextInt();
m=n;
while(m!=0)
d=m%10;
for(i=1;i<=d;i++)
f=f*i;
s=s+f;
f=1;
m=m/10;
if(s==n)
System.out.println("The sum of factorial of digits ="+s);
System.out.println(n+" is a special number");
else
System.out.println("The sum of factorial of digits ="+s);a
Page 29 of 31
System.out.println(n+" is not a special number");
Input:
Enter a number :
23
Output:
The sum of factorial of digits =8
23 is not a special number
Page 30 of 31
Bibliography
ICSE solved papers last 10 years.
Computer Applications text book.
www.Google.com
http://amanjava.blogspot.in/
http://www.guideforschool.com
http://www.icsej.com/
Page 31 of 31