Question
Create a Java application with a public class named Assign92. This class will contain your main method and a polymorphic method. You will be creating other classes for your assignment, and you should not make those public. Your filename will be Assign92.java and that is the file you will submit when the program is working to your satisfaction.
The purpose of this assignment is to show you how you can use interfaces and inheritance togther to implement polymorphic behavior in your programs. This gives you great flexibility when creating real-world applications, but it requires a lot of careful design up-front. Before you can design, though, you need to undertand the underlying concepts, and that is what this chapter and this assignment in particular is all about.
Explanation / Answer
namespace SalesPersonDemo { public interface ISell { string SalesSpeach(); double MakeSale(); } abstract class Salesperson { private string firstName; private string lastName; public Salesperson(string first, string last) { FirstName = first; LastName = last; } public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } public abstract string FullName(); } class RealEstateSalesperson : Salesperson { private double houseVal; private double totalValDollars = 0; private double totalCommission = 0; public RealEstateSalesperson(double commissionRate) { CommissionRate = commissionRate; } public int TotalVal { get { return totalValDollars; } set { totalValDollars = value; } } public int TotalCommission { get { return totalCommission; } set { totalCommission = value; } } public int CommissionRate { get { return commissionRate; } set { commissionRate = value; } } public override string SalesSpeach() { return "We must remind ourselves that value, not just price has to be a driving force behind our success. "; } public override double MakeSale() { double total; total = CommissionRate * TotalVal +TotalVal; return total; } public override double FullName() { return FirstName + " " + LastName; } } class GirlScout : Salesperson { private int numOfboxes = 0; public int NumOfBoxes { get { return numOfboxes; } set { numOfboxes = value; } } public override string SalesSpeach() { return "Selling Girl Scout Cookies is an important component of the Girl Scout Leadership Experience for girls."; } public override double FullName() { return FirstName + " " + LastName; } } } namespace SalesPersonDemo { public interface ISell { string SalesSpeach(); double MakeSale(); } abstract class Salesperson { private string firstName; private string lastName; public Salesperson(string first, string last) { FirstName = first; LastName = last; } public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } public abstract string FullName(); } class RealEstateSalesperson : Salesperson { private double houseVal; private double totalValDollars = 0; private double totalCommission = 0; public RealEstateSalesperson(double commissionRate) { CommissionRate = commissionRate; } public int TotalVal { get { return totalValDollars; } set { totalValDollars = value; } } public int TotalCommission { get { return totalCommission; } set { totalCommission = value; } } public int CommissionRate { get { return commissionRate; } set { commissionRate = value; } } public override string SalesSpeach() { return "We must remind ourselves that value, not just price has to be a driving force behind our success. "; } public override double MakeSale() { double total; total = CommissionRate * TotalVal +TotalVal; return total; } public override double FullName() { return FirstName + " " + LastName; } } class GirlScout : Salesperson { private int numOfboxes = 0; public int NumOfBoxes { get { return numOfboxes; } set { numOfboxes = value; } } public override string SalesSpeach() { return "Selling Girl Scout Cookies is an important component of the Girl Scout Leadership Experience for girls."; } public override double FullName() { return FirstName + " " + LastName; } } }