Computer Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment 2

Name – Sourasish Mondal


University Roll No. – 11500221053
Stream – CSE Section – B Group – 2
Subject – Object Oriented Programming Lab
Subject Code – PCC-CS593
Problem Statement:

1. (a) In class Employee created in Assignment 1 add static instance field emp_count.

Use emp_count for getting emp_id in proper sequence. Also include appropriate

accessor method for the instance field emp_count

(b) Write a static method in Employee class.

(c) Write methods to compare two Employees based upon their salary and return object
having higher salary.

(d) Write two overloading methods in your Employee class.

(e) Use final keyword for creating a constant field.

Code:

public class Employee{


String name;
int emp_id;
int salary;
String designation;
static int emp_count = 0;
final String COMPANY = "GOOGLE";

// For Executive
public Employee(String name, int salary, String designation, String COMPANY){
this.name = name;
this.emp_id = emp_count++;
this.salary = salary;
this.designation = designation;
this.COMPANY = COMPANY;
}

// For Freshers
public Employee(String name){
this.name = name;
emp_id = emp_count++;
salary = 20000;
designation = "Developer";
}

// For Temporary Member


public Employee(){
name = "Guest";
emp_id = emp_count++;
salary = 1000;
designation = "Rather not to say";

public static void main(String[] args){


Employee employee[] = new Employee[3];
// this is for Freshers
employee[0] = new Employee("Sourasish Mondal");
// this is for Executive
employee[1] = new Employee("Ankita Dutta", 200000, "CEO", "Microsoft");
// this is for Temporary Member
employee[2] = new Employee();

System.out.println("\nFreshers \n");
employee[0].display();
System.out.println("\nExecutive \n");
employee[1].display();
System.out.println("\nTemporary Member \n");
employee[2].display();

//Assignment 2 part c
System.out.println("\nPerson having high salary is = " +
Employee.compareSalary(employee[0], employee[1]));

// Assignment 2 part d
employee[0].salary_increase(10, 12000);
employee[1].salary_increase(20);
System.out.println("\nEmployee [0] = " + employee[0].salary);
System.out.println("Employee [1] = " + employee[1].salary);

//Assignment 2
System.out.println("\nNo. of employee in the organisation = " + emp_count);

public void display(){


System.out.println("Designation = " + designation);
System.out.println("Name = " + name);
System.out.println("Salary = " + salary);
System.out.println("Employee ID = " + emp_id);
}
public static String compareSalary(Employee a, Employee b){
if(a.salary >= b.salary){
return a.getName();
} else {
return b.getName();
}
}

//Assignment 2: part d
public void salary_increase(int p, int b){
salary = salary + salary * p/100 + b;
}
public void salary_increase(int p){
salary = salary + salary * p/100;
}

public String getName(){


return name;
}
}

Output:
For final keyword error output:

Code:

public class Employee{ String name;

int emp_id;
int salary;
String designation;
static int emp_count = 0;
final String COMPANY = "GOOGLE";

// For Executive
public Employee(String name, int salary, String designation, String COMPANY){

this.name = name;
this.emp_id = emp_count++; this.salary = salary; this.designation = designation;
this.COMPANY = COMPANY;

}
public static void main(String[] args){

Employee employee[] = new Employee[3];


// this is for Freshers
employee[0] = new Employee("Sourasish Mondal"); // this is for Executive

employee[1] = new Employee("Ankita Dutta", 200000, "CEO", "Microsoft"); // this is for


Temporary Member
employee[2] = new Employee();

System.out.println("\nFreshers \n"); employee[0].display();


System.out.println("\nExecutive \n"); employee[1].display();
System.out.println("\nTemporary Member \n"); employee[2].display();

}
2. Write a java program to illustrate - “Java uses pass by value”

Code:

public class Swap {

public static void main(String args[]) {


Animal a1 = new Animal("Lion");
Animal a2 = new Animal("Crocodile");

System.out.println("Before Swap:- a1:" + a1 + "; a2:" + a2);


swap(a1, a2);
System.out.println("After Swap:- a1:" + a1 + "; a2:" + a2);
}

public static void swap(Animal animal1, Animal animal2) {


Animal temp = new Animal("");
temp = animal1;
animal1 = animal2;
animal2 = temp;
}

class Animal {
String name;

public Animal(String name) {


this.name = name;
}

public String toString() {


return name;
}
}

Output:

You might also like