1818 * If statements are not specifically covered in the lesson this module, but review module 2 flow control section for an overview if needed.
1919 * Resources: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_if_else.htm
2020 *
21- * @author Your Name
21+ * @author Irene Schild
2222 */
2323
2424public with sharing class VariablesDataTypesOperators {
@@ -32,7 +32,12 @@ public with sharing class VariablesDataTypesOperators {
3232 * @return The sum of the two numbers, or null if either number is null.
3333 */
3434 public static Integer addition (Integer a , Integer b ) {
35- return null ; // Replace null with the variable you used to store the result
35+
36+ if (a == null || b == null ) {
37+ return null ;
38+ }
39+
40+ return a + b ;
3641 }
3742
3843 /**
@@ -44,7 +49,12 @@ public with sharing class VariablesDataTypesOperators {
4449 * @return The difference between the two numbers.
4550 */
4651 public static Integer subtraction (Integer a , Integer b ) {
47- return null ; // Replace null with the variable you used to store the result
52+
53+ if (a == null || b == null ) {
54+ return null ;
55+ }
56+
57+ return a - b ;
4858 }
4959
5060 /**
@@ -56,7 +66,12 @@ public with sharing class VariablesDataTypesOperators {
5666 * @return The product of the two numbers.
5767 */
5868 public static Integer multiplication (Integer a , Integer b ) {
59- return null ; // Replace null with the variable you used to store the result
69+
70+ if (a == null || a == 0 || b == null || b == 0 ) {
71+ return null ;
72+ }
73+
74+ return a * b ; // Replace null with the variable you used to store the result
6075 }
6176
6277 /**
@@ -69,7 +84,12 @@ public with sharing class VariablesDataTypesOperators {
6984 * @return The quotient of the division, or 0 if the denominator is zero.
7085 */
7186 public static Double division (Double a , Double b ) {
72- return null ; // Replace null with the variable you used to store the result
87+
88+ if (a == null || a == 0 || b == null || b == 0 ) {
89+ return 0 ;
90+ }
91+
92+ return a / b ; // Replace null with the variable you used to store the result
7393 }
7494
7595 /**
@@ -83,7 +103,8 @@ public with sharing class VariablesDataTypesOperators {
83103 * @return True if the number is even, False otherwise.
84104 */
85105 public static Boolean isEven (Integer num ) {
86- return null ; // Replace null with the variable you used to store the result
106+
107+ return Math .mod (num , 2 ) == 0 ;
87108 }
88109
89110 /**
@@ -94,7 +115,7 @@ public with sharing class VariablesDataTypesOperators {
94115 * @return true if the number is positive, false otherwise.
95116 */
96117 public static Boolean isPositive (Integer num ) {
97- return null ; // Replace null with the variable you used to store the result
118+ return num >= 1 ;
98119 }
99120
100121 /**
@@ -106,7 +127,7 @@ public with sharing class VariablesDataTypesOperators {
106127 * @return The concatenated string.
107128 */
108129 public static String concatenateStrings (String str1 , String str2 ) {
109- return null ; // Replace null with the variable you used to store the result
130+ return str1 + str2 ;
110131 }
111132
112133 /**
@@ -121,7 +142,7 @@ public with sharing class VariablesDataTypesOperators {
121142 * @return The complete sentence as a single String.
122143 */
123144 public static String createSentence (String noun , String verb , String endingPunctuation ) {
124- return null ; // Replace null with the variable you used to store the result
145+ return ' The ' + noun + ' is ' + verb + endingPunctuation ;
125146 }
126147
127148 /**
@@ -134,7 +155,7 @@ public with sharing class VariablesDataTypesOperators {
134155 * @return True if the date is in the past, False otherwise.
135156 */
136157 public static Boolean isDateInPast (Date dt ) {
137- return null ; // Replace null with the variable you used to store the result
158+ return Date . today () > dt ; // Replace null with the variable you used to store the result
138159 }
139160
140161 /**
@@ -147,7 +168,7 @@ public with sharing class VariablesDataTypesOperators {
147168 * @return True if the date is today or in the future, False otherwise.
148169 */
149170 public static Boolean isDateTodayOrFuture (Date dt ) {
150- return null ; // Replace null with the variable you used to store the result
171+ return Date . today () < dt ;
151172 }
152173
153174 /**
@@ -159,10 +180,16 @@ public with sharing class VariablesDataTypesOperators {
159180 * @param minutes The number of minutes.
160181 * @return The number of milliseconds equivalent to the given number of minutes.
161182 */
162- Integer MILLISECONDS_PER_MINUTE = null ; // Make this value a constant
183+
163184 public static Integer convertMinutesToMilliseconds (Integer minutes ) {
164- Integer milliseconds ;
165- return null ; // Replace null with the variable you used to store the result
185+
186+ if (minutes == null ) {
187+ return null ;
188+ }
189+ Integer MILLISECONDS_PER_MINUTE = 60000 ;
190+
191+ Integer milliseconds = minutes * MILLISECONDS_PER_MINUTE ;
192+ return milliseconds ;
166193 }
167194
168195 /**
@@ -176,7 +203,14 @@ public with sharing class VariablesDataTypesOperators {
176203 * @return The average of the three numbers.
177204 */
178205 public static Double averageOfThreeNumbers (Integer a , Integer b , Integer c ) {
179- return null ; // Replace null with the variable you used to store the result
206+
207+ Double sumOfNumbers = a + b + c ;
208+
209+ if (a == null || b == null || c == null || sumOfNumbers == 0 ) {
210+ return 0 ;
211+ }
212+
213+ return sumOfNumbers / 3 ; // Replace null with the variable you used to store the result
180214 }
181215
182216 /**
@@ -190,11 +224,16 @@ public with sharing class VariablesDataTypesOperators {
190224 * @return The result of the adjusted arithmetic expression.
191225 */
192226 public static Integer adjustOrderOfOperations1 (Integer a , Integer b , Integer c ) {
227+
228+ if (c == null ) {
229+ return null ;
230+ }
231+
193232 // Add parentheses around the addition operation so that it is performed before multiplication
194- Integer result = a + b * c ;
233+ Integer result = ( a + b ) * c ;
195234
196235 // Return the result
197- return null ; // Replace null with the variable you used to store the result
236+ return result ;
198237 }
199238
200239 /**
@@ -206,8 +245,8 @@ public with sharing class VariablesDataTypesOperators {
206245 // Add parentheses in the below expression to change the result.
207246 // The result of the expression as it is right now is 43.
208247 // You should add parentheses so that the result of the expression becomes 8.
209- Integer answer = 48 - 15 + 5 * 2 ;
210- return null ; // Replace null with the variable you used to store the result
248+ Integer answer = 48 - ( 15 + 5 ) * 2 ;
249+ return answer ;
211250 }
212251
213252 /**
@@ -226,9 +265,14 @@ public with sharing class VariablesDataTypesOperators {
226265 */
227266 public static Double complexOrderOfOperations (Integer a , Integer b , Integer c , Integer d , Integer e ) {
228267 // Add parentheses around the multiplication and subtraction operations so that they are performed before division and addition
229- Double result = a * b - c / (Double ) d + e ;
230268
231- return null ; // Replace null with the variable you used to store the result
269+ if (d == null ) {
270+ return null ;
271+ }
272+
273+ Double result = ((a * b ) - c ) / (Double ) d + e ;
274+
275+ return result ; // Replace null with the variable you used to store the result
232276 }
233277
234278 /**
@@ -242,8 +286,12 @@ public with sharing class VariablesDataTypesOperators {
242286 final Double SUBTRACT_FACTOR = 32.0 ;
243287 final Double MULTIPLY_FACTOR = 5.0 ;
244288 final Double DIVIDE_FACTOR = 9.0 ;
245-
246- return null ; // Replace null with the variable you used to store the result
289+
290+ if (fahrenheit == null ) {
291+ return 0 ;
292+ }
293+
294+ return (fahrenheit - SUBTRACT_FACTOR ) * MULTIPLY_FACTOR / DIVIDE_FACTOR ;
247295 }
248296
249297
@@ -258,12 +306,17 @@ public with sharing class VariablesDataTypesOperators {
258306 * @return The quotient as a casted (round down) Integer.
259307 */
260308 public static Integer performDivisionAndCast (Double a , Double b ) {
309+
310+ if (a == null || a == 0 || b == null || b == 0 ) {
311+ return null ;
312+ }
313+
261314 // Perform the division and cast (round down) off the result.
262- Double divisionResult = null ;
315+ Double divisionResult = a / b ;
263316
264317 // Write the code for type casting the divisionResult to an Integer
265- Integer roundedResult = null ;
266- return null ; // Replace null with the variable you used to store the result
318+ Integer roundedResult = ( Integer ) divisionResult ;
319+ return roundedResult ;
267320 }
268321
269322 /**
@@ -276,8 +329,12 @@ public with sharing class VariablesDataTypesOperators {
276329 * @return A Decimal representing the weekly paycheck.
277330 */
278331 public static Decimal calculateWeeklyPaycheck (Decimal hourlyRate , Double numberOfHours ) {
279- // Calculate the weekly paycheck using the formula: rate multiplied by hours
280- return null ; // Replace null with the variable you used to store the result
332+
333+ if (hourlyRate == null || hourlyRate == 0 || numberOfHours == null || numberOfHours == 0 ) {
334+ return 0 ;
335+ }
336+
337+ return hourlyRate * (decimal ) numberOfHours ;
281338 }
282339
283340 /**
@@ -291,7 +348,12 @@ public with sharing class VariablesDataTypesOperators {
291348 * @return A Decimal representing the monthly paycheck.
292349 */
293350 public static Decimal calculateMonthlyPaycheck (Decimal hourlyRate , Double numberOfHours ) {
294- return null ; // Replace null with the variable you used to store the result
351+
352+ if (hourlyRate == null || hourlyRate == 0 || numberOfHours == null || numberOfHours == 0 ) {
353+ return 0.0 ;
354+ }
355+
356+ return hourlyRate * numberOfHours * 4 ;
295357 }
296358
297359 /**
@@ -305,6 +367,11 @@ public with sharing class VariablesDataTypesOperators {
305367 * @return The total cost after applying the sales tax.
306368 */
307369 public static Decimal calculateTotalCost (Decimal pricePerUnit , Integer numberOfUnits , Decimal salesTaxRate ) {
308- return null ; // Replace null with the variable you used to store the result
370+
371+ if (pricePerUnit == null || numberOfUnits == null ) {
372+ return null ;
373+ }
374+
375+ return (pricePerUnit * numberOfUnits ) * (1 + salesTaxRate );
309376 }
310377}
0 commit comments