Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Lab Assignment: Inheritance and Polymorphism

I. Overriding the equals Method

File Player.java contains a class that holds information about an athlete: name, team, and uniform number.

File ComparePlayers.java contains a skeletal program that uses the Player class to read in information about two baseball players and determine whether or not they are the same player.

1. Fill in the missing code in ComparePlayers so that it reads in two players and prints "Same player" if theyare the same, "Different players" if they are different. Use the equals method, which Player inherits fromthe Object class, to determine whether two players are the same. Are the results what you expect?

2. The problem is that as defined in the Object class, equals does an address comparison. It says thattwo objects are the same if they live at the same memory location, that is, if the variables that holdreferences to them are aliases. The two Player objects in this program are not aliases, so even if theycontain exactly the same information they will be "not equal." To make equals compare the actualinformation in the object, you can override it with a definition specific to the class. It might make sense tosay that two players are "equal" (the same player) if they are on the same team and have the same uniformnumber.

- Use this strategy to define an equals method for the Player class. Your method should take a Playerobject and return true if it is equal to the current object, false otherwise.

- Test your ComparePlayers program using your modified Player class. It should give the results youwould expect.

II. Another Type of Employee

The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and Hourly.java are from Listings 9.1 - 9.7 in the text. The program illustrates inheritance and polymorphism.

In this exercise you will add one more employee type to the class hierarchy (see Figure 9.1 in the text).

The employee will be one that is an hourly employee but also earns a commission on sales. Hence the class,which we'll name Commission, will be derived from the Hourly class.

Write a class named Commission with the following features:

- It extends the Hourly class.

- It has two instance variables (in addition to those inherited): one is the total sales the employee has made(type double) and the second is the commission rate for the employee (the commission rate will be typedouble and will represent the percent (in decimal form) commission the employee earns on sales (so .2would mean the employee earns 20% commission on sales)).

- The constructor takes 6 parameters: the first 5 are the same as for Hourly (name, address, phone number,social security number, hourly pay rate) and the 6th is the commission rate for the employee. Theconstructor should call the constructor of the parent class with the first 5 parameters then use the 6th to setthe commission rate.

- One additional method is needed: public void addSales (double totalSales) that adds the parameter to theinstance variable representing total sales.

- The pay method must call the pay method of the parent class to compute the pay for hours worked then addto that the pay from commission on sales. (See the pay method in the Executive class.) The total salesshould be set back to 0 (note: you don't need to set the hours Worked back to 0-why not?).

- The toStringmethod needs to call the toStringmethod of the parent class then add the total sales to that.

To test your class, update Staff.java as follows:

- Increase the size of the array to 8.

- Add two commissioned employees to the staffList-make up your own names, addresses, phone numbersand social security numbers. Have one of the employees earn $6.25 per hour and 20% commission and theother one earn $9.75 per hour and 15% commission.

- For the first additional employee you added, put the hours worked at 35 and the total sales $400; for thesecond, put the hours at 40 and the sales at $950.

Deliverables

1. Complete all the activities in sectionsI & II in this lab; then zip all your Java SOURCE CODE FILES for submission.

2. Write a lab report in Word or PDF document. The report should be named lab5.docx or lab.pdfand must contain the following:

a. The first page should be a cover page that includes the Class Number, Lab Activity Number, Date, and Instructor's name.

b. A description of the lab activities I & II, the concepts learned and applied. Be sure to elaborate on all the concepts applied. For example, it is not enough to say "I learned and applied Inheritance". You should be able to expand on which part of inheritance you applied, how it works, etc. A minimum of 2-page description is expected.

c. A 1-paragraph conclusion about your experience; how long you spent completing the lab, the challenges you faced.

Attachment:- Lab-Assignment.rar

Java, Programming

  • Category:- Java
  • Reference No.:- M92755469
  • Price:- $45

Priced at Now at $45, Verified Solution

Have any Question?


Related Questions in Java

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As