FILES
FILES
Signature: Signature:
2.)METHOD OR FUNCTON
(THEORY, ALGORITHM,FLOWCHART & PROGRAM)
3.) ARRAYS
(THEORY, ALGORITHM, FLOWCHART & PROGRAM)
4.) CONSTRUCTOR
( THEORY, ALGORITHM, FLOWCHART & PROGRAM)
7.) INHERITANCE
B led to C,C evolved to C++,and C++ set the stage for java.
A program module or a part of the program used simultaneously at different
instances in the program is known as Method or Function. In Java programs, the
executable instructions are specified through methods or functions.
The general form of function definition is as given below:
[Access specifier] [Modifier] return-type function name (parameter list)
{
Body of the function
}
Access specifier used to determine the type of access to the function. It can be either
public or protected or private.
Modifier can be one of: final, native, synchronized, transient, volatile.
Return_type specifies the type of value that the function returns. It may be of any valid
type data_type.If no value is being returned, it should be void.
A Function may be without any parameters, in which case, the parameter list is empty. A
function is invoked in two manners:Call by Value and Call by Reference. Basically these
two ways of invoking
Functions are also referred to as Pass by Value and Pass by Reference. These are
discussed in brief as under:
intact.Thus,in call by value method,the changes are not reflected back to the original
values.
CALL BY REFRENCE
In call by value method,the called function creates a new set of variables and copies
the values of arguments into them. The function does not have access to the original
variable and can only work on the copies of value it created. The call by value method
also offers insurance that the function can’t harm the original value. The call by reference
method uses a different mechanism. In place of passing a value to the function being
called a reference to the original variable.Thus,in call by reference method the changes are
reflected back to the original values.
PROGRAM
A cloth showroom has announced the following festival discounts on the purchase of
items, based on the total cost of the items purchased:
Write a program to input the total cost and to compute and display the amount to paid
be
customer after availing the discount.
//To compute festival discount
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class DiscountFest {
public static void main(String[] args) throws IOException {
System.out.println("Enter the no. of items you have purchased:");
BufferedReader bf;
bf= new BufferedReader(new InputStreamReader(System.in));
String InputValue = (String) bf.readLine();
int ItemCount = Integer.parseInt(InputValue);
double totalCost = 0;
for (int i=0;i<ItemCount;i++)
{
System.out.println("Enter the cost of item" + (i+1)+":");
bf = new BufferedReader(new InputStreamReader(System.in));
InputValue = (String) bf.readLine();
totalCost += Integer.parseInt(InputValue);
}
System.out.println("Total is cost is: " + totalCost);
if(totalCost<=2000)
{
totalCost =totalCost - totalCost*0.05;
System.out.println("Amount to pay after 5% Discount is: " +
totalCost);
}
else if(totalCost>2000 && totalCost<=5000)
{
totalCost =totalCost - totalCost*0.25;
System.out.println("Amount to pay after 25% Discount is: " +
totalCost);
}
else if(totalCost>5000 && totalCost<=10000)
{
totalCost =totalCost - totalCost*0.35;
System.out.println("Amount to pay after 35% Discount is: " +
totalCost);
}
else if(totalCost>10000)
{
totalCost =totalCost - totalCost*0.5;
System.out.println("Amount to pay after 50% Discount is: " +
totalCost);
}
}
}
Searching:-
The process of finding a particular element of an array is called searching.
Types of searching:
There are two types of searching:-
1.Linear Search:- Linear search examines each value in turn until the target is found or
the end of the list is reached.
2.Binary Search:-The binary search algorithm requires that the list is sorted,but peovides
significant improvements in efficiency over a linear search.
Sorting Arrays:-
Sorting of an array means arranging the array elements in a specified order.
Types of sorting:-
1.Linear Sort:- This sorting method is based on comparing and exchanging the top
element with least value,until all elements in an array are stored.
2.Bubble Sort:-This sort method is based on comparing and exchanging pairs of adjacent
elements,until all the elements in an array are sorted.
3.Selection sort:- Selection sort works by putting each value into it’s final position one at
a time.
4.Insertion sort:-Insertion sort works by inserting each value into a previously sorted
subset of the list.
FLOW CHART
//x[0]=5;x[1]=3;x[2]=8;x[1]=3;x[1]=3;x[1]=3;x[1]=3;x[1]=3;x[1]=3;x[1]=3;
int x[]={5,3,8,4,9,2,1,12,98,16};
int n = x.length;
// exchange elements
System.out.println(x[i]);
}
The constructors has the same name as the class.The constructors has no return
value specification not even void.they are certainly not instance methods.since they don’t
belong to object,since they are responsible for creating objects,they exist before any
objecthas been created.According to the java language specifications,they are technically
not members of the class at all in particular,constructors are not referred to as “methods”.
CHARACTERISTICS:
i. It has no return type not, even void.
ii. It is called when the object is created.
iii. It has function name same as the name as its class.
iv. It can be called either implicity or explicity.
:-Types of constructors:
There are two types of constructors:
1. Parameterized constructors:- the parameterized constructors can pass value into the
constructors that have different parameters in the constructors functions.
For example, the constructor declared as:
Teacher(String,String,String,Double)// parameterized constructors means that the
teacher constructors has four parameter’s in its body. There would be four values or four
variables values would be passed to theparameterized constructor.
2. Non-parameterized constructors:-These are also known as default constructors because
it declares no parameters. The constructor can declare its function body either inside the
class as:
Teacher( )
{
Salary = 8000;
}
The aforementioned default constructor initializes a value 8000 to the private data
member salary.
To avoid confusion for the compiler with same name of the instance variable. The this
keyword can be implicit or explicit . In implicit this java itself uses keyword this to refer
to data member of object.
intrest (double a)
ROI = a;
return( (x*y*ROI)/100);
{
intrest i=new intrest(5.2);
double SI=0.0;
SI=i.simpleintrest(1000,3);
THE WHILE STATEMENT: - The while statement is one of several java construct
statement. A construct is a programming language statement of a series of statements
that controls looping. The while is a looping statement. Looping statement controls
execution of a series of other statement, causing parts of a program to execute repetectly
as long as a certain statement is end.
REMARKS