Darhil Computer Project
Darhil Computer Project
PROGRAMS
SPICER HIGHER SECONDARY SCHOOL
Aundh Road, Ganeshkhind, Pune 07.
“Computer Applications”
STD : X th
SEC :D
_____________________ ___________________
1
INDEX
Sr. No. Topic Page no.
1. If statement 3
i. if
ii. if else
iii. if else ladder
Program 1 4
2. Loops 5
Program 2
Program 3 6
3. Methods/function 7
Program 4
8
Program 5
4. Constructor
i. parameterized constructor
ii. non-parameterized constructor
Program 6 9
5. String 10 -13
Program 7
Program 8
6. Arrays
i. General 13
ii. Linear
iii. Bubble 15
Program 9
Program10 16
Program 11
7. Special Number 18
Program 12
2
8. Bibliography 18
1. THE IF STATEMENT : i.
if
Definition:
An if statements 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.
Syntax:
if (expression)
statement;
ii. if else-
Definition:
There is a another form of if that allows for this kind of either-or condition by
providing an else clause.
Syntax:
if (expression)
statement 1 ;
else
statement 2 ;
3
Program 1:
1) Question : Write a program to compare two given numbers and display which of
them is greater or whether they are equal
class CompareTwoNumber
{
void main()
{
int num1 = 779 ;
int num2 = 456 ;
if (num1 > num2)
{
System.out.println(num1 + " is greater than " + num2);
{
if (num1 < num2 )
{
System.out.println(num1 + " is less than " + num2);
if(num1 == num2 )
{
System.out.println(num1 + " is equal to " + num2);
}
}
}
}
}
}
Output:
: 779 is greater than 456
4
2. LOOPS :
Definition:
Loops in Java is a feature used to execute a particular part of the program
repeatedly if a given condition evaluates to be true..
Syntax:
for(initialization; condition; increment/decrement){
//statement or code to be executed
}
Program 2:
Question: Write a menu driven program for addition and subtraction?
Input:
class pattern
{
void main()
{
int i,j;
for (i=5;i>0;i--)
{
for (j=0;j<i;j++)
{
System.out.print('*');
}
System.out.println();
}
}
}
5
Output:
*****
****
***
**
*
Program 3
Question: Write a program that calculates the sum of first 100 natural number
and prints their sum
Input:
class pattern
{
void main()
{
int currentNum = 1;
int sum = 0;
while (currentNum<=100)
{
sum = sum + currentNum;
currentNum = currentNum + 1;
}
System.out.println("the sum of the number from 1 to 100 is"+sum+".");
}
}
Output:
the sum of the number from 1 to 100 is5050
6
3.METHOD/ FUNCTION OVERLOADING :
Definition:
A function name having several definitions in the same scope that are
differentiable by the number or types of their arguments, is said to be an overloaded
function. Process of creating overloaded functions is called function overloading.
Syntax:
float divide (int a, int b) {.....}
float divide (float a, float b) {....}
Defination:
A method in Java is a block of code that, when called, performs specific
actions mentioned in it. For instance, if you have written instructions to draw a circle
in the method, it will do that task. You can insert values or parameters into methods
Program 4
Question: Write a program to find area of circle and triangle using function
overloading?
Input:
import java.io.*;
public class FunctionOverloading
{ int a,b;
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
void area(double r)throws IOException
{
double pi=3.14;
System.out.println("Enter the radius");
r=Double.parseDouble(br.readLine());
double areaofcircle=pi*r*r;
System.out.println("The area of circle is : "+areaofcircle);
7
}
void area(double base,double height)throws IOException
{
System.out.println("Enter the values of base and height");
base=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
double areaoftriangle=(1/2) * base * height;
System.out.println("The area of triangle is : "+ areaoftriangle);
}
public static void main() throws IOException
{
FunctionOverloading f=new FunctionOverloading();
f.area(7.0);
f.area(5.0,7.0);
}
}
Output:
Enter the radius
7.0
The area of circle is : 153.86
Enter the values of base and height
5.0
7.0
The area of triangle is : 17.5
Program 5
Question : Write a program that finds the maximum of two integers using a
method.
Input :
class rectangle
{
void main()
{
int x = 85;
int y = 26;
int z = (x+y);
System.out.println("the larger one is"+z);
}
}
8
Output :
the larger one is111
4. CONSTRUCTOR :
Definition:
A constructor is a member method of a class that is called for initializing
when an object is created of that class. It (the constructor) has the same name as that
of the class's name and its primary job is to initialize the object to a legal initial value
for the class.
Types of constructors :
a) Parameterized constructors :
Definition:
A constructor that receives arguments are known as Parameterized Constructors.
Syntax:
class ABC
{ int i; float j;
char k;
public ABC (int a, float b, char c) // parameterized constructor
{ i=a; j=b;
k=c
;
}
}
Program 6:
9
{
Operation op=new Operation();
}
}
Output:
The value of a is 0 and b is 0.
b) Non-parameterized constructors :
Definition:
A constructor that accepts no parameter is called the non-parameterized constructors.
Non-parameterized constructors considered as default constructors.
Syntax:
class A
{
int i ;
public void getval( ) { .... }
public void prnval( ) { .... }
: // member function definitions
} class
B
{ public void Test( )
{
A o1 = new A( );
o1.getval( );
o1.prnval( );
}
}
5. STRING
Definition:
Class String provides many accessor methods that may be used to perform
operations on strings.
10
Char charAt (int index) Return the character at the specified index.
String replace(char oldChar, Return a new string resulting from replacing all
char newChar) occurrences of oldChar in the this string with newChar.
boolean startsWith(string Tests if the this string starts with the specified suffix (str).
str)
String Returns a new string that is a substring of the this string.
substring(intbeginIndex,
intendIndex)
String toLowerCase( ) Converts all of the characters in the this string to lower
case.
String toString( ) Returns the string itself.
String toUpperCase( ) Converts all of the characters in the this string to upper
case.
String trim( ) Removes white space from both ends of the this string.
String valueOf(all types) Returns string representation of the passed argument e.g.,
12 represented as “12”
11
Program 7:
Question: Write a program to find the longest word of the sentence and give its
count?
Input:
import java.io.*; public
class Sentence
{ int longestWord(String str)
{
int l=0,max=0,temp=0; int
len=str.length(); for(int
i=0;i<len;i++)
{
if(str.charAt(i) == ' ')
{
if(max<l)
max=l;
l=0;
}
if(str.charAt(len-1)!= ' ')
if(max<l)
max=l;
l++;
}
System.out.println(“The count of longest word of the sentence is ”+ max)
}
}
Output:
Program 8:
Question : To find the length of a string, we use the length() method of the String
Input :
class Main {
public static void main(String[] args) {
// create a string
String greet = "Hello! World";
12
System.out.println("String: " + greet);
Output :
String: Hello! World
Length: 12
6. ARRAY :
Definition:
An array is a collection of variables of the same type that are referred by a name.
Normal :
Program 9:
Question: Write a program to arrange the elements of 1-D array in ascending
order?
Input:
import java.io.*; public
class Ascending
{
public static void main()throws IOException
{
int a[]={4,6,7,1,3,34,5,2,16,42};
int temp; for(int i=0;i<9;i++)
{
if(a[i]<a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
for(i=0;i<9;i++)
{
System.out.println(a[i+1]);
}
}
}
}
13
Output:
1
2
3
4
5
6
7
16
34
42
SEARCHING :
Definition:
2. Binary search
Definition:
Binary Search is a search-technique that works for sorted arrays. Here search-item is
compared with the middle element of array. If the search-item matches with the
element, search finishes. If search-item is less than middle (in ascending array)
perform binary-search in the first half of the array, otherwise perform binary search in
the latter half of the array.
14
Program 10 :
Question: Write a program to search the element from the 1-D array and give its
position?
Input:
import java.io.*; public
class LinearSearch
{
public void lsearch(int n)
{
int a[]={5,3,8,4,9,2,1,6,7,0};
int i,flag=0;
for(i=0;i<10;i++)
{
if(n==a[i])
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Element present at position" +(i+1));
else
System.out.println("Element is not present");
}
}
Output: n=
6
Element present at position 8
Definition:
Sorting of an array means arranging the array elements in a specified order i.e,
either ascending or descending order. Two popular techniques of sorting are selection
sort and bubble sort.
1.Selection Sort :
Definition:
Selection Sort is a sorting technique where next smallest (or next largest) element is
found in the array and moved to its correct position (find position) e.g., the smallest
15
element should be at 1st position (for ascending array),second smallest should be at 2nd
position and so on.
2.Bubble Sort :
Definition:
In bubble sort, the adjoining values are compared and exchanged if they are not in
proper order. This process is repeated until the entire array is sorted. The idea of
bubble Sort is to move the largest element to the highest index position in the array.
Program 11 :
Question: Write a program to sort the elements of the 1-D array using bubble
sort?
Input:
Output:
Array in ascending order is -->
1
2
3
4
5
8
16
9
12
15
90
17
Special Number Program
12 :
Input:
Output:
Enter the number
565
The enetered number is Palindrome
Enter the number
234
The entered number is not Palindrome
BIBLOGRAPHY
• www.w3school.com
• www.google.com
• Ten-Years ICSE
• www.encyclopida.org 10 Computer Text book (Sumita Arora).
• Java Black Book.
18
19