• Tidak ada hasil yang ditemukan

King Saud University College of Computer and Information Sciences Department of Computer Science CSC113 – Computer Programming II – Tutorial No 6 – Spring 2017

N/A
N/A
Protected

Academic year: 2025

Membagikan "King Saud University College of Computer and Information Sciences Department of Computer Science CSC113 – Computer Programming II – Tutorial No 6 – Spring 2017"

Copied!
5
0
0

Teks penuh

(1)

Employee class:

Attributes:

name: the name of the employee.

rank: rank in the company structure (1-15).

yearsOfExperience: number of years of experience for the employee.

Methods:

Employee (name: String, rank: int, yearsOfExperience: int):

constructor

getRank(): this method returns the rank of employee.

getYearsOfExperience (): returns the years of experience of the employee.

display(): this method display all the attributes of the employee or consultant or manager

calculateSalary(): this method calculates and return the Salary of the employee as the following:

 Employee: years of experience * 500 + rank * 1000

 Manager: years of experience * 500 + rank * 1500 + nbOfEmps * 10

 Consultant: hoursPerWeek * 1000 + rank *500

(2)

Manager class:

Attributes:

 nbEmps: number of managed employees Methods:

 Manager (name: String, rank: int, yearsOfExperience: int, nbEmps:

int): constructor

 getNbEmps(): this method returns the number of managed employees

Consultant class:

Attributes:

 hoursPerWeek: number of hours per week.

Methods:

 Consultant(name: String, rank: int, yearsOfExperience: int, hoursPerWeek: int): constructor

Company class:

Attributes:

 name: the name of the company.

Methods:

 Company (name: String, size: int): constructor

 addEmployee(Employee e): this method adds an employee to the company. An employee can be of type Employee, type Manager or type Consultant.

 displayAll (): Display all employees and their attributes along with their salary.

 countConsultant(): this method returns the number of all Consultants in the company

 getHighSalary(double s): this method returns an array all employees with salary greater than s.

 getManagers (int n): returns an array of all managers with nbEmps greater than n.

Exercise: Translate into Java-code the classes Employee, Manager, Consultant, and Company.

(3)

public class Employee { private String name;

private int rank;

private int yearsOfExperince;

Employee(String name, int rank, int yearsOfExperince) { this.name = name;

this.rank = rank;

this.yearsOfExperince = yearsOfExperince;

}

Employee(Employee e) { name = e.name;

rank = e.rank;

yearsOfExperince = e.yearsOfExperince;

}

public int getRank() { return rank;

}

public int getYearsOfExperince() { return yearsOfExperince;

}

public double calculateSalary() {

return yearsOfExperince * 500 + rank * 1000;

}

public void display(){

System.out.println("name: " + name +" Rank: " + rank + " Years of Exp: " + yearsOfExperince + " Salary: " + calculateSalary());

} }

(4)

public class Manager extends Employee{

private int nbEmps;

Manager(String name, int rank, int yearsOfExperince, int nbEmps) { super(name, rank, yearsOfExperince);

this.nbEmps = nbEmps;

}

Manager(Manager m){

super(m);

this.nbEmps = m.nbEmps;

}

public int getNbEmps() { return nbEmps;

}

public void display(){

super.display();

System.out.println("Number of employees: " + nbEmps);

}

public double calculateSalary(){

return getYearsOfExperince() * 500 + getRank() * 1500 + nbEmps * 10;

} }

public class Consultant extends Employee{

private int hoursPerWeek ;

Consultant(String name, int rank, int yearsOfExperince, int hoursPerWeek) {

super(name, rank, yearsOfExperince);

this.hoursPerWeek = hoursPerWeek ; }

Consultant(Consultant c){

super(c);

hoursPerWeek= c.hoursPerWeek;

}

public int getHoursPerWeek() { return hoursPerWeek;

}

public void display(){

super.display();

System.out.println("Hours per Week: " + hoursPerWeek);

}

public double calculateSalary(){

return hoursPerWeek * 1000 + getRank() * 500 ; }

}

(5)

public class Company { private String name;

private Employee[] arrEmps;

private int nbEmp;

Company(String name, int size){

this.name = name;

arrEmps = new Employee[size];

nbEmp = 0;

}

public boolean addEmployee(Employee e){

if(nbEmp >= arrEmps.length) return false;

if(e instanceof Manager)

arrEmps[nbEmp++] = new Manager((Manager)e);

else if(e instanceof Consultant)

arrEmps[nbEmp++] = new Consultant((Consultant)e);

else

arrEmps[nbEmp++] = new Employee(e);

return true;

}

public void displayAll(){

System.out.println("Name: " + name);

for(int i = 0; i < nbEmp; i++)

arrEmps[i].display(); // polymorphism here }

public int countConsultant(){

int count=0;

for(int i = 0; i < nbEmp; i++)

if(arrEmps[i] instanceof Consultant) count++;

return count;

}

public Employee[] getHighSalary(double s){

Employee[] res = new Employee[nbEmp];

int j = 0;

for(int i = 0; i< nbEmp; i++)

if(arrEmps[i].calculateSalary() > s) // polymorphism here res[j++]= arrEmps[i];

return res;

}

public Manager[] getManagers(int n){

Manager[] res = new Manager[nbEmp];

int j=0;

for(int i = 0; i < nbEmp; i++)

if(arrEmps[i] instanceof Manager)

if(((Manager)arrEmps[i]).getNbEmps() > n) res[j++]=(Manager) arrEmps[i];

return res;

} }

Referensi

Dokumen terkait

In this paper we put forward an intrusion detection model integrating chi-square feature selection and multi class support vector machine for high accuracy and low false positive rate..

For the 10%, 15%, and 20% noise ratios, CFTNB outper- forms PFTNB and NFTNB with respect to the average classi- fication accuracy and number of data sets on which the methods achieve

Instead of using a named entity tagger to directly identify names and their spelling variants, we link spelling variants with a name in another language via a method that is widely used

Moreover, the proposed refactoring patterns are formalized in B, using systematic rules for trans- forming a class diagram and its OCL constraints into B Ledang, 2001; Marcano and Levy,

This paper compares neural network based sentiment classification methods back propagation neural network BPN, probabilistic neural network PNN & homogeneous ensemble of PNN HEN using