Spaces:
Running
Running
Create 3A. Parameters
Browse files
Week 2: Methods, strings and lists/3A. Parameters
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
public static void printLarger(int num1, int num2) {
|
| 2 |
+
if (num1 > num2) {
|
| 3 |
+
System.out.println(num1);
|
| 4 |
+
} else {
|
| 5 |
+
System.out.println(num2);
|
| 6 |
+
}
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
public static void main(String[] args) {
|
| 11 |
+
printLarger(10,4);
|
| 12 |
+
printLarger(111, 11111);
|
| 13 |
+
printLarger(5 * 5, 3 * 9);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
Program outputs:
|
| 17 |
+
10
|
| 18 |
+
11111
|
| 19 |
+
27
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
====================
|
| 23 |
+
|
| 24 |
+
parameter data type
|
| 25 |
+
eg double
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
public class Example{
|
| 30 |
+
public static void main(String[] args) {
|
| 31 |
+
double a;
|
| 32 |
+
a = 4.0;
|
| 33 |
+
a = 24;
|
| 34 |
+
float f = 23.32f;
|
| 35 |
+
a = f;
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
========================================
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
public class Example {
|
| 49 |
+
public static void main(String[] args) {
|
| 50 |
+
printSquare(3.5); //accept double
|
| 51 |
+
printSquare(10); //accept int
|
| 52 |
+
printSquare(1.5f);//accept float
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
public static void printSquare(double num) {
|
| 56 |
+
System.out.println(num * num);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
Program outputs:
|
| 61 |
+
12.25
|
| 62 |
+
100.0
|
| 63 |
+
2.25
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
========================================
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
Method parameters can also be of different types, for example:
|
| 74 |
+
|
| 75 |
+
public class Example {
|
| 76 |
+
public static void main(String[] args) {
|
| 77 |
+
tempBetween(10, 30, 25.5);
|
| 78 |
+
tempBetween(-5, 5, -15.25);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
public static void tempBetween(int min, int max, double temp) {
|
| 82 |
+
if (temp >= min && temp <= max) {
|
| 83 |
+
System.out.println("Temperature is between the given values!");
|
| 84 |
+
} else {
|
| 85 |
+
System.out.println("Temperature is not between the given values.");
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
Program outputs:
|
| 90 |
+
|
| 91 |
+
Temperature is between the given values!
|
| 92 |
+
Temperature is not between the given values.
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|