Solved

java program

Posted on 2010-08-25
10
3,396 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
  • 5
  • 3
10 Comments
 

Author Comment

by:cieasa
Comment Utility
Thank you mwvisa1
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
Comment Utility
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
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
Comment Utility
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 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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 59

Expert Comment

by:Kevin Cross
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now