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  Naveen Tariq  
2222 */  
2323
2424public  with sharing class  VariablesDataTypesOperators  {
@@ -32,7 +32,14 @@ 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+         if  (a ==  null  ||  b  ==  null ){                 //  Check if either of the numbers is null
36+             System .debug (' Numbers are null.' //  If it is then print this message
37+             return  null ;                            //  End the code and return null
38+         } else {
39+             Integer  sum  =  a  +  b ;                    // Otherwise add a and b to get the sum
40+             Systerm .debug  (sum );                    //  Print the sum to the debug log
41+         }
42+         return  sum ;                                 //  Replace null with the variable you used to store the result
3643    }
3744
3845    /**  
@@ -44,8 +51,15 @@ public with sharing class VariablesDataTypesOperators {
4451     * @return  The difference between the two numbers. 
4552     */  
4653    public  static  Integer  subtraction (Integer  a , Integer  b ) {
47-         return  null ; //  Replace null with the variable you used to store the result
54+         if  (a  == null  ||  b  ==  null ){                 //  Good Practise to check if either of the numbers is null
55+             System .debug (' Numbers are null.' //  Print the message
56+             return  null ;                            //  End the code and return null
57+         } else  {
58+             Integer  sub  =  a  -  b ;                     //  Otherwise substract the numbers
59+             System .debug (sub );                     //  Print the result to the debug log
60+         return  sub ;                                //  Replace null with the variable you used to store the result
4861    }
62+ }
4963
5064    /**  
5165     * Question 3 
@@ -56,8 +70,15 @@ public with sharing class VariablesDataTypesOperators {
5670     * @return  The product of the two numbers. 
5771     */  
5872    public  static  Integer  multiplication (Integer  a , Integer  b ) {
59-         return  null ; //  Replace null with the variable you used to store the result
73+     if  (a  ==  null  ||  b  ==  null ){                 //  Good practise to check if either of the numbers is null
74+             System .debug (' Numbers are null.' 
75+         return  null ;                             //  End the code to avoid errors
76+     }else {
77+         Integer  multiply  =  a  *  b ;              //  Multiplay the numbers
78+         System .debug (multiply );                //  Print multplication
79+         return  multiply ; //  Replace null with the variable you used to store the result
6080    }
81+ }
6182
6283    /**  
6384     * Question 4 
@@ -69,8 +90,13 @@ public with sharing class VariablesDataTypesOperators {
6990     * @return  The quotient of the division, or 0 if the denominator is zero. 
7091     */  
7192    public  static  Double  division (Double  a , Double  b ) {
72-         return  null ; //  Replace null with the variable you used to store the result
93+         if  (b  ==  0 ) {                        // Check if the denominator is zero
94+             return  0 ;                        //  if it is then return 0
95+     }else {
96+             Double  divisionResult  =  a  /  b ;  //  The quotient of the division,
97+         return  divisionResult ;              //  Replace null with the variable you used to store the result
7398    }
99+ }
74100
75101    /**  
76102     * Question 5 
@@ -83,7 +109,16 @@ public with sharing class VariablesDataTypesOperators {
83109     * @return  True if the number is even, False otherwise. 
84110     */  
85111    public  static  Boolean  isEven (Integer  num ) {
86-         return  null ; //  Replace null with the variable you used to store the result
112+         Integer  remainder  =  num  /  2 ;    // Check if the number is even or odd.If you divde a nuber by 2 and the remainder is 0 then the number is even.
113+         // Another method; Math.mod (x,y) it divides variable x by y and gives the remainder
114+         // Integer remainder = Math.mod(num, 2);
115+         Boolean  result ;                 // Check true or false
116+         if  (remainder  ==  0 ) {           //  if its 0 its even then return true
117+             result  =  true ;
118+         }else {
119+             result  =  false ;            // Otherwise its false
120+         }
121+         return  remainder ;              //  Replace null with the variable you used to store the result
87122    }
88123
89124    /**  
@@ -94,7 +129,16 @@ public with sharing class VariablesDataTypesOperators {
94129     * @return  true if the number is positive, false otherwise. 
95130     */  
96131    public  static  Boolean  isPositive (Integer  num ) {
97-         return  null ; //  Replace null with the variable you used to store the result
132+         if  (num  ==  null ) {                  //  Step 1: First check if the number is not null to avoid errors
133+         System .debug (' Number is null.' 
134+     }
135+         Boolean  result ;                     // Bollean to checkif the statement is true or false
136+         if  ( num  >  0 ){                      //  Check if number is greater than 0
137+             result  =  true ;                  // Then the number is positive so return true
138+         }else {                              // Otherwise its negative
139+             result  =  false ; 
140+         }
141+         return  result ;                      //  Replace null with the variable you used to store the result
98142    }
99143
100144    /**  
@@ -106,7 +150,9 @@ public with sharing class VariablesDataTypesOperators {
106150     * @return  The concatenated string. 
107151     */  
108152    public  static  String  concatenateStrings (String  str1 , String  str2 ) {
109-         return  null ; //  Replace null with the variable you used to store the result
153+         String  concatenatedString  =  str1  +  str2 ;    // Concatenat/ combine str1 and str2 to remove space in between them
154+         System .debug (concatenatedString );
155+         return  concatenatedString ;                  //  Replace null with the variable you used to store the result
110156    }
111157
112158	/**  
@@ -121,7 +167,9 @@ public with sharing class VariablesDataTypesOperators {
121167	 * @return  The complete sentence as a single String. 
122168	 */  
123169	public  static  String  createSentence (String  noun , String  verb , String  endingPunctuation ) {
124- 		return  null ; //  Replace null with the variable you used to store the result
170+ 		String  sentence  =  ' The' +  noun  +  ' is' +  verb  +  endingPunctuation ; // Form a sentence by concatenating all the variables 
171+         System .debug (' Sentence:' +  sentence );
172+         return  sentence ;                                                  //  Replace null with the variable you used to store the result
125173	}
126174
127175    /**  
@@ -134,7 +182,17 @@ public with sharing class VariablesDataTypesOperators {
134182     * @return  True if the date is in the past, False otherwise. 
135183     */  
136184    public  static  Boolean  isDateInPast (Date  dt ) {
137-         return  null ; //  Replace null with the variable you used to store the result
185+         Date  todayDate  =  Date .today ();      // Fetch today's date
186+         Boolean  dateInPast ;                 // Declare a Boolean variable to check if the date is in the past
187+         if  (dt  ==  null ) {                   //  Good practise to check if the date is null
188+             System .debug (' Date is null.' //  if null print the message
189+             return  false ;                   //  End the code to avoid errors
190+         }else  if  (dt  < todayDate ){           //  if the date is less than today's date then its in the past
191+             dateInPast  =  true ;                      
192+         }else {                              // otherwise it is not in the past
193+             dateInPast  =  false ; 
194+         }
195+         return  dateInPast ;                  //  Replace null with the variable you used to store the result
138196    }
139197
140198    /**  
@@ -147,7 +205,17 @@ public with sharing class VariablesDataTypesOperators {
147205     * @return  True if the date is today or in the future, False otherwise. 
148206     */  
149207    public  static  Boolean  isDateTodayOrFuture (Date  dt ) {
150-         return  null ; //  Replace null with the variable you used to store the result
208+          Date  todayDate  =  Date .today ();     // Fetch today's date
209+         Boolean  dateInFutureOrToday ;        // Declare a Boolean variable to check if the date is infuture or today
210+         if  (dt  ==  null ) {                   //  Good practise to check if the date is null
211+             System .debug (' Date is null.' //  if null print the message
212+             return  false ;                   //  End the code to avoid errors
213+         }else  if  (dt  >=  todayDate ){         //  if the date is equalo or greater thab today's date then its in future or today
214+             dateInFutureOrToday  =  true ;                      
215+         }else {                              // otherwise it is not in future or today
216+             dateInFutureOrToday  =  false ; 
217+         }
218+         return  dateInFutureOrToday ;         //  Replace null with the variable you used to store the result
151219    }
152220
153221    /**  
@@ -159,11 +227,16 @@ public with sharing class VariablesDataTypesOperators {
159227     * @param  minutes  The number of minutes. 
160228     * @return  The number of milliseconds equivalent to the given number of minutes. 
161229     */  
162-     Integer  MILLISECONDS_PER_MINUTE  =  null ; //  Make this value a constant
230+     Integer  MILLISECONDS_PER_MINUTE  =  60000 ; //  Make this value a constant
163231    public  static  Integer  convertMinutesToMilliseconds (Integer  minutes ) {
164-         Integer  milliseconds ;
165-         return  null ; //  Replace null with the variable you used to store the result
166-     }    
232+         if  (minutes  ==  null ) {               //  Good practise to check if the minutes is null
233+             System .debug (' Minutes is null.' 
234+             return  0 ;                       //  End the code to avoid errors and return 0
235+         }else {
236+         Integer  milliseconds  =  minutes  *  MILLISECONDS_PER_MINUTE ; // Multiply the minutes by the constant to get the milliseconds    
237+         return  milliseconds ; //  Replace null with the variable you used to store the result
238+         }    
239+     }
167240
168241    /**  
169242     * Question 12 
@@ -176,8 +249,14 @@ public with sharing class VariablesDataTypesOperators {
176249     * @return  The average of the three numbers. 
177250     */  
178251    public  static  Double  averageOfThreeNumbers (Integer  a , Integer  b , Integer  c ) {
179-         return  null ; //  Replace null with the variable you used to store the result
252+         if  (a  ==  null  ||  b  ==  null  ||  c  ==  null ) { //  Good practise to check if the numbers are null
253+             System .debug ('  Numbers are null.' 
254+             return  0 ;                       //  End the code to avoid errors and return 0
255+         }else {
256+             Double  average  =  (a  +  b  +  c  ) /  (3 );
257+         return  average ; //  Replace null with the variable you used to store the result
180258    }
259+ }
181260
182261    /**  
183262     * Question 13 
@@ -191,10 +270,10 @@ public with sharing class VariablesDataTypesOperators {
191270     */  
192271    public  static  Integer  adjustOrderOfOperations1 (Integer  a , Integer  b , Integer  c ) {
193272        //  Add parentheses around the addition operation so that it is performed before multiplication
194-         Integer  result  =  a  +  b  *  c ;
273+         Integer  result  =  ( a  +  b )  *  c ;
195274
196275        //  Return the result
197-         return  null ; //  Replace null with the variable you used to store the result
276+         return  result ; //  Replace null with the variable you used to store the result
198277    }
199278
200279    /**  
@@ -206,8 +285,8 @@ public with sharing class VariablesDataTypesOperators {
206285        //  Add parentheses in the below expression to change the result.
207286        //  The result of the expression as it is right now is 43. 
208287        //  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
288+         Integer  answer  =  48  -  ( 15  +  5 ) *  2 ;
289+         return  answer ; //  Replace null with the variable you used to store the result
211290    }
212291
213292    /**  
@@ -226,9 +305,9 @@ public with sharing class VariablesDataTypesOperators {
226305     */  
227306    public  static  Double  complexOrderOfOperations (Integer  a , Integer  b , Integer  c , Integer  d , Integer  e ) {
228307        //  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 ;
308+         Double  result  =  ( a  *  b  -  c )  /  (Double ) d  +  e ;  //  I dont understand this Double here and how to check this in Execute Annonymous Window? 
230309
231-         return  null ; //  Replace null with the variable you used to store the result
310+         return  result ; //  Replace null with the variable you used to store the result
232311    }
233312
234313    /**  
@@ -239,14 +318,19 @@ public with sharing class VariablesDataTypesOperators {
239318     * @return  The temperature in Celsius equivalent to the given Fahrenheit temperature. 
240319     */  
241320    public  static  Double  convertFahrenheitToCelsius (Double  fahrenheit ) {
242-         final  Double  SUBTRACT_FACTOR  =  32.0 ;
321+         final  Double  SUBTRACT_FACTOR  =  32.0 ;             // What is final? 
243322        final  Double  MULTIPLY_FACTOR  =  5.0 ;
244323        final  Double  DIVIDE_FACTOR  =  9.0 ;
324+         if  (fahrenheit  ==  null ) {               //  Good practise to check if the numbers are null
325+             System .debug (' Fahrenheit is null.' 
326+            return  null ;
327+         }else {
328+         Double  celsius  =  ((fahrenheit  -  SUBTRACT_FACTOR ) *  MULTIPLY_FACTOR  /  DIVIDE_FACTOR );
245329
246-         return  null ; //  Replace null with the variable you used to store the result
330+         return  celsius ; //  Replace null with the variable you used to store the result
331+         }
247332    }
248333
249-     
250334
251335    /**  
252336     * Question 17 
@@ -260,10 +344,15 @@ public with sharing class VariablesDataTypesOperators {
260344    public  static  Integer  performDivisionAndCast (Double  a , Double  b ) {
261345        //  Perform the division and cast (round down) off the result.
262346        Double  divisionResult  =  null ;
263- 
347+          if  (b  ==  null ) {                       //  Check if the b is null
348+         System .debug (' Denominator is null' 
349+         return  null ;  
350+         }                          //  Return null if division is not possibl
264351        //  Write the code for type casting the divisionResult to an Integer
352+         divisionResult  =  a  /  b ;
265353        Integer  roundedResult  =  null ;
266-         return  null ; //  Replace null with the variable you used to store the result
354+         roundedResult  =  (Integer ) divisionResult ;
355+         return  roundedResult ; //  Replace null with the variable you used to store the result
267356    }
268357
269358    /**  
@@ -277,7 +366,12 @@ public with sharing class VariablesDataTypesOperators {
277366     */  
278367    public  static  Decimal  calculateWeeklyPaycheck (Decimal  hourlyRate , Double  numberOfHours ) {
279368        //  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
369+         if  (hourlyRate  ==  null  ||  numberOfHours  ==  null ) {       //  Good practise to check if the numbers are null
370+             System .debug (' Numbers are null.' 
371+             return  null ;                                        //  End the code to avoid errors and return null
372+          }                                            
373+         Decimal  weeklyPaycheck  =  hourlyRate   *  numberOfHours ;   //  Otherwise Calculate the weekly paycheck using the formula: rate multiplied by hours
374+         return  weeklyPaycheck ;                                 //  Replace null with the variable you used to store the result
281375    }
282376
283377    /**  
@@ -291,20 +385,35 @@ public with sharing class VariablesDataTypesOperators {
291385     * @return  A Decimal representing the monthly paycheck. 
292386     */  
293387    public  static  Decimal  calculateMonthlyPaycheck (Decimal  hourlyRate , Double  numberOfHours ) {
294-         return  null ; //  Replace null with the variable you used to store the result
388+         if  (hourlyRate  ==  null  ||  numberOfHours  ==  null ) {           //  Good practise to check if the numbers are null
389+             System .debug (' Numbers are null.' 
390+             return  null ;                                          //  End the code to avoid errors and return null
391+          }                                           
392+         Decimal  weeklyPaycheck  =  hourlyRate   *  numberOfHours ;    // First  Calculate the weekly paycheck using the formula: rate multiplied by hours 
393+         Decimal  monthlyPaycheck  =  weeklyPaycheck  *  4 ;           //  Now multiply the weekly paycheck by 4 to get the monthly paycheck
394+         return  monthlyPaycheck ;      //  Replace null with the variable you used to store the result
295395    }
296396
297397    /**  
298398     * Question 20 
299399     * Calculates the total cost based on price per unit, number of units, and a sales tax rate. 
300-      * Total cost is calculated by the formula:  (pricePerUnit * numberOfUnits) * (1 + salesTaxRate). 
400+      * Total cost is calculat (pricePerUnit * numberOfUnits) * (1 + salesTaxRate).ed by the formula:   
301401     * Example: calculateTotalCost(1.0, 2, 0.05) should return 2.1 
302402     * @param  pricePerUnit  The price per unit of the item. 
303403     * @param  numberOfUnits  The number of units purchased. 
304404     * @param  salesTaxRate  The sales tax rate as a decimal (e.g., 0.05 for 5% tax). 
305405     * @return  The total cost after applying the sales tax. 
306406     */  
307407    public  static  Decimal  calculateTotalCost (Decimal  pricePerUnit , Integer  numberOfUnits , Decimal  salesTaxRate ) {
308-         return  null ; //  Replace null with the variable you used to store the result
408+         
409+         if  (pricePerUnit  ==  null  ||  numberOfUnits  ==  null  ||  salesTaxRate  ==  null ) {    //  Good practise to check if the numbers are null
410+             System .debug (' Numbers are null.' 
411+             return  null ;                                                                //  End the code to avoid errors and return null
412+             }
413+         Decimal  totalCost  =  (pricePerUnit  *  numberOfUnits ) *  (1  +  salesTaxRate );  
414+              //  Calculate the total cost using the formula
415+         return  totalCost ; //  Replace null with the variable you used to store the result
309416    }
310417}
418+ 
419+ 
0 commit comments