-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMethods.java
More file actions
83 lines (69 loc) · 2.52 KB
/
JavaMethods.java
File metadata and controls
83 lines (69 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class JavaMethods {
// method block of reusable code that is executed when called ()
//creating a method
/*
void methodNmae(){
statements
}
since the method will be called in the main method which is static then the static key wordis used in creatin the new mwthod hence
static void methodName(){
statement
}
*/
public static void main (String []args){
//calling the method
happyBirthday();
// calling method with parameters ,, passing arguments to method call
// the argument can also be a variable in the calling method as lomg as its set in the parameter
int age=25;
happyBirthday2("john",age);
// calling the method with return
double result =square(3);
System.out.println(result);
System.out.println(getFullname("john ","Muriungi"));
//age check
if(ageCheck(age)){
System.out.println("You may sign up now");
}
else{
System.out.println("You have to be 18+");
}
}
static void happyBirthday(){
System.out.println("Happy birthday to you!");
System.out.println("Happy birthday dear you!");
System.out.println("you are X years old");
System.out.println("Happy birthday to you!\n");
}
// method with parameters
// remember to pass the data type on the parameters
// parameters are comma separated
// the name of the parameters can be different from the arguments name
// as long as the data type and the order remain eg String name could be person and it will still workout
static void happyBirthday2(String name, int age){
System.out.println("Happy birthday to you!");
System.out.printf("Happy birthday dear %s!\n",name);
System.out.printf("you are %d years old\n",age);
System.out.println("Happy birthday to you!\n");
}
//using a method that returns something
// use the datatype in the method header insteadof void
// eg double square()
static double square(double number){
System.out.println("using a method that returns a value: ");
return number *number ;
}
// a method that returns the full name
static String getFullname(String first, String last){
return first +" "+last;
}
// method to check age of someone
static boolean ageCheck(int age){
if(age>=18){
return true;
}
else{
return false;
}
}
}