Description
Unformatted Attachment Preview
Using Operators to Move Objects
Open the FlyingBird_Lesson2 program. Use save as and resave the program as
FlyingBird_Lesson3. In the FlyingBird_Lesson3 program do the following:
Add the methods and variables as indicated below.
Part 1: Open the editor for the Bird1 class and create the following variables and
methods.
Name of Method
Purpose
public void applyGravity()
This method will simulate gravity causing the
bird to fall. Use the bird’s current x and
increment the Y using the velocity variable.
Variables
needed
velocity
gravity
Part 1: ACTIVITIES TO DO – Complete the following 1-4 activities
1) Open the source code editor for the Bird1 and create the global instance variable for
velocity and the class variable for gravity in the field area.
velocity is a double, private variable, set it to 0
Gravity is a static final double private variable, set it to 0.5
2) Under the act method, Create the method header for the applyGravity() method. It
will be public and void.
This method has two statements.
a) Use the setLocation(int x, int y) method and write a statement that will get the
current x and y for the bird and add the velocity variable to the Y coordinate.
b) Write an expression that will update the velocity variable by adding the gravity
variable to it.
3) Add the applyGravity() method to the act() method to call it.
4) Test to see if the bird falls. Debug if needed.
Turning It In
Using Operators to Move Objects
Create a zip file called Using_Operators_To_Move_Objects and place the entire
FlyingBird_Lesson3 Greenfoot folder in it. Turn in the zip file to the Using Operators to
Move Objects assignment.
Purchase answer to see full attachment
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Explanation & Answer
View attached explanation and answer. Let me know if you have any questions.For this assignment, ...
Completion Status:
100%
Review
Review
Anonymous
Just what I needed. Studypool is a lifesaver!
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Most Popular Content
CS 230 Southern New Hampshire University Software Design Paper
Attach is the rubric for the assignment please update me if additional information is needed
CS 230 Southern New Hampshire University Software Design Paper
Attach is the rubric for the assignment please update me if additional information is needed
American Military University Creating a Flowchart
Getting Started with Documenting your ProgramThis assignment will assess your basic knowledge and use of flowcharts. While ...
American Military University Creating a Flowchart
Getting Started with Documenting your ProgramThis assignment will assess your basic knowledge and use of flowcharts. While there are a number of different symbols to support building a flowchart, use the attached basic flowchart symbols.Create a FLOWCHART for each problem below!Use the information below to solve each problem using flowchart symbols to illustrate how you would complete your program. You may use Microsoft PowerPoint® or Microsoft Word ® for your flowchart.Problem 1: Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate. (The formula to calculate payroll is pay = hourly rate * hours worked.) Display hourly rate, hours worked, and pay. ( Do not add any feature such as overtime, bonus, taxes, insurance, … )Problem 2: Build a flowchart that will calculate the average miles per gallon obtained on a trip. Input the amount of gas used and the number of miles driven. (The formula to calculate miles per gallon is miles per gallon = number of miles driven / amount of gas used. ) Display gas used, miles driven and MPG.Use your own values to test the flow. The values should not be part of the flowchart. The flowchart is a blue print for any programming language and for a wide range of input values.
Employe
please use Eclipse Jee Oxygen and can you combine all the code to wordTopics ass 15Object Type Casting.Upcasting.Downcasti ...
Employe
please use Eclipse Jee Oxygen and can you combine all the code to wordTopics ass 15Object Type Casting.Upcasting.Downcasting.instanceofPolymorphism.Polymorphism using inheritance.DescriptionWrite 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).ImplementationCreate the following classes:class EmployeeCreate 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 WorkerCreate 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 * pctBonusclass ManagerCreate 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.00class ExecutiveCreate 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.00class TestEmployeeCreate 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 outWoutW = 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 outMoutM = 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 outEoutE = outE + “Name:” + name + “\n”;//add other values in outE }}//After getting out of the loopout = outW + outM + outE://display outJOPtionPane.showMessageDialog(null, out);TestingUse 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:3Enter Number of Managers:2Enter Number of Executives:1Enter a worker data:John Adam, 60000, .05Enter a worker data:Rick Smith, 65000, .05Enter a worker data:Raymond Woo, 70000, .05Enter a manager data:Ray Bartlett, 80000, .10, 5000Enter a manager data:Mary Russell, 85000, .10, 5000Enter an executive data:Andy Wong, 100000, .15, 10000, 500OutputThe output may appear as below:Name: John AdamSalary: $ 60000.00PercentBonus: .05Total Bonus: $ 3000.00Name:: Rick SmithYearly Salary: $ 65000.00PercentBonus: .05Total Bonus: $ 3250.00Name: Raymond WooYearly Salary: $ 70000.00PercentBonus: .05Total Bonus: $ 3500.00Name: Ray BartletYearly Salary: $ 80000.00PercentBonus: .10Total Bonus: $ 8500.00Travel Expense: $ 5000.00Name: Mary RusselYearly Salary: $ 85000.00PercentBonus: .10Total Bonus: $ 9000.00Travel Expense: $ 5000.00Name: Andy WongYearly Salary: $ 100000.00PercentBonus: .15Total Bonus: $ 16000.00Travel Expense: $ 10000.00Options Count: 500SubmitSubmit a printed copy of the source code and the output.No need to turn in the input.PolymorphismIn 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.DiscussionAbstract ClassAn 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 TypeIn 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 Narrowingobj2 = st1;//Down Casting or Wideningst1 = (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 instanceofsib = (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.PurposeJava 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.
2 pages
Python Flowchart
Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate ...
Python Flowchart
Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate (the formula to calculate ...
Similar Content
Cloud Computing
Pick one of the options:Explain the concept and application of federated identity. How is it used with cloud services? Wh...
CSIS 240 Grossmont College Graph Matrix Java Program Project
5/12/22, 11:10 AM
PP: Graph/Matrix
PP: Graph/Matrix
Start Assignment
Due Tuesday by 11:59pm
Points 35
Submitting a fi...
University of Central Florida C language Problems
Demographics
# This is a Unix shell script that will be used to test project 7.
# To use the script, copy it into the same...
Write a Java program (non-GUI preferred) to demonstrate the Singleton pattern, assignment help
Write a Java program (non-GUI preferred) to demonstrate the Singleton pattern.The key parts of the singleton pattern ...
mentally fear, emotionally fear and physically fear, assignment help
introduction, the thesis statement are : mentally fear, emotionally fear and physically fear. and follow the style below ...
University of The Cumberlands Data Capture with Wireshark Project
...
Related Tags
Book Guides
Get 24/7
Homework help
Our tutors provide high quality explanations & answers.
Post question
Most Popular Content
CS 230 Southern New Hampshire University Software Design Paper
Attach is the rubric for the assignment please update me if additional information is needed
CS 230 Southern New Hampshire University Software Design Paper
Attach is the rubric for the assignment please update me if additional information is needed
American Military University Creating a Flowchart
Getting Started with Documenting your ProgramThis assignment will assess your basic knowledge and use of flowcharts. While ...
American Military University Creating a Flowchart
Getting Started with Documenting your ProgramThis assignment will assess your basic knowledge and use of flowcharts. While there are a number of different symbols to support building a flowchart, use the attached basic flowchart symbols.Create a FLOWCHART for each problem below!Use the information below to solve each problem using flowchart symbols to illustrate how you would complete your program. You may use Microsoft PowerPoint® or Microsoft Word ® for your flowchart.Problem 1: Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate. (The formula to calculate payroll is pay = hourly rate * hours worked.) Display hourly rate, hours worked, and pay. ( Do not add any feature such as overtime, bonus, taxes, insurance, … )Problem 2: Build a flowchart that will calculate the average miles per gallon obtained on a trip. Input the amount of gas used and the number of miles driven. (The formula to calculate miles per gallon is miles per gallon = number of miles driven / amount of gas used. ) Display gas used, miles driven and MPG.Use your own values to test the flow. The values should not be part of the flowchart. The flowchart is a blue print for any programming language and for a wide range of input values.
Employe
please use Eclipse Jee Oxygen and can you combine all the code to wordTopics ass 15Object Type Casting.Upcasting.Downcasti ...
Employe
please use Eclipse Jee Oxygen and can you combine all the code to wordTopics ass 15Object Type Casting.Upcasting.Downcasting.instanceofPolymorphism.Polymorphism using inheritance.DescriptionWrite 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).ImplementationCreate the following classes:class EmployeeCreate 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 WorkerCreate 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 * pctBonusclass ManagerCreate 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.00class ExecutiveCreate 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.00class TestEmployeeCreate 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 outWoutW = 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 outMoutM = 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 outEoutE = outE + “Name:” + name + “\n”;//add other values in outE }}//After getting out of the loopout = outW + outM + outE://display outJOPtionPane.showMessageDialog(null, out);TestingUse 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:3Enter Number of Managers:2Enter Number of Executives:1Enter a worker data:John Adam, 60000, .05Enter a worker data:Rick Smith, 65000, .05Enter a worker data:Raymond Woo, 70000, .05Enter a manager data:Ray Bartlett, 80000, .10, 5000Enter a manager data:Mary Russell, 85000, .10, 5000Enter an executive data:Andy Wong, 100000, .15, 10000, 500OutputThe output may appear as below:Name: John AdamSalary: $ 60000.00PercentBonus: .05Total Bonus: $ 3000.00Name:: Rick SmithYearly Salary: $ 65000.00PercentBonus: .05Total Bonus: $ 3250.00Name: Raymond WooYearly Salary: $ 70000.00PercentBonus: .05Total Bonus: $ 3500.00Name: Ray BartletYearly Salary: $ 80000.00PercentBonus: .10Total Bonus: $ 8500.00Travel Expense: $ 5000.00Name: Mary RusselYearly Salary: $ 85000.00PercentBonus: .10Total Bonus: $ 9000.00Travel Expense: $ 5000.00Name: Andy WongYearly Salary: $ 100000.00PercentBonus: .15Total Bonus: $ 16000.00Travel Expense: $ 10000.00Options Count: 500SubmitSubmit a printed copy of the source code and the output.No need to turn in the input.PolymorphismIn 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.DiscussionAbstract ClassAn 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 TypeIn 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 Narrowingobj2 = st1;//Down Casting or Wideningst1 = (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 instanceofsib = (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.PurposeJava 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.
2 pages
Python Flowchart
Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate ...
Python Flowchart
Build a flowchart to solve a simple payroll calculation. Calculate the amount of pay, given hours worked, and hourly rate (the formula to calculate ...
Earn money selling
your Study Documents