Apex Programming Coding Examples -
How to Write Apex Class in Salesforce / How to Write Apex Program in Salesforce
1. Write an apex program, if a=6, b=4 then the Addition of a & b is?
Ans -
public class Addition {
public void add() {
integer a = 6;
integer b = 4;
integer result;
result = a + b;
System.debug("Addition of a & b is :" +result);
}
}
open an anonymous window and write -
Addition obj = new Addition();
obj.add();
2. Write an apex program, to check whether the number is even or add.
Ans -
public class Number {
public void check() {
Integer n = 3;
if( math.mod( 1,2 ) == 0 )
{
System.debug('Number is Even');
}else
{
System.debug('Number is Odd');
}
}
}
open an anonymous window and write -
Number obj = new Number();
obj.check();
3. Write an apex program, to find the Biggest Number.
Ans -
public class Biggest {
public void big() {
integer a = 2;
integer b = -66;
integer c = 46;
if( a > b )
{
if( a > c )
{
System.debug( a + ' a is the Biggest Number' );
} else
{
System.debug( c+ ' c is the Biggest Number' );
} else
{
if( b > c )
{
System.debug( b+ 'b is the Biggest Number' );
} else
{
System.debug(c+ 'c is the Biggest Number' );
}
}
}
}
}
open an anonymous window and write -
Biggest obj = new Biggest();
obj.big();
4. Write an apex program, to calculate the simple interest.
Ans -
public class Interest {
public void find() {
integer principleAmount = 20000;
integer tenure = 6;
integer rateOfInterest = 4;
integer interestAmount;
interestAmount = ( principleAmount * tenure * rateOfInterest ) / 100;
System.debug(' Interest Amount is :' +interestAmount );
}
}
open an anonymous window and write -
Interest obj = new Interest();
obj.find();
5. Write an apex program, to print the season name based on the month number as below -
Month number Season Name
1-4 Winter
5-8 Summar
9-12 Spring
<1 or > 12 Invalid Month
Ans -
public class Season {
public void check() {
integer monthNumber = 15;
Switch ON monthNumber
{
When 1, 2, 3, 4
{
System.debug(' Winter Season' );
}
When 5, 6, 7, 8
{
System.debug(' Summar Season' );
}
When 9, 10, 11, 12
{
System.debug(' Spring Season' );
}
When else {
System.debug(' Invalid Month Number' );
}
}
}
}
open an anonymous window and write -
Season obj = new Season();
obj.check();
6. Write an apex program, to print a welcome message 12 times.
Ans -
public class WelcomeMessage {
public void message() {
integer counter=1;
while( counter <= 12 )
{
System.debug( ' Welcome To Pune ' );
counter++;
}
}
}
open an anonymous window and write -
WelcomeMessage obj = new WelcomeMessage();
obj.message();
7. Write an apex program, to print odd numbers between 1 to 100.
Ans -
public class OddNumber {
public void number() {
integer counter = 1;
do
{
System.debug( ' Odd Number is : +counter );
counter +=2;
}while( counter <= 100 );
}
}
open an anonymous window and write -
OddNumber obj = new OddNumber();
obj.number();
8. Write an apex program, to print even numbers between 1 to 50 in reverse order.
Ans -
public class EvenNumber {
public void number() {
integer counter = 50;
do
{
System.debug(' Even Number is : ' +counter );
counter -=2;
}while( counter >= 1 );
}
}
open an anonymous window and write -
EvenNumber obj = new EvenNumber();
obj.number();
9. Write an apex program, to print an even number between 1 to 200.
Ans -
public class EvenNumber {
public void number() {
for( integer counter = 2; counter <= 100; counter +=2
)
{
System.debug(' Even number is : ' +counter );
}
}
}
open an anonymous window and write -
EvenNumber obj = new EvenNumber();
obj.number();