#Myproject FINAL
#Myproject FINAL
1:If Statement
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-statement is executed. Otherwise (if
The condition evaluates to false), the course-of-action is ignored.
Syntax
If (expression)
Statement ;
2:If else
There is another form of If that allow for this kind of either-or condition by providing an
Else clause.
Syntax
If (expression)
Statement 1 ;
else
Statement 2 ;
3: If else if Ladder
A common programming construct in java is the if-else-if ladder , which is often also
calledthe if-else-if staircase because of its appearance.
Syntax
if (expression1) statement1 ;
Else
if (expression2) statement2 ;
else
if (expression3) statement3 ;
:
else statement4
PROGRAM:
Program to calculate commission for the salesman. The commission is calculated
according to following rates
Sales commission Rate
30001 onwards 15%
22001-30000 10%
12001-22000 7%
5001-12000 3%
0-5000 0%
1
Public class commission
{
Public void display (double sales)
{
doublecomm= 0.0;
if (sales<=5000)
comm= 0.0;
else
if (sales<=12000)
comm= sales * 0.03;
else
if (sales<=22000)
comm= sales * 0.07;
else
if (sales<=30000)
comm= sales * 0.10;
else
comm.= sales * 0.15;
System.out.println(“for sales Rs”+salse+”the commission is Rs”+ comm);
}
}
Input: 6000
Output:for sales Rs 6000 the commission is Rs 180
2
II) SWITCH
Java provides a multiple-branch selection statement known as switch case. This selection
statement successively tests the value of an expression against a list of integer or
character constants, for equality. When a match is found, the statements associated with
that constant are executed.
PROGRAM:1
3
case 1 :
int f = 0, s = 1, t;
System.out.print(f+ ‘\t’+ s);
for(i = 1: i<= 8; i++)
{
t = f+s;
System.out.print(“\t” + t);
f+s;
s=t;
}
case 2:
System.out.println(“Enter Number to get the sum of digits”);
n = Integer.parseInt(br.readLine());
while (n! = 0)
{
S = S+ n % 10;
n = n/10;}
System.out.println(“sum of the digits”+S);
default :
System.out.println(“enter correct choice”);}
}
}
Input: 112233
Output: Sum of the digits = 12
PROGRAM:2
write a program WithOut Infinite Loop
class DoWhileBasics
{
4
}
}
Output :
1
2
3
4
5
6
7
8
9
10
5
III) FUNCTION OVERLOADING
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.
PROGRAM:
Write a class with the name volume using function overloading that computes the volume
of a cube, a sphere and a cuboid. Formula:
volume of a cube (vc) = s*s*s
volume of a sphere (vs) = 4/3**r*r*r (where = 3.14 or 22/7)
volume of cuboid (vcd) = l*b*h
class volume
{
void cube (int s)
{
System.out.println(“Volume of cube is”+(s*s*s));
}
void cube (int l, int b, int h)
{
System.out.println(“Volume of cuboid is”+(l*b*h));
}
double cube(double r)
{
double v;
v=(4/3)*22/7*r*r*r;
return v;
}
void test()
{
cube (10);
cube (5, 7, 9);
double Al=cube(3.5);
System.out.println(“Volume of sphere is”+Al);
}
}
6
Input: s = 10, l = 5, b = 7, h = 9
Output: Volume of cube is 1000
Volume of cuboid is 315
Volume of sphere is 128.625
7
IV) CONSTRUCTOR
A member function with the same name as its class is called constructor and it is used to
initialize the objects of that class type with a legal initial value.
1:Types of Constructors
There are two typesofconstructors:-
Non - Parameterized Constructor
Parameterized Constructor
2 :Non-Parameterized Constructor
A constructor that accepts no parameter is called the Non-Parameterized constructor.
Non-Parameterized constructor are considered default constructor.
When a user-defined class does not contain an explicit constructor, the
compilerautomatically Supplies a default constructor, having no arguments. For instance,
consider the following code snippet :
Syntax
Class A
{
inti ;
3: Parameterized Constructor
A constructor may also take arguments. Such constructor are called Parameterized
constructors. The parameterized constructors allow us initialize the various data
elements of different objects with different values when they are created. Following
definition shows a parameterized constructor:
Syntax
class ABC
{
inti ;
float j ;
8
char k;
public ABC (int a, float b, char c) //parameterized constructor
{
i=a;
j=b;
k=c;
}
} //other members
PROGRAM:
Define a class student described as below:
Data members/instance variable:
name, age,m1,m2,m3(marks in 3 subjects),maximum,average
Member methods:
i. A parameterized constructor to initialize the data members.
ii. To accept the detail of a student.
iii. To compute the average and the maximum out of three marks.
iv. To display the name,age, marks in three subjects,maximum and average.
Write a main method to create an object of a class and call the above member methods.
import java.io.*;
class Student
{
string name;
int age,m1,m2,m3,max;
doubleavg;
public Student(String n,inta,intx,inty,int z)
{
name=n;
age=a
m1=x;
m2=y;
m3=z;
}
void accept()throws IOException
{
9
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter name age and marks in 3 subjects”);
name=br.readLine();
age=Integer.parseInt(br.readLine());
m1=Integer.parseInt(br.readLine());
m2=Integer.parseInt(br.readLine());
m3=Integer.parseInt(br.readLine());
}
void compute()
{
avg=(m1+2+m3)/3.0;
if(m1>m2&&m1>m30
{
max=m1;
}
else if(m2>m1&&m2>m3)
{
max=m2
}
else
{
max=m3
}
}
void display()
{
System.out.println(“Name:”+name);
System.out.println(“Age:”+age);
System.out.println(“Mark1:”+m1);
System.out.println(“Mark2:”+m2);
System.out.println(“Mark3:”+m3);
System.out.println(“Maximum:”+max);
System.out.println(“Average:”+avg);
}
publicstatic void main(String args[])throws IOException
10
{
Student ob=new Student(“Niranjan nagalkar”,15,97,93,84);
ob.accept();
ob.compute();
ob.display();
}
}
Output:Name : Niranjannagalkar
Age : 15
Marks 1 : 97
Marks 2 : 93
Marks 3 : 84
Maximum : 97
Average : 91.33
11
V) STRING
A String is a series of characters. Strings are constant and their values cannot be changed
afterthey are created but string are object can be shared.
In java, you can work with character data i.e., single character or group of character i.e.,
stringsin three different ways. Java offers three classes to work with character data.
1:Character Class: Whose instances or objects can hold single character data.This class
offers many methods to manipulate or inspect single character data.
2:String Class: Whose instances or objects can hold unchanging string (immutable
string) i.e., once initialized cannot be modified.
3:String Buffer Class: Whose instances or object can hold (mutable) strings that can be
changed or modified.
Accessory Methods
Methods used to obtain information about an object are called as accessormethods. The
class String provides many accessor methods that may be used to perform operations on
the strings. The Table lists some most used accessor methods of String class.
12
to the
specified object str.
booleanequalsIgnoreCase(String Compare the this String (current String object)
str) to str
, ignoring case considerations.
Returns the index2 within the this String
intindexOf(char ch) (current
string object) of the first occurrence of the
specified
character.
intLastIndexOf(char ch) Returns the index within the this String of last
occurrence of the specified character.
int Length( ) Returns the Length of the this string.
String replace(char old char, char Returns a new String resulting from replacing
new a
occurrence of oldChar in this string with
char) newChar.
booleanstartsWith(String str) Tests if the this string starts with the specified
suffix (str).
String substring(intbeginIndex,
int Returns a new string that is a substring of the
endIndex) this string.
13
PROGRAM:1
Program to check whether the string is palindrome or not. A Palindrome is a string that reads
the same from left to right and vice- versa.
Example:
MADAM, ARORA, 151, 1221 etc…
importjava.util.*;
class Palindrome
{
public static void main(String args [])
{
String original, reverse=””;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to check if it is a palindrome”);
original =in.nextLine( );
int length = original.length( );
for ( int i = length – 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt( i );
if (original.equals(reverse))
System.out.println(“Entered string is a palindrome.”);
else
System.out.println(“Entered string is not a palindrome.”);
}
}
Output:
Enter a string to check if it is a palindrome
45654
Entered string is a palindrome.
PROGRAM:2
Program for selection sort
import java.io.*;
class selectionsort
14
{
public static void main(String args[])throws IOException
{
int x[]=new int[10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter 10 Integer values ");
for(int i=0;i<10;i++)
{
x[i] = Integer.parseInt(br.readLine()); //input array of numbers
}
int h,p=0;
for(int i=0;i<10;i++)
{
h=x[i];
for(int j=i+1;j<10;j++) //loop for comparison with next element
{
if(x[j]>h) //descending order
{
h=x[j];
p=j;
}
}
x[p]=x[i]; //swap
x[i]=h;
}
System.out.println(" After Sorting");
for(int i =0;i<=x.length-1;i++)
{
System.out.println(x[i]);
}
}
}
Output
Enter 10 Integer values
15
1234567891
1212
1212
12
1
21
21
2
12
12
After Sorting
1212
1212
1212
21
21
12
12
12
12
2
16
VI) ARRAYS
An array is a collection of variables of the same type that are referenced by a
commonname.This is called arrays.
Types of Arrays
1)Single (one) dimensional array:
The simplest form of an array is a single dimendional array. The array is given a name and
its elements are referred to by their subscripts or indices.
Syntax:
type array-name[ ] = new type [size]; or type[ ] arrayname = new type [size];
Example:
int marks [ ]=new int [50];
2 )Multi-dimensional Arrays
A two-dimensional array is an array in which each element is itself an array. For instance, an
array A [M] [N] is an M by N table with M rows and N columns containing M x N elements.
Syntax:
type array-name [ ] [ ] = new type [row] [columns];
Example:
int sales [ ] [ ]=new int [5] [12]; columns rows
Searching in 1-D Array
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
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 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.
17
PROGRAM:1
Write a program to accept the year of graduation from school as an integer value from the
user. Using the Binary Search technique on the sorted array of integers given below,
Output the message “Record exists” If the value input is located in the array. If not, output
the message “Record does not exist”.
{1982, 1987, 1993, 1999, 2003, 2006, 2007, 2009, 2010}
import java.io.*;
class Search
{
A[]={1982, 1987, 1993, 2003, 2006, 2007, 2009, 2010};
int n, l, h, mid, flag=0;
void display()throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter year”);
n=Integer.parseInt(br.readLine());
l=0;
h=A.length-1;
while(1<=h)
{
mid=(l+h)/2;
if(n>A[mid])
{
l=mid+1
}
else if(n<A[mid])
{
h=mid-1;
}
else
{
flag=1;
break;
18
}
}
if(flag==1)
{
System.out.println(“Record exists”);
}
else
{
System.out.println(“Record does not exists”);
}
}
}
Input: 1994
Output: Record does not exists
PROGRAM :2
A program for searching in 1-D arrays
Public class Ar2
{
Public void recruitmentprocess(float people[])
}
Inti;
Floatavg=0,total=0;
for(i=0;i<7;i++)
total+=recruitment[i];
avg=total/5;
System.out.println(“Total recruitment=”+ total);
System.out.println(“Average recruitments=”+ avg);
}
}
Output:
19
Total recruitment=1590.0
Average recruitments=318.0
Sorting
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.
Selection Sort:
Selection Sort is a sorting technique where next smallest (or next largest) element is
foundin the array and moved to its correct position (find position) e.g., the smallest element
should be at 1st position (for ascending array), second smallest should be at 2nd position and
so on.
Bubble Sort:
The idea of BubbleSort is to move the largest element to the highest index position in the
array.To attain this, two adjacent elements are comared repeatedly and exchanged if they
are not in correct order.
PROGRAM:3
Write a program to input 10 integer elements in an array and sort them in descending
order using the bubble sort technique.
import java.io.*;
class Arrange
{
int A[]=new int[]10;
intI,j,t;
void display()throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<10;i++)
{
System.out.println(“Enter a no.”);
A[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<10;i++)
{
for(j=0;j<9-I;j++)
{
if(A[j]<A[j+1])
20
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
for(i=0;i<10;i++)
{
System.out.println(A[i]);
}
}
}
21
VII) SPECIAL NUMBERS
PROGRAM:
Write a program to input a number and print whether a number is a special number or
not.
import java.io.*;
class Special
{
int s=0, n, r, f, I, t;
void check()throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a no.”);
n=Integer.parseInt(br.readLine());
t=n;
while(n>0)
{
r=n%10;
f=1;
for(i=1;i<=r;i++)
{
f=f*i;
}
s=s+f;
n=n/10;
}
if(s==t)
{
System.out.println(“it is a special number.”);
}
else
{
System.ot.println(“Not a special no.”);
}
22
}
}
Input: 145
Output: it is a special number
23
BIBLIOGRAPHY
www.Google.com
ICSE solved papers last10 years
Computer Applications text book.
http://amanjava.blogspot.in/
http://www.guideforschool.com
http://www.icsej.com/
24