You are on page 1of 7

1.

create a class student with datamembers


1.name
1.age
2.branch;
public class Student{
public String name;
public Integer age;
public String branch;
}
2.Create a class Employee with data memebers
1.empName
2.salary
3.exp
public class Employee{
public String empName;
public Decimal Salary;
public Decimal exp;
}
3. Create a class Movie with data Memebers
1.movieName
2.heroName
3.heroineName
public class Movie{
public String movieName;
public String heroName;
public String heroineName;
}
4.create a class Book with data Members
1.bookName
2.cost
3.available
public class Book{
public String bookName;
public Decimal cost;
public Integer available;
}
5.create a class college with data memebers
1.collegeName
2.city
3.branch
4.phone
public class College{

public
public
public
public

String
String
String
String

collegeName;
city;
branch;
phone;

}
6.Create a class ProductData with data memebers
1.productName;
2.productCode
3.cost
4.quantity
public ProductData{
public
public
public
public

String productName;
String productCode;
Decimal cost;
Integer quantity;

}
7.create a class student with the following data members and methods
1. name
2. age
3. show() : void
This should display name and age value
public class Student{
public String name;
public Integer age;
public void show(){
System.debug('My Name :'+name);
System.debug('My Age :'+age);
}
}
-------------------------------------------------------------------------8 .Create a class Bank with follwing data members and methods
1.balance
2.deposit(amt) : void
This method wwill add the amt to the balance
3.withdraw( amt) : void
This method will reduce the amt from balance
4.display(): void
This method should display the balance
public class Bank{
public Decimal balance;
public void deposit(Decimal amt){
balance=balance+amt;
}

public void withdraw(Decimal amt){


balance=balance-amt;
}
public void display(){
System.debug('Balance:'+balance);
}
}
-----------------------------------------------------------------------9. Create class Movie with following data memebers and methods
1.available
2.cost
3.checkAvailable(noTickets) : This method will take the no of who want
as parameter and check the availabilty
and return true if the tickets are
available.
4.bookTickets(noTickets) :This method will take noTickets you want as
inputparameter and check the availabilty .
if the tickets are available calculate the
total amount and reduce the available
tickets.Return the totalCost.
public class Movie{
public Integer available ;
public Decimal cost;
public Boolean checkAvailable(Integer noTickets){
if(noTickets <= available){
return true;
}else{
return false;
}
}
public Decimal bookTickets(Integer noTickets){
Decimal totalcost=0;
if(noTickets <=available){
totalcost=noTickets*cost;
}
return totalCost;
}
}
=============================================================================
Constructor :
===========================================================================
1,Constructor is a method that has class name as method name
2.No return type of the constructor
3. This is used to intialize the data memebers of the class
4. Constructor will be invoked only once that is at the time
of creating the object.
Syntax: public class ClassName{
public ClassName(){
}

}
10.Create a class student with constructor
public class Student{
public Student(){
}
}
11. Create a class Employee with constructor
public class Employee{
public Employee(){
}
}
12.Create a class Student with a constructor where name is parameter
in the constructor
public class Student{
public Student(String name){
}
}
13.Create a class Employee with follwing data members and methods
1.
2.
3.
4.

name
exp
salary
Constructor() : Assign value to the data members in the
constructor

public class Employee{


public String name;
public Decimal exp;
public Decimal salary;
public Employee(){
name='Satish';
exp=20;
salary=10000;
}
}
-------------------------------------------14. Create a class ProductData with datamemebers and methods
1.prodName
2.prodCode
3.cost

4.ProductData() : assing the value to the data members in the


constructor
5.display() : Print all the data memebers of the class using
system.debug
public class ProductData{
public String prodName;
public String prodCode;
public Decimal cost;
public ProductData(){
prodName='Dell';
prodCode='P01';
cost=10000;
}
public void display(){
System.debug(' Name :'+prodName);
System.debug(' Code :'+prodCode);
System.debug(' Cost :'+cost);
}
}
---------------------------------------------------------------------------15. Create class Book
1. bookName
2. authorName
3.price
4.Book(bname,aname) : pass the bookname and author name as parameters
and assign the values to the data members
public class Book{
public String bookName;
public String authorName;
public Decimal cost;
public Book(String bname,String aname){
bookName=bname;
authorName=aname;
}
}
============================================================================
Object
==========================================================================
1. new : New is the keyword which allocate the memory for the data members
public class Student{
public Integer age ;
public String name;
public Student(){
name='Satish';
age=20;
}
when we call new it will allocate memory

------------| name=null |
|
|
| age=null |
-------------Again i called new again it will allocate the memory
------------| name=null |
|
|
| age=null |
-------------Again if we call new it will allocate the memory again
------------| name=null |
|
|
| age=null |
-------------2. Once new keyword has assigned the memory then constructor is called
and it will intilize the data

--------------| name=satish |
|
|
| age=20
|
---------------3. Create a reference variable
This will store the refernce to the memeory location allocated by new
keyword
Primitive variables : Primitive variable contains data
Integer age ;
String name;
Decimal salary;
Boolean active;
Long a;
Blob body;
Refernce variables contain reference to memory locatiomn
Employee e;
Account a ;
Student std;
Object : object is a refernce variable or new Instance of the class
ClassName obj=new ClassName();
Student s=new Student();

You might also like