Employe

Anonymous
timer Asked: Dec 17th, 2017
account_balance_wallet $15

Question description

please use Eclipse Jee Oxygen and can you combine all the code to word

Topics ass 15

Object Type Casting.

Upcasting.

Downcasting.

instanceof

Polymorphism.

Polymorphism using inheritance.

Description

Write a program to compute bonuses earned this year by employees in an organization. There are three types of employees: workers, managers and executives. Each type of employee is represented by a class. The classes are named Worker, Manager and Executive and are described below in the implementation section.

You are to compute and display bonuses for all the employees in the organization using a single polymorphic loop. This will be done by creating an abstract class Employee and making the classes Worker, Manager and Executive as subclasses of the class Employee. This will allow for creating an array of Employee references and storing in it references of Worker, Manager and Executive objects. (This can be done because objects of classes, Worker, Manager and Executives are also of type Employee as Employee is their parent class and they inherit all its characteristics).

Implementation

Create the following classes:

class Employee

Create an abstract class Employee that includes the following:

Two fields, name and salary, for holding employee name and salary.

A constructor for initializing name and salary.

Accessor methods, getName ( ) and getSalary ( ) for returning name and salary respectively.

An abstract method computeBonus ( ) for computing and returning the bonus. (Do not provide a body for this method).

class Worker

Create a class Worker that extends Employee and provides the following:

A field pctBonus for specifying the percentage value for the bonus.

A constructor to initialize name, salary and pctBonus.

An accessor method getPctBonus for returning pctBonus value.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = salary * pctBonus

class Manager

Create a class Manager that extends Employee and provides the following:

A field pctBonus for specifying the percentage value for the bonus.

A field travelExpense for specifying travel expenses assigned.

A constructor to initialize name, salary, pctBonus, and travelExpense.

An accessor method getPctBonus for returning pctBonus value.

An accessor method getTravelExpense for returning travelExpense.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = (salary * pctBonus) + 500.00

class Executive

Create a class Executive that extends Employee and provides the following:

A field pctBonus for specifying the percentage value for the bonus.

A field travelExpense for specifying travel expenses available.

A field optionsCount for specifying the number of stock options awarded.

A constructor to initialize name, salary, pctBonus, travelExpense, and optionsCount.

An accessor method getPctBonus for returning pctBonus value.

An accessor method getTravelExpense for returning travelExpense.

An accessor method getOptionsCount for returning optionsCount.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = (salary * pctBonus) + 1000.00

class TestEmployee

Create a class TestEmployee containing the main method. The method main will do the following:

Prompt the user to enter the number of workers, say nw.

Prompt the user to enter the number of managers, say nm.

Prompt the user to enter the number of executives, say ne.

Compute the total number of employee (say n) as below:

n = nw + nm + ne

Create an array of n Employee references. (The objects will be created in the subsequent steps).

Create nw Worker objects. Do this by setting up an nw count loop. In each pass through the loop, do the following:

Ask the user to enter data for one worker.

Create a Worker object and initialize it with data provided by the user.

Store the Worker object reference in the array of Employee references created earlier at the appropriate location in the Employee array.

Create nm Manager objects. Do this by setting up an nm count loop.

In each pass through the loop, do the following:

Ask the user to enter data for one manager.

Create a Manager object and initialize it with data provided by the user.

Store the Manager object reference in the array of Employee references created earlier.

(Store the Manager references in the array elements following where Worker references were stored.)

Create ne Executive objects. Do this by setting up an ne count loop.

In each pass through the loop, do the following:

Ask the user to enter data for one executive.

Create an Executive object and initialize it with data provided by the user.

Store the Executive object reference in the array of Employee references created earlier.

(Store the Executive references in the array elements following where Manager references were stored).

Find the total bonus amount paid to each employee.

Do this by calling the function computeBonus( ) of each object

using a polymorphic call within a single polymorphic loop as shown below.

In the code below, employee is an array of Employee references containing objects of various Employee subclasses (Worker, Manager, Executive).

String name;

double salary, pctBonus, bonus, travelExpense;

int optionsCount;

for (int i=0; i < employee.length; i++)

{

//get name and salary

//To access target object methods that are defined in class Employee, we don’t need type casting.

name = employee[i].getName ( );

salary = employee[i].getSalary ( );

//call computeBonus using polymorphic call

//Since computeBonus is declared in Employee class, we don’t need type casting.

bonus = employee [i].computeBonus ( );

//use name, salary, bonus, pctBonus returned above towards building the summary report.

String out, outW=””, outM=””, outE=””;

//To access target object methods that are not defined in class Employee, we use down casting.

//Downcasting (Widening) should be done within an if statement using instanceof

//Since getPctBonus method is not present in class Employee, it is accessed via down casting.

//Similarly getTravelExpense, and getOptionsCount are accessed via downcasting.

//The instanceof clause is used below to ensure a safe down casting (widening).

if (employee[i] instanceof Worker){

pctBonus = ( (Worker) employee[i] ).getPctBonus ( );

//accumulate Worker output in outW

outW = outW + “Name:” + name + “\n”;

//add other values in outW

}

else if (employee[i] instanceof Manager) {

pctBonus = ( (Manager) employee[I] ).getPctBonus ( );

travelExpense = ( (Manager) employee[I] ).getTravelExpense ( );

//accumulate Manager output in outM

outM = outM + “Name:” + name + “\n”;

//add other values in outM

}

else if (employee[i] instanceof Executive) {

pctBonus = ( (Executive) employee[I] ).getPctBonus ( );

travelExpense = ( (Executive) employee[I] ).getTravelExpense ( );

optionsCount = ( (Executive) employee[I] ).getOptionsCount ( );

//accumulate Executive output in outE

outE = outE + “Name:” + name + “\n”;

//add other values in outE

}

}

//After getting out of the loop

out = outW + outM + outE:

//display out

JOPtionPane.showMessageDialog(null, out);

Testing

Use the following test data.

In the data below, there are 3 workers, 2 managers and 1 executive.

The first employee is John Adam and has a yearly salary of $60000.00 and is paid 5% yearly bonus.

Enter Number of Workers:

3

Enter Number of Managers:

2

Enter Number of Executives:

1

Enter a worker data:

John Adam, 60000, .05

Enter a worker data:

Rick Smith, 65000, .05

Enter a worker data:

Raymond Woo, 70000, .05

Enter a manager data:

Ray Bartlett, 80000, .10, 5000

Enter a manager data:

Mary Russell, 85000, .10, 5000

Enter an executive data:

Andy Wong, 100000, .15, 10000, 500

Output

The output may appear as below:

Name: John Adam

Salary: $ 60000.00

PercentBonus: .05

Total Bonus: $ 3000.00

Name:: Rick Smith

Yearly Salary: $ 65000.00

PercentBonus: .05

Total Bonus: $ 3250.00

Name: Raymond Woo

Yearly Salary: $ 70000.00

PercentBonus: .05

Total Bonus: $ 3500.00

Name: Ray Bartlet

Yearly Salary: $ 80000.00

PercentBonus: .10

Total Bonus: $ 8500.00

Travel Expense: $ 5000.00

Name: Mary Russel

Yearly Salary: $ 85000.00

PercentBonus: .10

Total Bonus: $ 9000.00

Travel Expense: $ 5000.00

Name: Andy Wong

Yearly Salary: $ 100000.00

PercentBonus: .15

Total Bonus: $ 16000.00

Travel Expense: $ 10000.00

Options Count: 500

Submit

Submit a printed copy of the source code and the output.

No need to turn in the input.

Polymorphism

In the code above, calls to methods are not bound (resolved) at compile time. Instead, they are bound at run time. In Java a call to an instance method is bound at run time to the corresponding method in the target object. For example, the call to computeBonus is bound at run time to the corresponding method in the target object. Since, the target object may be different in each pass through the loop, the call may be bound to a different computeBonus method in each pass through the loop. This characteristic is called polymorphism and such a call is referred as polymorphic call.

In the code above, for accessing target object methods that are present in class Employee, we need not use any type casting. However for accessing target object methods that are not present in class Employee, we must use explicit down casting. Since down casting is potentially unsafe, we do this using an instanceof clause within an if/else statement. The instanceof clause checks for the type of its target object at run time. We use the instanceof clause before down casting to make sure that the target object is of the specific type before we down cast it to that required type.

Discussion

Abstract Class

An abstract class is just like any other concrete class except for the following:

Compiler does not let you create an object of an abstract class.

An abstract class has a constructor. It may have instance variables and instance methods just like any other concrete class. However, additionally, it my have one or more abstract methods unlike a concrete class. A child of an abstract class must provide a concrete version of each of its abstract methods. If it doesn’t, compiler treats it also as an abstract class.

Reference Type

In Java a reference variable is used to point to an object. It contains the address of the target object. Each reference variable has a type associated with it. Its type refers to the type of its target object. Specifically, it refers to the way the reference variable views the target object. For example, in the code fragment below, the reference variable st is of type Student. This means that it views the target object as a Student object. Using st, the user can reference Student object variables and methods.

Student st;

st = new Student ( );

Normally, a user uses reference variable to keep the reference of an object of its target type. For example, in the code fragment below, the reference obj1 is of Object type and is being used to keep the reference of an Object object. Similarly, the reference st1 is of Student type and is being used to keep the reference of a Student object.

However, a reference variable can be assigned the reference of its ancestor or descendent types. For example, in the code fragment below, the reference obj2 is of type Object. However, it is being assigned the reference of Student type ( its descendent type). Similarly, the reference st2 is of Sibling type. However, it is being assigned the reference of Object type (its ancestor type).

Object obj1, obj2;

Student st1, st2;

Object obj1 = new Object ( );

Student st1 = new Student ( );

//Up Casting or Narrowing

obj2 = st1;

//Down Casting or Widening

st1 = (Student) obj1;

Although a reference may be assigned a reference of its descendent or ancestor type, it still views the target object as its own type. In the above example, although obj2 is pointing to a Student object, it still views it as an Object object. By using the reference obj2, the user will be able to reference Object fields and methods (but not Student fields and methods). On the other hand, the refernce st1 is of type Student. Therefore, by using the st1, the user will be able to reference Student fields/methods (and also Object fields/methods because Student inherits Object fields/methods).

When a reference is assigned a descendent type reference, it is referred to as Up Casting or Narrowing, It is referred to as Up Casting because in this case a reference low in inheritance hierarchy is assigned to a reference up in hierarchy. It is also referred to as Narrowing because the new reference views the target object with a narrower view than the original reference being assigned to it.

In the example above, the st1 reference which is pointing to a Student object is being assigned to reference obj2 of type Object. This is up casting because a Student reference (low in hierarchy) is being assigned to an Object reference (up in hierarchy). This is also narrowing because the reference obj2 will view the target object as an Object instead of as a Student object.

The Up Casting is safe and can be done without explicit casting.

When a reference is assigned an ancestor type reference, it is referred to as Down Casting or Widening, It is referred to as Down Casting because a reference up in inheritance hierarchy is being assigned to a reference low in hierarchy. It is also referred to as Widening because the new reference will view the object with a wider view than the reference being assigned to it.

In the example above, the obj1 reference which is pointing to an Object object is being assigned to reference st2 of type Student. This is Down casting because an Object reference (up in inheritance hierarchy) is being assigned to a Student reference (low in inheritance hierarchy). This is also widening because the reference st2 will view the target object as a Student object instead of as an Object object.

The Down Casting or Widening is potentially unsafe and must be done using explicit casting as shown in the above example.

Since Down Casting is potentially unsafe even when done using explicit type casting, it is recommended that it be done within an If statement containing an instanceof as shown below.

Object obj = new Object ( );

Sibling sib;

if (obj instanceof Sibling)

{

//downcasting becomes safe when done using instanceof

sib = (Sibling) obj;

}

Note that in the above example, the down casting will not take place if the result of the instanceof expression is false as is the case in the above example. Thus Down casting done using an If statement containing an instanceof clause as above is safe.

Purpose

Java allows Up Casting to make polymorphism possible. Once up casting is allowed. It is expected that down casting be also allowed to return to the original type. Thus Java also allows Down Casting when done using explicit casting.

Formating A Deciamal Number:

The following will convert a double n to 2 places and will display it.

Method 1:

import java.text.*;

double n = 999.9999;

DecimalFormat df = new DecimalFormat ( “.00”);

String nFormatted = df.format (n);

JOptionPane.showMessageDialog (null, nFormatted);

Method 2:

import java.text.*;

double n = 999.9999;

DecimalFormat df = new DecimalFormat ( );

df.applyPattern ( “.00” );

String nFormatted = df.format (n);

JOptionPane.showMessageDialog (null, nFormatted);

Patterns:

.000 diplay to three decimal places. If not enough digit, fill them with zeror.

####.000 display 4 places before the decimal places. If more than 4, display them all.

Tutor Answer

lasermaster
School: Purdue University

Attached.

public abstract class Employee {
String name;
double salary;

public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}

public String getName(){
return name;
}

public double getSalary(){
return salary;
}

public abstract double computeBonus();
}

public class Executive extends Employee{

double pctBonus, travelExpense;
int optionsCount;

public Executive(String name, double salary, double pctBonus, double travelExpense, int
optionsCount) {
super(name, salary);
this.name = name;
this.salary = salary;
this.pctBonus = pctBonus;
this.travelExpense = travelExpense;
this.optionsCount = optionsCount;
}

public double computeBonus() {

return (salary * pctBonus) + 1000.0;

}

public double getPctBonus(){

return pctBonus;

}

public double getTravelExpense(){

return travelExpense;

}

public int getOptionsCount(){

return optionsCount;

}

}

public class Manager extends Employee{

double pctBonus, travelExpense;

public Manager(String name, double salary, double pctBonus, double travelExpense) {
super(name, salary);
this.name = nam...

flag Report DMCA
Review

Anonymous
Goes above and beyond expectations !

Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors