PGD202 Object Oriented Programming

Object Oriented Programming (JAVA)

Hello World in Java.

package kamal_programs;

public class Hello_World {

public static void main(String[] args)
{

System.out.printf(“\nHello World – 1”);

System.out.println(“\nHello World – 2”);

}

}

 

Maximum & Minimum:

package kamal_programs;

public class Minnium {

int n,i,j=0,c,s;
int [] arr =new int[10];
double [] arr1 =new double[10];
double s1;

public void bubbleShort()
{

arr[0]=5;
arr[1]=15;
arr[2]=10;
arr[3]=6;
arr[4]=8;
arr[5]=18;
arr[6]=25;
arr[7]=3;
arr[8]=78;
arr[9]=88;

n=10;

for(c=0;c<n-1;c++)
{
for(j=0;j<n-c-1;j++)
{
if(arr[j]<arr[j+1])
{
s=arr[j];
arr[j]=arr[j+1];
arr[j+1]=s;
}
}
}

System.out.println(“Sorted Array is:”);
for(int i=0;i<10;i++)
{
System.out.println(arr[i]);

}

System.out.println(“”);
}

public void bubbleShort_Float()
{

arr1[0]=5.5;
arr1[1]=15.2;
arr1[2]=10.3;
arr1[3]=6.2;
arr1[4]=8.8;
arr1[5]=18.2;
arr1[6]=25.1;
arr1[7]=3.3;
arr1[8]=78.5;
arr1[9]=88.9;

n=10;

for(c=0;c<n-1;c++)
{
for(j=0;j<n-c-1;j++)
{
if(arr1[j]<arr1[j+1])
{
s1=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=s1;
}
}
}

System.out.println(“Sorted Array is:”);
for(int i=0;i<10;i++)
{
System.out.println(arr1[i]);

}

System.out.println(“”);
}

public void display()
{n=10;
System.out.println(arr[n-1]);
System.out.println(arr[n-2]);
System.out.println(arr[n-3]);

}

public void display_Double()
{n=10;
System.out.println(arr1[n-1]);
System.out.println(arr1[n-2]);
System.out.println(arr1[n-3]);

}
}

 

 

 Inheritance:

package oopfinalpractice;

class Employee{
float salary=40000;

void print()
{
System.out.println(“”+this.salary);

}
}
class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println(“Programmer salary is:”+p.salary);
System.out.println(“Bonus of Programmer is:”+p.bonus);

Employee e =new Employee();
e.print();
}
}

 

Function Overloading:

package kamal_programs;

public class Cube_Overload {

double height, length, width, get_volume, result;

public Cube_Overload(int hwl)
{
this.height=hwl;
this.width=hwl;
this.length=hwl;

}

public Cube_Overload(int h, int w, int l)
{
this.height=h;
this.width=w;
this.length=l;

}

public void setVolume()
{

this.get_volume=this.height*this.width*this.length;
}

public void display()
{
System.out.println(“Volume is: “+this.get_volume);
}

}

 

package kamal_programs;

public class Cube_Name {

public static void main(String[] args) {

Cube c=new Cube(10, 5, 10);
double result=c.getVolume();
System.out.println(“Volume is: “+result);

Cube c1=new Cube(5, 5, 5);
System.out.println(“Volume is: “+c1.getVolume());

}

}

 

package kamal_programs;

public class Cube_Overload_Main {

public static void main(String[] args) {

Cube_Overload C1=new Cube_Overload(5);
C1.setVolume();
C1.display();

Cube_Overload C2=new Cube_Overload(10,5,5);
C2.setVolume();
C2.display();

}

}

 

 

Polymorphism

package classtest;

class Employee{

String Name;

Employee(String n){
Name=n;

}

public void setEmployee( String n){
Name=n;

}

String getEmployee(){

return this.Name;
}

public String toString(String nn){

nn=String.format(“%s”, getEmployee());

return nn;
}

}

class ComissionEmployee extends Employee {

//String My_Name;
double total_earning, gross_sale, commission_rate;

ComissionEmployee(String n){
super(n);

}

double earning(){

total_earning = gross_sale*commission_rate;

return total_earning;
}

public String toString(String ear){

ear=String.format(“%ld”, earning());

return ear;
}

public void set_gross_sale( double gs){

this.gross_sale=gs;

}

double get_gross_sale(){

return gross_sale;
}

public void set_comission_rate(double com){

this.commission_rate=com;

}

double get_comission_rate(){

return commission_rate;
}

public static void main(String[] args) {

ComissionEmployee CE = new ComissionEmployee(“Md. Abdullah Al Kamal”);
CE.set_comission_rate(1.5);
CE.set_gross_sale(10000);

System.out.println(“Name Is: “+CE.Name);
System.out.println(“Comission Rate: “+CE.get_comission_rate());
System.out.println(“Total Sales: “+CE.get_gross_sale());
System.out.println(“Total Earning is: “+CE.earning());

System.out.println(“\n”);

ComissionEmployee CE1 = new ComissionEmployee(“Sakib Al Hasan”);
CE1.set_comission_rate(2.5);
CE1.set_gross_sale(10000);

System.out.println(“Name Is: “+CE1.Name);
System.out.println(“Comission Rate: “+CE1.get_comission_rate());
System.out.println(“Total Sales: “+CE1.get_gross_sale());
System.out.println(“Total Earning is: “+CE1.earning());

}

}

Super, Upcasting, Etc:

package labexam02;

abstract class Employee {

String name;

Employee(String name)
{
setEmployee(name);
}

abstract double earning();

public void setEmployee( String name)
{this.name = name;}

String getEmployee()
{return this.name; }
public String toString()
{ System.out.println(“Employee Name\t\t:\t”+getEmployee());

return “”;
}

}

 

package labexam02;

class ComissionEmployee extends Employee {

double total_earning, gross_sale, commission_rate;

ComissionEmployee(String name, double gross_sale, double commission_rate){
super(name);
set_gross_sale(gross_sale);
set_comission_rate(commission_rate);

}

public double earning(){

total_earning = (this.gross_sale*this.commission_rate)/100;

return total_earning;
}

public void set_gross_sale( double gross_sale)
{ this.gross_sale=gross_sale; }

double get_gross_sale()
{return this.gross_sale; }

public void set_comission_rate(double commission_rate)
{this.commission_rate=commission_rate; }

double get_comission_rate()
{return this.commission_rate; }

public String toString()
{
System.out.println(“================================================================”);
System.out.println(“Gross Sales\t\t:\t”+get_gross_sale());
System.out.println(“Comission Rate (%)\t:\t”+get_comission_rate());
System.out.println(“Commission Earning\t:\t”+earning());

return “\n\n”;
}

}

 

package labexam02;

public class Base_Plus_Commission_Employee extends ComissionEmployee {

double gross_sale, com_percentage, base_salary, total_salary, commission_Percentage,commission_earned;
String commission_status;

Base_Plus_Commission_Employee(String name, double gross_sale, double com_percentage, double base_salary, String commission_status)
{

super(name, gross_sale, com_percentage);
Set_com_percentage(com_percentage );
Set_Gross_sale(gross_sale);
Set_base_Salary(base_salary);
Set_Commission_Status(commission_status);
}

public void Set_base_Salary(double base_salary)
{ this.base_salary =base_salary; }

public double Get_base_Salary()
{ return this.base_salary;}

public void Set_Gross_sale(double gross_sale)
{ this.gross_sale=gross_sale; }

public double Get_Gross_sale()
{ return this.gross_sale;}

public void Set_com_percentage(double com_percentage )
{ this.com_percentage = com_percentage; }

public double Get_com_percentage()
{ return this.com_percentage; }

public void Set_Commission_Status(String commission_status)
{ this.commission_status=commission_status; }

public String Get_Commision_Status()
{ return this.commission_status; }

public double commission_earned()
{
commission_earned = (Get_Gross_sale()*Get_com_percentage())/100;

return commission_earned;
}

public double earning()
{
total_salary=Get_base_Salary() + commission_earned();

return total_salary;
}

public String toString()
{
System.out.println(“================================================================”);
System.out.println(“Base Salary\t\t:\t”+Get_base_Salary());
System.out.println(“Gross Sale\t\t:\t”+Get_Gross_sale());
System.out.println(“Comission Percentage(%)\t:\t”+Get_com_percentage());
System.out.println(“Commission Get? (Y/N)\t:\t”+Get_Commision_Status());
System.out.println(“Comission Earned\t:\t”+commission_earned());
System.out.println(“Total Salary\t\t:\t”+earning());
return “\n”;//+earning();
}

}

 

package labexam02;

public class Employee_Main {
public static void main(String[] args) {

Employee e[]={new ComissionEmployee(“Md.Abdullah Al Kamal”, 60500.00, 5.50),
new Base_Plus_Commission_Employee(“Jamilur Reza Choudhury”, 130500.00, 10, 55000, “Yes”)};

//for(Employee E: e);

for(int i=0; i<2;i++)
{
if(e[i] instanceof Base_Plus_Commission_Employee == true)
{
//Base_Plus_Commission_Employee BCE = (Base_Plus_Commission_Employee) e;

System.out.println(“Employee Name\t\t:\t”+e[i].getEmployee());
System.out.println(e[i]);
}
else
{

System.out.println(“Employee Name\t\t:\t”+e[i].getEmployee());
System.out.println(e[i]);

}
}

}

}

 Calculator:

 

package kamal_programs;

public class Calculator {

int sum, firstnum, secondnum, sub;
double sum1, firstnum1, secondnum1, sub1;

public Calculator()
{
System.out.println(“Object Created”);
System.out.println(“”);

}

public int add(int a, int b)
{
int sum;
sum=a+b;
this.sum=sum;

return sum;
}

public double add(double a, double b)
{ double sum;

sum=a+b;
this.sum1=sum;
return sum;

}

public int subtract(int a, int b)
{int sub;
sub=a – b;
this.sub=sub;
return sub;
}

public double subtract(double a, double b)
{double sub;

sub=a-b;
this.sub1=sub;
return sub;
}

public void addintDisplay()
{
System.out.println(“Integer Addition is:” +this.sum);

}

public void adddoubleDisplay()
{
System.out.println(“Float Addition is:” +this.sum1);

}

public void subintDisplay()
{
System.out.println(“Integer Subtract is:” +this.sub);

}

public void subdoubleDisplay()
{
System.out.println(“Float Subtract is:” +this.sub1);

}
}

 

package kamal_programs;

public class Calculator_Main {

public static void main(String[] args) {

Calculator C1=new Calculator();

C1.add(10,15);
C1.addintDisplay();

C1.add(10.5,25.75);
C1.adddoubleDisplay();

Calculator C2=new Calculator();
C2.subtract(25,18);
C2.subintDisplay();

C2.subtract(25.75,8.25);
C2.subdoubleDisplay();

}

}

 Calendar:

package kamal_programs;

public class Calender
{
int day, month, year;

public Calender(int d, int m, int y) //Constrator
{

if(d>0 && m>0 && y>0) //Method
{
day=d;
month=m;
year=y;
}
else
{
day=0;
month=0;
year=0;
}

}

public void displayDate()
{

//System.out.printf(“Date: %d / %d / %d\n”,day,month,year);
System.out.println(“Date:” +day+”/”+month+”/”+year);

}

}

package kamal_programs;

public class Calendermain
{

public static void main(String[] args)
{

Calender D1=new Calender(10, 5, 2015);
D1.displayDate();

Calender D2=new Calender(12, 12, 2015);
D2.displayDate();

Calender D3=new Calender(-5, 10, 2015);
D3.displayDate();

}
}

 Invoice:

 

package kamal_programs;

public class Invoice {

String part_number, description;
int quantity, price;

public Invoice(String part_number, String description, int quantity, int price)
{
this.part_number=part_number;
this.description=description;
this.quantity=quantity;
this.price=price;

}

public Invoice(String part_number, int quantity, int price)
{

this.part_number=part_number;
this.quantity=quantity;
this.price=price;

}

public void display1()
{
System.out.println(“Part Number:”+this.part_number);
System.out.println(“Description: “+this.description);
System.out.println(“Quantity: “+this.quantity);
System.out.println(“Price:”+this.price);

}

public void display2()
{
System.out.println(“Part Number:”+this.part_number);
System.out.println(“Quantity: “+this.quantity);
System.out.println(“Price:”+this.price);

}

}

 

package kamal_programs;

public class Invoice_Main {

public static void main(String[] args) {

Invoice I1=new Invoice(“A1001″,”Description”,50,10);
I1.display1();

System.out.println(“”);

Invoice I2=new Invoice(“B001”,5000,100);
I2.display2();

}

}

Advertisements

Please Login to Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s