-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_10_methods.java
More file actions
65 lines (44 loc) · 2.29 KB
/
Copy pathDay_10_methods.java
File metadata and controls
65 lines (44 loc) · 2.29 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
package Programing.Section4;
public class Day_10_methods {
public static void main(String[] args) {
boolean gameover = true;
int score = 800;
int levelcompleted = 5;
int bonus = 100;
calculatescore( gameover, score, levelcompleted, bonus); // result/output : first line finalscore by this manner. (variables defined in main)
calculatescore(true,800,5,200); //second line finalscore by this method (void , no return)
calculatescore(true,800,5,300);
calculatescore(true, 1000, 4); // fourth line i.e (2nd score) by this method (int, return type)
int playerA = calculatescore(true, 3000);
int playerB = calculatescore(true,5500);
int teampoint = playerA + playerB;
System.out.println("teampoint of A and B is :"+teampoint); // fourth line and th return value of playerA and player B is used.
}
public static void calculatescore(boolean gameover, int score, int levelcompleted, int bonus){
if (gameover){
int finalscore = score + (levelcompleted*bonus); // void method
finalscore += 2000;
System.out.println("your finalscore is: "+finalscore);
}
}
// if we have used 'int' instead of 'void' before 'calculatescore' then we have to return a value too.
// and also we have to make sure that all the possible outcomes are covered in our code in a method.
// eg:-
public static int calculatescore (boolean gameover , int score, int levelcompleted){
if (gameover){
int finalscore = score; // int method
System.out.println("2nd score (by int return type): "+score);
return finalscore;
}
return -1;
}
// by doing this we can also give this method a variable name in main and we can then can use its value for some another part of code.
// refer to main method
public static int calculatescore (boolean gameover , int score){
if (gameover){
int finalscore = score; // int method and the return value is assigned to a variable and teamscore is calculated.
return finalscore;
}
return -1;
}
}