Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java program

Posted on 2010-08-25
10
Medium Priority
?
3,762 Views
Last Modified: 2013-11-23
the question in the book is as follows:
many programs written with inheritance could be written with composition instead, and vice versa. rewrite classBasePluscommissionEmployee (fig. 9.11) of the commissionEmployee-BasePlusCommissionEmployee hierarchy to use composition rather than inheritance.

what is the program code for this question?  I will attach the examples from the book.
cieasa

these are also from the book


// Exercise 9.3 Solution: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee using composition.

public class BasePlusCommissionEmployee
{
   private CommissionEmployee commissionEmployee; // composition
   private double baseSalary; // base salary per week

   // six-argument constructor
   public BasePlusCommissionEmployee( String first, String last, 
      String ssn, double sales, double rate, double salary )
   {
      commissionEmployee = 
         new CommissionEmployee( first, last, ssn, sales, rate );
      setBaseSalary( salary ); // validate and store base salary
   } // end six-argument BasePlusCommissionEmployee constructor
   
   // set first name
   public void setFirstName( String first )
   {
      commissionEmployee.setFirstName( first );
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return commissionEmployee.getFirstName();
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      commissionEmployee.setLastName( last );
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return commissionEmployee.getLastName();
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      commissionEmployee.setSocialSecurityNumber( ssn );
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return commissionEmployee.getSocialSecurityNumber();
   } // end method getSocialSecurityNumber

   // set commission employee's gross sales amount
   public void setGrossSales( double sales )
   {
      commissionEmployee.setGrossSales( sales );
   } // end method setGrossSales

   // return commission employee's gross sales amount
   public double getGrossSales()
   {
      return commissionEmployee.getGrossSales();
   } // end method getGrossSales
   
   // set commission employee's rate
   public void setCommissionRate( double rate )
   {
      commissionEmployee.setCommissionRate( rate );
   } // end method setCommissionRate

   // return commission employee's rate
   public double getCommissionRate()
   {
      return commissionEmployee.getCommissionRate();
   } // end method getCommissionRate

   // set base-salaried commission employee's base salary
   public void setBaseSalary( double salary )
   {
      baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
   } // end method setBaseSalary

   // return base-salaried commission employee's base salary
   public double getBaseSalary()
   {
      return baseSalary;
   } // end method getBaseSalary

   // calculate base-salaried commission employee's earnings
   public double earnings()
   {
      return getBaseSalary() + commissionEmployee.earnings();
   } // end method earnings

   // return String representation of BasePlusCommissionEmployee
   @Override
   public String toString()
   {
      return String.format( "%s %s\n%s: %.2f", "base-salaried", 
         commissionEmployee.toString(), "base salary", getBaseSalary() );
   } // end method toString
} // end class BasePlusCommissionEmployee

Open in new window


// Exercise 9.3 Solution: BasePlusCommissionEmployee.java
// Testing class BasePlusCommissionEmployee.

public class BasePlusCommissionEmployeeTest
{
   public static void main( String[] args ) 
   {
      // instantiate BasePlusCommissionEmployee object
      BasePlusCommissionEmployee employee = 
         new BasePlusCommissionEmployee( 
         "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
      
      // get base-salaried commission employee data
      System.out.println( 
         "Employee information obtained by get methods: \n" );
      System.out.printf( "%s %s\n", "First name is",
         employee.getFirstName() );
      System.out.printf( "%s %s\n", "Last name is", 
         employee.getLastName() );
      System.out.printf( "%s %s\n", "Social security number is", 
         employee.getSocialSecurityNumber() );
      System.out.printf( "%s %.2f\n", "Gross sales is", 
         employee.getGrossSales() );
      System.out.printf( "%s %.2f\n", "Commission rate is",
         employee.getCommissionRate() );
      System.out.printf( "%s %.2f\n", "Base salary is",
         employee.getBaseSalary() );

      employee.setBaseSalary( 1000 ); // set base salary
      
      System.out.printf( "\n%s:\n\n%s\n", 
         "Updated employee information obtained by toString", 
         employee.toString() );
   } // end main
} // end class BasePlusCommissionEmployeeTest

Open in new window


// Exercise 9.3 Solution: CommissionEmployee.java
// CommissionEmployee class represents a commission employee.

public class CommissionEmployee
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;
   private double grossSales; // gross weekly sales
   private double commissionRate; // commission percentage

   // five-argument constructor
   public CommissionEmployee( String first, String last, String ssn, 
      double sales, double rate )
   {
      // implicit call to Object constructor occurs here
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
      setGrossSales( sales ); // validate and store gross sales
      setCommissionRate( rate ); // validate and store commission rate
   } // end five-argument CommissionEmployee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first; // should validate
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      lastName = last; // should validate
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      socialSecurityNumber = ssn; // should validate
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   } // end method getSocialSecurityNumber

   // set gross sales amount
   public void setGrossSales( double sales )
   {
      grossSales = ( sales < 0.0 ) ? 0.0 : sales;
   } // end method setGrossSales

   // return gross sales amount
   public double getGrossSales()
   {
      return grossSales;
   } // end method getGrossSales
   
   // set commission rate
   public void setCommissionRate( double rate )
   {
      commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
   } // end method setCommissionRate

   // return commission rate
   public double getCommissionRate()
   {
      return commissionRate;
   } // end method getCommissionRate

   // calculate earnings
   public double earnings()
   {
      return getCommissionRate() * getGrossSales();
   } // end method earnings

   // return String representation of CommissionEmployee object
   @Override
   public String toString()
   {
      return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 
         "commission employee", getFirstName(), getLastName(), 
         "social security number", getSocialSecurityNumber(), 
         "gross sales", getGrossSales(), 
         "commission rate", getCommissionRate() );
   } // end method toString
} // end class CommissionEmployee

Open in new window


ch09.zip
0
Comment
Question by:cieasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 

Author Comment

by:cieasa
ID: 33527108
Thank you mwvisa1
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33527126
You are welcome. By the way, to answer your question, "Program" here is referring to the collective code including the implementation classes for types of employees and the driver (test class with public static void main method).

It would appear you have the code from the book on what composition is, so you should be able to study from that, right? Or are you having difficulty with some other aspect of the assignment.
0
 

Author Comment

by:cieasa
ID: 33527441
I am just lost on the whole thing.  one of the codes I pasted on here was from the books web page.  it looks almost right if you ask me, what do you think?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33527451
It does illustrate composition which is when one object is made up of its components (i.e., they are members of the class) versus inheritance where the object is its component. And the last phrasing is the best way to think about it...

composition == has // a car has 4 tires
inheritance == is a // a care is a vehicle
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33527453
Right is perspective. Given this is an assignment to see your interpretation and implementation, the correct code would be how you construct the class based on your understanding. *smile*
0
 

Author Comment

by:cieasa
ID: 33527516
I asume this is right?

public class BasePlusCommissionEmployee
{
   private CommissionEmployee commissionEmployee; // composition
   private double baseSalary; // base salary per week

   // six-argument constructor
   public BasePlusCommissionEmployee( String first, String last,
      String ssn, double sales, double rate, double salary )
   {
      commissionEmployee =
         new CommissionEmployee( first, last, ssn, sales, rate );
      setBaseSalary( salary ); // validate and store base salary
   } // end six-argument BasePlusCommissionEmployee constructor
   
   // set first name
   public void setFirstName( String first )
   {
      commissionEmployee.setFirstName( first );
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return commissionEmployee.getFirstName();
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      commissionEmployee.setLastName( last );
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return commissionEmployee.getLastName();
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      commissionEmployee.setSocialSecurityNumber( ssn );
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return commissionEmployee.getSocialSecurityNumber();
   } // end method getSocialSecurityNumber

   // set commission employee's gross sales amount
   public void setGrossSales( double sales )
   {
      commissionEmployee.setGrossSales( sales );
   } // end method setGrossSales

   // return commission employee's gross sales amount
   public double getGrossSales()
   {
      return commissionEmployee.getGrossSales();
   } // end method getGrossSales
   
   // set commission employee's rate
   public void setCommissionRate( double rate )
   {
      commissionEmployee.setCommissionRate( rate );
   } // end method setCommissionRate

   // return commission employee's rate
   public double getCommissionRate()
   {
      return commissionEmployee.getCommissionRate();
   } // end method getCommissionRate

   // set base-salaried commission employee's base salary
   public void setBaseSalary( double salary )
   {
      baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
   } // end method setBaseSalary

   // return base-salaried commission employee's base salary
   public double getBaseSalary()
   {
      return baseSalary;
   } // end method getBaseSalary

   // calculate base-salaried commission employee's earnings
   public double earnings()
   {
      return getBaseSalary() + commissionEmployee.earnings();
   } // end method earnings

   // return String representation of BasePlusCommissionEmployee
   @Override
   public String toString()
   {
      return String.format( "%s %s\n%s: %.2f", "base-salaried",
         commissionEmployee.toString(), "base salary", getBaseSalary() );
   } // end method toString
} // end class BasePlusCommissionEmployee

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 33527542
I didn't look at the original program in the zip file and so am not speaking to the code performing the logic it is intended, but yes this is an example of composition as it is commented to be:

public class BasePlusCommissionEmployee {
   private CommissionEmployee commissionEmployee; // composition
   
   // ...
}

Because BasePlusCommissionEmployee has a CommissionEmployee.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36541473
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question