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

Darhil Computer Project

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

Darhil Computer Project

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

JAVA

PROGRAMS
SPICER HIGHER SECONDARY SCHOOL
Aundh Road, Ganeshkhind, Pune 07.

This project is submitted in the partial fulfilment of the requirement subject

“Computer Applications”

Submitted To : Mrs. Shakila Thorne

Submitted By : Darshil shivkumar

STD : X th

SEC :D

Submitted On : 28 july 2023

_____________________ ___________________

INTERNAL EXAMINER EXTERNAL EXAMINER

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 ;

iii. if - else ladder


Definition:
A common programming construct in java is the if-else-if ladder , which is often
also called the if-else-staircase because of its appearance.
Syntax:
if (expression1)
statement1 ; elseif
(expression2)
statement2 ;
elseif (expresssion3)
statement3 ;
:
else
statement n ;

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:

Question: Write a program to initialize the value of a and b using default


constructor?
Input:

import java.io.*; class


Operation
{
public Operation()
{
int a=0;
int b=0;
}
public static void main()

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.

 Accessor Methods Definition:


Methods used to obtain information about an object are known as accessors methods.
Method prototype Description

10
Char charAt (int index) Return the character at the specified index.

int capacity ( ) Return maximum no. of characters that can be entered in


the current string object(this) i.e., its capacity .
int compareTo (string1, Compares two strings lexicographically.
anotherstring)
String concat(string str) Concatenates the specified string to the end of this string
(current string object) string.
Str1 + str2 Concatenation operator (i.e., + ), achieves same as concat
method.
Boolean endswith(string str) Tests if the this string (current string object ) to the
specified suffix (str).
Boolean equals (string str) Compares the this string ( current string object)to the
specified object str.
Boolean equalslgnoreCase Compares the this string (current string object) to str,
(string str) ignoring case considerations.
int indexOf (char ch) Returns the index2 within the this string (current string
object ) of the first occurrence of the specified character.
int lastIndexOf(char ch) Returns the index within the this string of the last
occurrences of the specified character.
int length( ) Return the length of the this string.

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:

The count of longest word of the sentence is 7

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);

// get the length of greet


int length = greet.length();
System.out.println("Length: " + length);
}
}

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:

Sometimes you need to search for an element in an array. To accomplish this


task, you can use different searching techniques. Here, we are going to discuss two
very common search techniques viz., linear search and binary search.

1. Linear search Definition:


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
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:

import java.io.*; public


class Sorting
{
public void bubblesort(int A[])
{
inti,j,tmp;
for(i=0;i<10;i++)
for(j=0;j<9-i-1;j++)
{
if(A[j]>A[j+1])
{
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
}
}
System.out.println("Array is ascending order is -->");
for(i=0;i<10;i++)
System.out.print(A[i]+"\n");
}
}

Output:
Array in ascending order is -->
1
2
3
4
5
8

16
9
12
15
90

17
Special Number Program
12 :

Question: Write a program to check whether the number is palindrome or not?

Input:

import java.io.*; public


class SpecialNumber
{
public static void main() throws IOException
{
int rem,rev=0,n;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the number");
n=Integer.parseInt(br.readLine());
int t=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(t==rev)
System.out.println("The enetered number is Palindrome");
else
System.out.println("The entered number is not Palindrome");
}
}

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

You might also like