Apex Codes-Day 1 & 2
Apex Codes-Day 1 & 2
{//delimiters
integer res=N1+N2;
System.debug(res);
}*/
}
===================================================================================
====================
public class StaticExample
{
decimal Area=pi*radius*radius;
System.debug('Area of a Circle:' + Area);
}
}
===================================================================================
====================
public class StudentDetails
{
integer Age;
string Name;
string Address;
public StudentDetails()//constructor
{
Age=24;
Name='Ayush';
Address='New Delhi';
DisplayDetails();
}
}
===================================================================================
====================
public class PropertyClass
{
static integer a;//private variable
else if(units<=200)
{
BillAmount=Units*3;
System.debug('Bill Amount: '+ BillAmount);
}
else if(Units<=300)
{
BillAmount=Units*4;
System.debug('Bill Amount: '+ BillAmount);
}
else
{
BillAmount=Units*5;
System.debug('Bill Amount: '+ BillAmount);
}
}
}
===================================================================================
====================
public class NestedIfElse
{
public static void CheckEligibility(integer Age,integer Marks,string Qual)
{
if(Age>=18)
{
if(Marks>=75)
{
if(Qual=='B.Tech' || Qual=='BCA')
{
system.debug('Congratulations! Candidate is selected');
}
else
{
system.debug('Sorry! Candidate is rejected as not having the desired
qualification');
}
}
else
{
system.debug('Sorry! Candidate is rejected as marks are less than 75');
}
}
else
{
system.debug('Sorry! Candidate is rejected as Age is less than 18');
}
}
do
{
system.debug(i);//0,1,2,3,4
i++;//unary operator
//i=i+1;//arithmetic operator
//i+=1;//arithmetic assignment operator
}
while(i<5);//condition
}
/* Score[0]=89;//initialization
Score[1]=67;
Score[2]=78;
Score[3]=75;
Score[4]=81;
Score[5]=58;
Score[6]=89;
Score[7]=59;
Score[8]=57;
Score[9]=85;*/
/* System.debug(Score[0]);
System.debug(Score[1]);
System.debug(Score[2]);*/
string [] names=new
string[]{'Ritik','Heera','Aman','Aryan','Dhruv'};//declaration + initialization
for(integer i=0;i<names.size();i++)
{
system.debug(names[i]);
}
system.debug(names);
===================================================================================
===================
public class ForeachExample
{
public static void foreachLoop()
{
contact [] cons= [select firstname from contact];
for(Contact c:cons)
{
system.debug(c);
}
}
}
===================================================================================
====================
public class ListCollection
{
public static void ListExample()
{
// List<string> colors=new List<string>();//declaration
List<string> colors=new List<string>{'Orange','Blue'};//declaration+
initialization
colors.add('Red');//initialization
colors.add('Yellow');//add will add a new value at the end of the list
colors.add('Green');
System.debug(colors);
System.debug(colors);
system.debug(colors);
}
}
===================================================================================
=====================
public class SetCollection
{
public static void SetExample()
{
// Set<string> Addresses=new Set<string>();//declaration
Set<string> Addresses=new Set<string>{'Pune','Chennai'};//declaration +
initialization
Addresses.add('New Delhi');//initialization
Addresses.add('Mumbai');
Addresses.add('Banglore');
System.debug(addresses);
System.debug(addresses.size());
if(Addresses.contains('Delhi'))
{
system.debug('The searched address is available');
}
else
{
system.debug('The searched address is not available');
}
Addresses.addAll(Names);
System.debug(Addresses);
}
}
===================================================================================
====================
public class MapCollection
{
public static void MapExample()
{
Map<integer,string> EmpAddress=new Map<integer ,string>();//declaration
EmpAddress.put(1,'New Delhi');//initialization
EmpAddress.put(2,'Mumbai');//will add the values in a map
EmpAddress.put(3,'Chennai');
EmpAddress.put(4,'Hyderabad');
EmpAddress.put(3,'Pune');
Map<string,string> newMap=new
Map<string,string>{'a'=>'apple','b'=>'ball'};//declaration+ initialization
newMap.put('c','cat');
System.debug(EmpAddress);
System.debug(EmpAddress.get(2));//to get the value from a index
for(string s:Address)
{
system.debug(s);
}
System.debug(NewMap);
}
}
===================================================================================
====================
Anonymous Window Code
===================================================================================
====================
firstClass ob1=new firstClass();//memory allocation
ob1.FirstMethod();//function calling
StaticExample.Add();
StaticExample ob=new StaticExample();
ob.Add();
ConstantExample.CalculateArea(5);
===================================================================================
====================