Page 193 - computer science (868) class 11
P. 193
08 METHODS AND
CONSTRUCTORS
Learning Objectives
8.1 User-Defined Methods 8.2 Need for using a Method
8.3 Parts of a Method 8.4 Actual and Formal Parameters
8.5 Defining a Function 8.6 Calling a method
8.7 Pure Method and Impure Method 8.8 Method Overloading
8.9 Objects and Classes 8.10 new Operator
8.11 this Keyword 8.12 Constructor
As explained in the previous chapter, there are various built-in methods in Java that are already defined and a
programmer only has to use them in the correct portion of the coding to obtain the required result. Apart from
built-in methods, there are also user-defined methods. These methods are declared and defined by the programmer
according to the need of the problem.
8.1 USER-DEFINED METHODS
Sometimes, we may come across certain problems which require executing some tasks repeatedly, but with different
input values. This is exactly the situation where we use user-defined methods.
Let us take an example.
Program 1 Write a program in Java to print the sum and average of two marks 10 and 30 obtained by a
student. Also, the same operation is required to be done for the other student who got 20
and 35 marks.
1 class sum_average
2 {
3 public static void main()
4 {
5 int m1=10, m2=30, n1=20, n2=35, sum;
6 double avg;
7 sum = m1+m2;
191
Methods and Constructors 191

