3 Methods in Java
3 Methods in Java
__________________________________________________________
Modularize the tasks of the program or structure the program.
Functions separate the concept (what is done) from the implementation (how it is
done).
Note:-( ) Denotes for creating method
Defining a Method:
– A method definition consists of its method name, parameters, return value type, and body.
– The syntax for defining a method is:
Now you defined a method, you need to use it. For that, you have to call the method.
Here's how:
myMethod();
This statement calls the myMethod() method that was declared earlier.
A Java method can have zero or more parameters. And, they may return a value.
class SquareMain {
public static void main(String[] args) {
int result;
result = square();
System.out.println("Squared value of 10 is: " + result);
}
Exercise
__________________________________________________________
Java program which illustrates a square of any number accepted from keyboard using method.
Output
public class ArithematicMain {
10 + 20 = 30
20 x 40 = 800
// method defined
private static int getSquare(int x){
return x * x;
}
// method call
result = getSquare(i)
System.out.println("Square of " + i + " is : " + result); }
}
}
Square of 1 is : 1
Square of 2 is : 4
Square of 3 is : 9
Square of 4 is : 16
Square of 5 is : 25
Example
Sum of two integer number using method
EXERCISE
Maximum of two number using method in java
Method overloading
__________________________________________________________
Two or more METHODS can have the same name but different parameters.
Process of using the same name for two or more METHODS.
Used so that a programmer does not have to remember multiple METHOD names.
Array in Java
Array is a collection of similar type of elements that have contiguous memory location.
Java array is an object that contains elements of similar data type. We can store fixed of elements
on a java array.
Array in java is index based, first element of the array is stored at 0 index.
An array is a container that holds data (values) of one single type. For example, you can
create an array that can hold 100 values of int type.
One-dimensional arrays.
How to declare an array?
Here's how you can declare an array in Java:
datatype age [ ]
The length of the array is determined by the number of values provided which is separated by commas. In
our example, the length of age array is 5.
You can easily access and alter array elements by using its numeric index. Let's take an
example.
class ArrayExample {
public static void main(String[] args) {
age[2] = 14;
age[0] = 34;
Element at index 0: 34
Element at index 1: 0
Element at index 2: 14
Element at index 3: 0
Element at index 4: 0
Element at index 0: 12
Element at index 1: 4
Element at index 2: 5
Element at index 3: 2
Element at index 4: 5
class SumAverage {
public static void main(String[] args) {
Sum = 36
Average = 3.6
Multidimensional Arrays
In Java, you can declare an array of arrays known as multidimensional array. Here's an
example to declare and initialize multidimensional array.
Here, a is a two-dimensional (2d) array. The array can hold maximum of 12 elements of
type int.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
Exercise
What is output the “ff” code fragment?
Exercise
Exercise
Write a c++ program that accepts 2*2 array and print the given array Element.
The following loop initializes the array with user input values:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + "
columns: ");
for(int row = 0; row <matrix.length; row++){
for(int column = 0; column <matrix[0].length; column++){
matrix[row][column] = input.nextInt();
}
}
Example: Program to Add Two Matrices
public class AddMatrices {
-2 8 7
10 8 6
Example: Program to Multiply Two Matrices using a
Function
public class MultiplyMatrices {
StringTokenizer Class
• The StringTokenizer class allows an application to break a string into tokens.
• When you read a sentence, your mind breaks it into tokens—individual words and
punctuation marks that convey meaning to you.
• Tokens are separated from one another by delimiters, typically white-space characters
such as space, tab, newline and comma.
String is a class in java, which provides some of the predefined methods that
make string based problem solutions easier. We don’t need to write code for
every operation, we have to just use its methods.