0% found this document useful (0 votes)
3 views4 pages

Class Examples

The document contains three examples of Java classes demonstrating object-oriented programming concepts. The first example illustrates a Rectangle class for calculating the area, the second example showcases an Employee class for managing employee records, and the third example presents an Account class for banking operations like deposit and withdrawal. Each example includes a main method to test the functionality of the respective classes.

Uploaded by

Eshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Class Examples

The document contains three examples of Java classes demonstrating object-oriented programming concepts. The first example illustrates a Rectangle class for calculating the area, the second example showcases an Employee class for managing employee records, and the third example presents an Account class for banking operations like deposit and withdrawal. Each example includes a main method to test the functionality of the respective classes.

Uploaded by

Eshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXAMPLE 1

FIND AREA OF RECTANGLE USING CLASSES AND


OBJECTS THROUGH CONSTRUCTOR

class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){
System.out.println(length*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
} }
EXAMPLE 2 (EMPLOYEE CLASS)

Let's see an example where we are maintaining


records of employees.

class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){System.out.println(id+" "+name+"
"+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}
EXAMPLE- 3
//Java Program to demonstrate the working of a banki
ng-system
//where we deposit and withdraw amount from our ac
count.
//Creating an Account class which has deposit() and
withdraw() methods
class Account{
int acc_no;
String name;
float amount;
//Method to initialize object
void insert(int a,String n,float amt){
acc_no=a;
name=n;
amount=amt;
}
//deposit method
void deposit(float amt){
amount=amount+amt;
System.out.println(amt+" deposited");
}
//withdraw method
void withdraw(float amt){
if(amount<amt){
System.out.println("Insufficient Balance");
}else{
amount=amount-amt;
System.out.println(amt+" withdrawn");
}
}
//method to check the balance of the account
void checkBalance(){System.out.println("Balance is:
"+amount);}
//method to display the values of an object
void display(){System.out.println(acc_no+"
"+name+" "+amount);}
}
//Creating a test class to deposit and withdraw amou
nt
class TestAccount{
public static void main(String[] args){
Account a1=new Account();
a1.insert(832345,"Ankit",1000);
a1.display();
a1.checkBalance();
a1.deposit(40000);
a1.checkBalance();
a1.withdraw(15000);
a1.checkBalance();
}}

You might also like