Daily Class Code Snippets
Key Object Oriented Programming Terminologies:
● Class - Class is a collection of variables and methods.
● Variable - Variable stores values based on which calculations are
done.
● Method - Methods are a collection of instructions to solve a business
problem.
● Static - It is a keyword used to create static variables and methods
static.
● Void - It is a primitive data type and generally used as a return type
of method.
● Passing Parameters - we can pass any number and type of
parameters into methods if needed
● Returning Values - We can return any type of value from the method
if needed.
● Constructor - Used to initialize the class variables. There can be
default and parameterized constructors.
● Method Call - To execute a method we need to call it.
● Static Vs Non-Static Method - Non-Static methods are called
through instances of the class whereas Static methods are called
through class name.
Day - 1 Class with Variables and Methods
www.skillhorizon.org.in
Page 1
//Rectangle Class
public class Rectangle {
//variable
Integer length = 10, breadth = 20; //Integer is a predefined data type
//Data types defined type of variables
//length, breadth are variables
//methods - Process
public void area(){
Integer a;
a = length * breadth;
System.debug('Area of Rectangle= ' + a); //display the result in the logs
}
public void perimeter(){
Integer p;
p = 2 * (length + breadth);
System.debug('Perimeter of Rectangle = ' + p);
}
}
Day - 2 Class with Variables and Methods
//Simple Interest Class
public class SimpleInterest {
www.skillhorizon.org.in
Page 2
//Variables
Decimal prin = 20000, rate = 7, tm =3;
//Methods
public void calculate(){
Decimal si;
si = (prin * rate * tm)/100;
System.debug('Simple Interest = ' + si);
}
}
//Circle
public class Circle {
Decimal radius = 5, pi = 3.141;
public void area(){
Decimal a;
a = pi * radius * radius;
System.debug('Area = ' + a);
}
public void circumference(){
Decimal c;
c = 2 * pi * radius;
System.debug('Circumference = ' + c);
}
}
//Square
public class Square {
Decimal side = 10;
www.skillhorizon.org.in
Page 3
public void area(){
Decimal a;
a = side * side;
System.debug('Area = ' + a);
}
public void perimeter(){
Decimal p = 4 * side;
System.debug('Perimeter = ' + p);
}
}
//Anonymous Window code
SimpleInterest si = new SimpleInterest();
si.calculate(); //method call
Circle cr = new Circle();
cr.area();
cr.circumference();
Square s = new Square();
s.area();
s.perimeter();
//Calculator
public class Calculator {
Integer num1 = 10, num2 = 5;
public void addition(){
www.skillhorizon.org.in
Page 4
Integer sum;
sum = num1 + num2;
System.debug('Sum = ' + sum);
}
public void subtraction(){
Integer sub;
sub = num1 - num2;
System.debug('Subtraction = ' + sub);
}
public void multiplication(){
Integer mul;
mul = num1 * num2;
System.debug('Mul = ' + mul);
}
public void division(){
Integer div;
div = num1 / num2;
System.debug('div = ' + div);
}
}
//Anonymous window
Calculator c = new Calculator();
c.addition();
c.subtraction();
c.multiplication();
c.division();
Day - 3 Class with Constructor, Variable and Methods
//Implement Rectangle Class to Calculate Area and Perimeter
public class Rectangle {
//Variable
www.skillhorizon.org.in
Page 5
//Decimal is working as Datatype and length/breadth are variable names
Decimal length, breadth;
//default constructor
public Rectangle(){
length = 10.5;
breadth = 20.6;
}
//parameterized constructor
public Rectangle(Decimal l, Decimal b){
length = l;
breadth = b;
}
//Methods
//void - return type
public void area(){
Decimal a;
a = length * breadth;
System.debug('Area = ' + a);
}
public void perimeter(){
Decimal p = 2 * (length + breadth);
System.debug('Perimeter = ' + p);
}
}
//Simple Interest
public class SimpleInterest {
//Variables
Decimal prin, rate, tm;
//default constructor
www.skillhorizon.org.in
Page 6
public SimpleInterest(){
prin = 100000;
rate = 10;
tm = 3;
}
//Parameterized constructor
public SimpleInterest(Decimal prin, Decimal rate, Decimal tm){
this.prin = prin;
this.rate = rate;
this.tm = tm;
}
//Method
public Decimal calculate(){
Decimal result;
result = (prin * rate * tm)/100;
System.debug('Simple Interest = ' + result);
return result;
}
}
Anonymous Window
//default constructor testing
SimpleInterest si = new SimpleInterest();
si.calculate();
//parameterized constructor testing
SimpleInterest si = new SimpleInterest(10000,15,5);
Decimal res = si.calculate();
Circle c1 = new Circle(res);
c1.area();
//Circle Class
public class Circle {
www.skillhorizon.org.in
Page 7
//Variables
Decimal radius, pi = 3.141;
//default constructor
public Circle(){
radius = 5;
}
//parameterized constructor
public Circle(Decimal r){
radius = r;
}
public void area(){
Decimal a;
a = pi * radius * radius;
System.debug('Area = ' + a);
}
public void circumference(){
Decimal c;
c = 2 * pi * radius;
System.debug('Circumference = ' + c);
}
}
www.skillhorizon.org.in
Page 8