[Recommended]Java hw6

Java hw6 [IFT 102] Introduction to Java Technologies Lab 5: Inheritance and Polymorphism Score: 50 pts I. Overriding the equals Method File Player.java contains a…

Java hw6
[IFT 102]
Introduction to Java Technologies
Lab 5: Inheritance and Polymorphism
Score: 50 pts
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 they are the same, “Different players” if they are different. Use the equals method, which Player inherits from the 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 that two objects are the same if they live at the same memory location, that is, if the variables that hold references to them are aliases. The two Player objects in this program are not aliases, so even if they contain exactly the same information they will be “not equal.” To make equals compare the actual information in the object, you can override it with a definition specific to the class. It might make sense to say that two players are “equal” (the same player) if they are on the same team and have the same uniform number.
· Use this strategy to define an equals method for the Player class. Your method should take a Player object 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 you would expect.
// *********************************************************
// Player.java
//
// Defines a Player class that holds information about an athlete.
// **********************************************************
import java.util.Scanner;
public class Player
{
private String name;
private String team;
private int jerseyNumber;
//———————————————————–
// Prompts for and reads in the player’s name, team, and
// jersey number.
//———————————————————–
public void readPlayer()
{
Scanner scan = new Scanner(System.in);
System.out.print(“Name: “);
name = scan.nextLine();
System. out. print (“Team: “);
team = scan.nextLine();
System.out.print(“Jersey number: “);
jerseyNumber = Scan.nextInt();
}
}
// **************************************************************
// ComparePlayers
//
// Reads in two Player objects and tells whether they represent
// the same player.
// **************************************************************
import java.util.Scanner;
public class ComparePlayers
{
public static void main(String[] args)
{
Player player1 = new Player();
Player player2 = new Player();
Scanner scan = new Scanner();
//Prompt for and read in information for player 1
//Prompt for and read in information for player 2
//Compare player1 to player 2 and print a message saying
//whether they are equal
}
}
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 type double and will represent the percent (in decimal form) commission the employee earns on sales (so .2 would 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. The constructor should call the constructor of the parent class with the first 5 parameters then use the 6th to set the commission rate.
· One additional method is needed: public void addSales (double totalSales) that adds the parameter to the instance variable representing total sales.
· The pay method must call the pay method of the parent class to compute the pay for hours worked then add to that the pay from commission on sales. (See the pay method in the Executive class.) The total sales should be set back to 0 (note: you don’t need to set the hours Worked back to 0—why not?).
· The toString method needs to call the toString method 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 numbers and social security numbers. Have one of the employees earn $6.25 per hour and 20% commission and the other 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 the second, put the hours at 40 and the sales at $950.
Compile and run the program. Make sure it is working properly.
//*****************************************************************
// Firm.java Author: Lewis/Loftus
//
// Demonstrates polymorphism via inheritance.
// ****************************************************************
public class Firm
{
//————————————————————–
// Creates a staff of employees for a firm and pays them.
//————————————————————–
public static void main (String[] args)
{
Staff personnel = new Staff();
personnel.payday();
}
}
//********************************************************************
// Staff.java Author: Lewis/Loftus
//
// Represents the personnel staff of a particular business.
//********************************************************************
public class Staff
{
StaffMember[] staffList;
//—————————————————————–
// Sets up the list of staff members.
//—————————————————————–
public Staff ()
{
staffList = new StaffMember[6];
staffList[0] = new Executive (“Sam”, “123 Main Line”,
“555-0469”, “123-45-6789”, 2423.07);
staffList[1] = new Employee (“Carla”, “456 Off Line”,
“555-0101”, “987-65-4321”, 1246.15);
staffList[2] = new Employee (“Woody”, “789 Off Rocker”,
“555-0000”, “010-20-3040”, 1169.23);
staffList[3] = new Hourly (“Diane”, “678 Fifth Ave.”,
“555-0690”, “958-47-3625”, 10.55);
staffList[4] = new Volunteer (“Norm”, “987 Suds Blvd.”,
“555-8374”) ;
staffList[5] = new Volunteer (“Cliff”, “321 Duds Lane”,
“555-7282”);
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40);
}
//—————————————————————–
// Pays all staff members.
//—————————————————————–
public void payday ()
{
double amount;
for (int count=0; count < staffList.length; count++)
{
System.out.println (staffList[count]);
amount = staffList[count].pay(); // polymorphic
if (amount == 0.0)
System.out.println (“Thanks!”);
else
System.out.println (“Paid: ” + amount);
System.out.println (“————————————“);
}
}
}
//******************************************************************
// StaffMember.java Author: Lewis/Loftus
//
// Represents a generic staff member.
//******************************************************************
abstract public class StaffMember
{
protected String name;
protected String address;
protected String phone;
//—————————————————————
// Sets up a staff member using the specified information.
//—————————————————————
public StaffMember (String eName, String eAddress, String ePhone)
{
name = eName;
address = eAddress;
phone = ePhone;
}
//—————————————————————
// Returns a string including the basic employee information.
//—————————————————————
public String toString()
{
String result = “Name: ” + name + “n”;
result += “Address: ” + address + “n”;
result += “Phone: ” + phone;
return result;
}
//—————————————————————
// Derived classes must define the pay method for each type of
// employee.
//—————————————————————
public abstract double pay();
}
//******************************************************************
// Volunteer.java Author: Lewis/Loftus
//
// Represents a staff member that works as a volunteer.
//******************************************************************
public class Volunteer extends StaffMember
{
//—————————————————————
// Sets up a volunteer using the specified information.
//—————————————————————
public Volunteer (String eName, String eAddress, String ePhone)
{
super (eName, eAddress, ePhone);
}
//—————————————————————
// Returns a zero pay value for this volunteer.
//—————————————————————
public double pay()
{
return 0.0;
}
}
//******************************************************************
// Employee.java Author: Lewis/Loftus
//
// Represents a general paid employee.
//******************************************************************
public class Employee extends StaffMember
{
protected String socialSecurityNumber;
protected double payRate;
//—————————————————————
// Sets up an employee with the specified information.
//—————————————————————
public Employee (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
//—————————————————————
// Returns information about an employee as a string.
//—————————————————————
public String toString()
{
String result = super.toString ();
result += “nSocial Security Number: ” + socialSecurityNumber;
return result;
}
//—————————————————————
// Returns the pay rate for this employee.
//—————————————————————
public double pay()
{
return payRate;
}
}
//******************************************************************
// Executive.java Author: Lewis/Loftus
//
// Represents an executive staff member, who can earn a bonus.
//******************************************************************
public class Executive extends Employee
{
private double bonus;
//—————————————————————–
// Sets up an executive with the specified information.
//—————————————————————–
public Executive (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
bonus = 0; // bonus has yet to be awarded
}
//—————————————————————–
// Awards the specified bonus to this executive.
//—————————————————————–
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
//—————————————————————–
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
//—————————————————————–
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//******************************************************************
// Hourly.java Author: Lewis/Loftus
//
// Represents an employee that gets paid by the hour.
//*******************************************************************
public class Hourly extends Employee
{
private int hoursWorked;
//—————————————————————–
// Sets up this hourly employee using the specified information.
//—————————————————————–
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//—————————————————————–
// Adds the specified number of hours to this employee’s
// accumulated hours.
//—————————————————————–
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//—————————————————————–
// Computes and returns the pay for this hourly employee.
//—————————————————————–
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//—————————————————————–
// Returns information about this hourly employee as a string.
//—————————————————————–
public String toString()
{
String result = super.toString();
result += “nCurrent hours: ” + hoursWorked;
return result;
}
}
Deliverables
1. Complete all the activities in sections I & 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 lab5.pdf and 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.
3. Upload your lab report and the zip file of your source code to Blackboard. DO NOT SUBMIT separate source files. If you do, they will be ignored.

The post Java hw6 appeared first on ExpertCustomWritings.
Assignment status: Solved by our experts

>>>Click here to get this paper written at the best price. 100% Custom, 0% plagiarism.<<<

Leave a Reply

Your email address will not be published. Required fields are marked *