Skip to content

Instantly share code, notes, and snippets.

@AlwaysThinkin
Last active July 26, 2016 03:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlwaysThinkin/193ba2f1ed89be3b4cff5e889b81d23a to your computer and use it in GitHub Desktop.
Save AlwaysThinkin/193ba2f1ed89be3b4cff5e889b81d23a to your computer and use it in GitHub Desktop.
@isTest
public class DataTypeTestAnswers {
public static TestMethod void DataTypes(){
string text1 = 'this text';
string text2 = 'that text';
string text3 = text1 + text2;
System.debug(text3);
text3 = text3 + 'more text';
System.debug(text3);
string text4 = text3.toUpperCase();
System.debug('SHOUT! ' + text4);
/* For practicing dot notation and method usage
*/
string text5 = text4.toLowerCase();//FIX ME!!! Turn text4 into a lower case string.
System.debug('whisper ' + text5);
/* For practicing dot notation and method research.
* Other possible solutions (e.g. replace(' ', ''))
*/
string text6 = text5.deleteWhitespace();//FIX ME!!! Find a string method to remove whitespaces.
System.debug('No more whitespaces: ' + text6);
/* For practicing debug statements
* Demos methods with arguments and escape character
*/
string text7 = text6.remove('text');
System.debug('This is what\'s left: ' + text7);//FIX ME!!! Edit the debug output to include text7.
/* Demos mutliple arguments
*/
string text8 = text7.replace('more', 'less');
System.debug('now with less! ' + text8);
/* Demos integer argument
*/
string text9 = text8.repeat(2);
System.debug('repeated twice: ' + text9);
/* Demos return values of different data types
* Introduce Integer DataType
*/
Integer number1 = text9.length();
System.debug('how long? ' + number1);
/* Introduces concept of Index
*/
Integer number2 = text9.indexOf('this');
System.debug('find this: ' + number2);
/* For practicing use of Index
*/
Integer number3 = text9.indexOf('that');//FIX ME!!! Use indexOf() to find 'that' in text9.
System.debug('find that: ' + number3);
/* Introduces System.assertEquals
* Demonstrates determining index values to use as Expected value in assertEquals
*/
Integer number4 = text9.LastIndexOf('that');
System.assertEquals(16, number4, 'Expected value should equal actual value returned by lastIndexOf()');
/* Practice determining index value
*/
Integer number5 = text9.IndexOf('less');//FIX ME!!! Can you guess what the index of 'less' will be?
System.assertEquals(null,null);
//define some numbers we'll use to work with Integers and operators
Integer number6 = 6;
Integer number7 = 7;
/* Introduces math operators + and ++
* Explores how ++ before or after an Integer behaves differently
*/
Integer sum1 = number6 + number7;
System.Debug('do the math: ' + sum1);
System.Debug('x++ returns value then increments: ' + sum1++);
System.Debug('what is the value now?: ' + sum1);
System.Debug('++x increments then returns value: ' + ++sum1);
System.assertEquals(15, sum1);
/* Practice using math operators + and ++
* Practice using assertEquals
*/
Integer sum2 = number7 + number6;//FIX ME!!! add number7 to number6
Integer sum3 = ++sum2;//FIX ME!!! set sum3 to increment sum2.
System.assertEquals(14, sum3);//FIX ME!!! Uncomment and add your expected result.
/* Explore difference between Integers and Decimals in Apex
* by demonstrating that Apex does not reinterpret data types:
* dividing two integers produces another integers
* and drops the decimal remainder regardless of whether the result
* is assigned to a decimal or integer.
*/
//What happens if we assign a non-integer value?
Integer decimal0 = number6 / number7;
System.debug('Integer of 2 integers divided: ' + decimal0);
//Even though the result is a decimal, 2 Integers divided are still treated as integers.
Decimal decimal1 = number6 / number7;
System.debug('Decimal of 2 integers divided: ' + decimal1);
/* Introduces casting as a quick means of converting datatypes
*/
//"Casting" is a useful technique to convert one data type to another.
Decimal decimal2 = (decimal)number6 / (decimal)number7;
System.debug('2 integers "cast" to decimals: ' + decimal2);
/* Introduces Double and Long numeric types
* Introduces definining decimals or longs without casting
*/
//Care must be taken when working with different numeric types:
//Explicitly declare Double or Decimal values by adding .0 if no fractional value is present
Double nope = 5/3;
System.debug('Double of divided integers: 5/3 != ' + nope);
Double better = 5.0/3.0;
System.debug('Double of divided integers with .0 added: 5.0/3.0 == ' + better);
//Explicitly declare Long values by adding L if the number is likely to exceed 2147483647
Long wrong = 2147483647 + 1;
System.debug('Maximum integer value of 2147483647 plus 1: ' + wrong);
Long right = 2147483647L + 1;
System.debug('Maximum integer value as a Long plus 1: ' + right);
/* Introduces Date, Datetime and Time datatypes
*/
//Dates and times are both tricky and easy to work with.
Date date1 = Date.today();
Datetime datetime1 = Datetime.now();
Time time1 = Time.newInstance(18, 30, 2, 20);
/* Demonstrates chained dot notation
*/
Time time2 = Datetime.now().time();
/* Demonstrates mathematical operations with dates
*/
Date date2 = date1 + 1;
System.debug('Tomorrow: ' + date2);
/* Practice date calculation
* Practice creating the entire data type declaration, variable name & value
*/
Date date3 = date2 - 2;
System.debug('Yesterday: ' + date3);//FIX ME!!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment