Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions force-app/main/default/classes/VariablesDataTypesOperators.cls
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
* @return The sum of the two numbers, or null if either number is null.
*/
public static Integer addition(Integer a, Integer b) {
return null; // Replace null with the variable you used to store the result
if(a == null || b == null){
return null;
}

Check warning

Code scanning / regex

Found trailing whitespace at the end of a line of code. Warning

Found trailing whitespace at the end of a line of code.
Integer sum = a + b;
return sum; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -44,7 +49,8 @@
* @return The difference between the two numbers.
*/
public static Integer subtraction(Integer a, Integer b) {
return null; // Replace null with the variable you used to store the result
Integer difference = a -b;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work with the null check here! 👍

For consistency, you might also want to add similar null checking to other methods that take Integer parameters, like subtraction() and multiplication(). While the tests might not require it, it's a good defensive programming practice.

return difference; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -56,7 +62,8 @@
* @return The product of the two numbers.
*/
public static Integer multiplication(Integer a, Integer b) {
return null; // Replace null with the variable you used to store the result
Integer product = a * b;
return product; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -69,7 +76,12 @@
* @return The quotient of the division, or 0 if the denominator is zero.
*/
public static Double division(Double a, Double b) {
return null; // Replace null with the variable you used to store the result
if(b == 0){
return 0;
}

Double quotient = a / b;
return quotient; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -83,7 +95,8 @@
* @return True if the number is even, False otherwise.
*/
public static Boolean isEven(Integer num) {
return null; // Replace null with the variable you used to store the result
Boolean isEven = Math.mod(num, 2) == 0;
return isEven; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -94,7 +107,8 @@
* @return true if the number is positive, false otherwise.
*/
public static Boolean isPositive(Integer num) {
return null; // Replace null with the variable you used to store the result
Boolean isPositive = num > 0;
return isPositive; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -106,7 +120,8 @@
* @return The concatenated string.
*/
public static String concatenateStrings(String str1, String str2) {
return null; // Replace null with the variable you used to store the result
String jointString = str1 + str2;
return jointString; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -121,7 +136,8 @@
* @return The complete sentence as a single String.
*/
public static String createSentence(String noun, String verb, String endingPunctuation) {
return null; // Replace null with the variable you used to store the result
String completeSentence = 'The ' + noun + ' is ' + verb + endingPunctuation;
return completeSentence; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -134,7 +150,8 @@
* @return True if the date is in the past, False otherwise.
*/
public static Boolean isDateInPast(Date dt) {
return null; // Replace null with the variable you used to store the result
Boolean dateInThePast = dt < System.Today();
return dateInThePast; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -147,7 +164,8 @@
* @return True if the date is today or in the future, False otherwise.
*/
public static Boolean isDateTodayOrFuture(Date dt) {
return null; // Replace null with the variable you used to store the result
Boolean dateIsTodayOrLater = dt >= System.Today();
return dateIsTodayOrLater; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -159,10 +177,11 @@
* @param minutes The number of minutes.
* @return The number of milliseconds equivalent to the given number of minutes.
*/
Integer MILLISECONDS_PER_MINUTE = null; // Make this value a constant
Static Integer MILLISECONDS_PER_MINUTE = 60000; // Make this value a constant

Check warning

Code scanning / PMD

Field declaration for 'MILLISECONDS_PER_MINUTE' should be before method declarations in its class Warning

Field declaration for 'MILLISECONDS_PER_MINUTE' should be before method declarations in its class

Check warning

Code scanning / PMD

The static field name 'MILLISECONDS_PER_MINUTE' doesn't match '[a-z][a-zA-Z0-9]*' Warning

The static field name 'MILLISECONDS_PER_MINUTE' doesn't match '[a-z][a-zA-Z0-9]*'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo here: Java/Apex keywords are case-sensitive, so this should be lowercase static instead of Static.

static final Integer MILLISECONDS_PER_MINUTE = 60000;

The code still works because Apex is somewhat forgiving, but following proper casing conventions is important for maintainability!


Check warning

Code scanning / regex

Found trailing whitespace at the end of a line of code. Warning

Found trailing whitespace at the end of a line of code.
public static Integer convertMinutesToMilliseconds(Integer minutes) {
Integer milliseconds;
return null; // Replace null with the variable you used to store the result
Integer millisecond = minutes * MILLISECONDS_PER_MINUTE;
return millisecond; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -176,7 +195,8 @@
* @return The average of the three numbers.
*/
public static Double averageOfThreeNumbers(Integer a, Integer b, Integer c) {
return null; // Replace null with the variable you used to store the result
Double mean = (a + b + c) / 3;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this method! One thing to watch out for: when you divide integers, the result is truncated (rounded down) before being converted to a Double.

For a more accurate average, you could use:

Double mean = (a + b + c) / 3.0;

By using 3.0 instead of 3, you're doing floating-point division which gives you the precise decimal result. For example:

  • Your current code: (1 + 1 + 1) / 3 = 1.0 (3/3 = 1, then converted to 1.0)
  • With 3.0: (1 + 1 + 1) / 3.0 = 1.0 (correct in this case)
  • But with (1 + 1 + 2): current = 1.0, with 3.0 = 1.333...

It's a subtle but important distinction when working with averages!

return mean; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -191,10 +211,10 @@
*/
public static Integer adjustOrderOfOperations1(Integer a, Integer b, Integer c) {
// Add parentheses around the addition operation so that it is performed before multiplication
Integer result = a + b * c;
Integer result = (a + b) * c;

// Return the result
return null; // Replace null with the variable you used to store the result
return result; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -206,8 +226,8 @@
// Add parentheses in the below expression to change the result.
// The result of the expression as it is right now is 43.
// You should add parentheses so that the result of the expression becomes 8.
Integer answer = 48 - 15 + 5 * 2;
return null; // Replace null with the variable you used to store the result
Integer answer = 48 - (15 + 5) * 2;
return answer; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -226,9 +246,9 @@
*/
public static Double complexOrderOfOperations(Integer a, Integer b, Integer c, Integer d, Integer e) {
// Add parentheses around the multiplication and subtraction operations so that they are performed before division and addition
Double result = a * b - c / (Double) d + e;
Double result = (a * b - c) / (Double) d + e;

return null; // Replace null with the variable you used to store the result
return result; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -242,8 +262,10 @@
final Double SUBTRACT_FACTOR = 32.0;
final Double MULTIPLY_FACTOR = 5.0;
final Double DIVIDE_FACTOR = 9.0;

Double celciusTemp = (fahrenheit - SUBTRACT_FACTOR) * (MULTIPLY_FACTOR / DIVIDE_FACTOR);

return null; // Replace null with the variable you used to store the result
return celciusTemp; // Replace null with the variable you used to store the result
}


Expand All @@ -259,11 +281,11 @@
*/
public static Integer performDivisionAndCast(Double a, Double b) {
// Perform the division and cast (round down) off the result.
Double divisionResult = null;
Double divisionResult = a / b;

// Write the code for type casting the divisionResult to an Integer
Integer roundedResult = null;
return null; // Replace null with the variable you used to store the result
Integer roundedResult = (Integer) divisionResult;
return roundedResult; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -277,7 +299,8 @@
*/
public static Decimal calculateWeeklyPaycheck(Decimal hourlyRate, Double numberOfHours) {
// Calculate the weekly paycheck using the formula: rate multiplied by hours
return null; // Replace null with the variable you used to store the result
Decimal weeklyPaycheck = hourlyRate * numberOfHours;
return weeklyPaycheck; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -291,7 +314,8 @@
* @return A Decimal representing the monthly paycheck.
*/
public static Decimal calculateMonthlyPaycheck(Decimal hourlyRate, Double numberOfHours) {
return null; // Replace null with the variable you used to store the result
Decimal monthlyPaycheck = hourlyRate * numberOfHours * 4;
return monthlyPaycheck; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -305,6 +329,7 @@
* @return The total cost after applying the sales tax.
*/
public static Decimal calculateTotalCost(Decimal pricePerUnit, Integer numberOfUnits, Decimal salesTaxRate) {
return null; // Replace null with the variable you used to store the result
Decimal totalCostWithTax = (pricePerUnit * numberOfUnits) * (1 + salesTaxRate);
return totalCostWithTax; // Replace null with the variable you used to store the result
}
}
Loading