0% found this document useful (0 votes)
6 views3 pages

Java Methods: Built-in & User-defined

The document provides an overview of Java methods, categorizing them into built-in and user-defined methods. It includes examples of method syntax, static methods, method overloading, the 'this' keyword, recursive functions, and variable arguments. Additionally, it illustrates the use of default values in methods and provides specific examples for better understanding.

Uploaded by

laila.alheilat
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)
6 views3 pages

Java Methods: Built-in & User-defined

The document provides an overview of Java methods, categorizing them into built-in and user-defined methods. It includes examples of method syntax, static methods, method overloading, the 'this' keyword, recursive functions, and variable arguments. Additionally, it illustrates the use of default values in methods and provides specific examples for better understanding.

Uploaded by

laila.alheilat
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

Lab 4 - Java Methods

Java Methods Classified As:


1. Built-in Methods

2. User-defined Methods

Built-in Methods:
abs(x), ceil(x), cos(x), exp(x), floor(x), log(x), max(x,y), min(x,y), pow(x,y), sin(x), sqrt(x),
tan(x)

Note: [Link](x)

User-defined Methods:
1. Return Value Methods

2. Return Control Methods (void)

Syntax:

modifier returnType methodName(Parameter List) {


// method body
}

Static Keyword:
static → can be called without creating an object, using class name.

Example 1: minFunction
public static int minFunction(int n1, int n2) {
int min;
if(n1 > n2)
min = n2;
else
min = n1;
return min;
}

Example 2: Rank
public static void Rank(double x) {
if (x > 90)
[Link]("Excellent");
else if (x > 80)
[Link]("Very Good");
else
[Link]("Good");
}

Example 3: Swap
public static void Swap(int a, int b) {
int c = a;
a = b;
b = c;
[Link]("After swapping: a=" + a + " b=" + b);
}

Method Overloading:
Same method name, different parameters.

public static double minFunction(double x, double y) {


double min;
if(x > y)
min = y;
else
min = x;
return min;
}

This Keyword:
Used to refer to the current object, especially inside constructors.

Class Student:
class Student {
int a = 3;
int b = 5;

Student() {}

Student(int x) {
this(10, 10);
}

Student(int a, int b) {
this.a = a;
this.b = b;
}

public void print() {


[Link]("a=" + this.a + " b=" + this.b);
}
}

Recursive Function:
static int sum(int x, int y) {
if (y == 0)
return x;
else
return y + sum(x, 0);
}

Method Default Values:


static void print() { print(10); }
static void print(int x) { print(x, 20); }
static void print(int x, int y) {
[Link](x + " + " + y);
}

Variable Arguments:
public static void printMax(double... numbers) {
if([Link] == 0) {
[Link]("No arguments passed!");
return;
}
double result = numbers[0];
for(int i = 1; i < [Link]; i++) {
if(numbers[i] > result)
result = numbers[i];
}
[Link]("Max = " + result);
}

You might also like