-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathExercise.java
More file actions
167 lines (146 loc) · 5.35 KB
/
Exercise.java
File metadata and controls
167 lines (146 loc) · 5.35 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.booleanuk.core;
public class Exercise {
/*
One of the core building blocks of programming in any language is conditional statements.
We ask the computer yes / no questions and perform things based on the answer, e.g.
Do we have milk?
If yes, make a coffee
Else buy some milk
We represent yes and no in code using booleans: true / false. If something is true, do something.
We ask these questions in this format:
if (a condition is true) {
do the thing inside these curly brackets
} else {
the condition was false, so do the thing inside these brackets instead
}
The result of the yes / no question goes into the "a condition is true" bit. This is usually the result
of making some kind of comparison, e.g. checking that 1 < 5. Running 1 < 5 will result in the boolean, true.
Running 1 > 5 will result in the boolean, false.
if (1 < 5) {
// 1 is less than 5, so the condition is true and this block runs
} else {
// This else block will not run because the condition tested is true, not false
}
See the example below, study it and spend some time understanding it before moving on.
*/
public String sayGoodMorning(boolean isMorning) {
if (isMorning) {
return "Good morning!";
} else {
return "Good day!";
}
}
// 1. What will the returned value be if I run sayGoodMorning(false)?
// Change the returned value in the method below to your answer. It is case-sensitive.
public String one() {
return "Good day!";
}
// 2. What will the output be if I run sayGoodMorning(true)?
// Change the returned value in the method below to your answer. It is case-sensitive.
public String two() {
return "Good morning!";
}
// 3. What will the output be if I run sayGoodMorning("Hello".equals("Hello"))?
// Change the returned value in the method below to your answer. It is case-sensitive.
public String three() {
return "Good morning!";
}
// 4. What will the output be if I run sayGoodMorning(!"A word".equals("Another word"))
public String four() {
return "Good morning!";
}
// 5. What will the output be if I run sayGoodMorning(25 != 25)
public String five() {
return "Good day!";
}
// 6. Use a conditional statement to return "Correct!" if the input is more than 7
// or "Wrong!" if not
public String six(int num) {
if (num > 7){
return "Correct!";
}
return "Wrong!";
}
// 7. Use a conditional statement to return "Correct!" if the input is false
// or "Wrong!" if not
public String seven(boolean bool) {
if (bool == false){
return "Correct!";
}
return "Wrong!";
}
// 8. Use a conditional statement to return "Correct!" if numOne is more than or equal to numTwo
// or "Wrong!" if not
public String eight(int numOne, int numTwo) {
if (numOne >= numTwo){
return "Correct!";
}
return "Wrong!";
}
// 9. Use a conditional statement to return true if the array provided is not empty
// or false if it is empty
public boolean nine(int[] nums) {
if (nums.length != 0){
return true;
}
return false;
}
// 10. Use a conditional statement to return true if the provided string contains the word
// "milk", or false if not
// https://www.w3schools.com/java/java_ref_string.asp
public boolean ten(String sentence) {
if (sentence.contains("milk")){
return true;
}
return false;
}
// 11. Use conditional statements to return the number 3 if the provided string contains
// the word milk. Return the number 6 if the provided string contains the word coffee.
// Return the number 9 if the string contains both coffee and milk.
// Otherwise, return the number 0.
public int eleven(String sentence) {
if (sentence.contains("milk") && sentence.contains("coffee")){
return 9;
}
if (sentence.contains("milk")){
return 3;
}
if (sentence.contains("coffee")){
return 6;
}
return 0;
}
// 12. Use conditional statements to return true if num is more than or equal to lower and is
// less than or equal to upper, otherwise return false.
public boolean twelve(int num, int lower, int upper) {
if (num >= lower && num <= upper){
return true;
}
return false;
}
/*
12. Use conditional statements to return a string based on what the age parameter is.
The table below shows the string that should be returned for each range of values that age can be.
For example: If age is 3, you should return "Toddler"
0 | Baby
1-4 | Toddler
5-12 | Child
13-19 | Teenager
20+ | Adult
*/
public String thirteen(int age) {
if (age == 0){
return "Baby";
}
if (age <= 4){
return "Toddler";
}
if (age <= 12){
return "Child";
}
if (age <= 19){
return "Teenager";
}
return "Adult";
}
}