LEARN
SIMPLE JAVA SUBFUNCTION.
~Courtesy
of internet~
public static int
minFunction(int n1, int n2) {
int
min;
if (n1 > n2)
min = n2;
else
min = n1;
return
min;
}
public class ExampleMinNumber {
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
/** returns the minimum of two
numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
|
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void
methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println("Rank:A1");
}else if (points >= 122.4) {
System.out.println("Rank:A2");
}else {
System.out.println("Rank:A3");
}
}
}
|
public class swappingExample {
public static void main(String[] args) { int a = 30; int b = 45; System.out.println("Before swapping, a = " + a + " and b = " + b);
// Invoke the swap method swapFunction(a, b); System.out.println("\n**Now, Before and After swapping values will be same here**:"); System.out.println("After swapping, a = " + a + " and b is " + b); }
public static void swapFunction(int a, int b) { System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2 int c = a; a = b; b = c; System.out.println("After swapping(Inside), a = " + a + " b = " + b); } } |
Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45
|
public class ExampleOverloading {
public static void main(String[] args) { int a = 11; int b = 6; double c = 7.3; double d = 9.4; int result1 = minFunction(a, b);
// same function name with different parameters double result2 = minFunction(c, d); System.out.println("Minimum Value = " + result1); System.out.println("Minimum Value = " + result2); }
// for integer public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1;
return min; }
// for double public static double minFunction(double n1, double n2) { double min; if (n1 > n2) min = n2; else min = n1;
return min; } } |
|
0 comments:
Post a Comment