How can i implement this constructor?
I tried replacing commissionEmployee = new CommissionEmployee with this but the complier asked me to create another constructor.

public class BasePlusCommissionEmployeeComposition {

    private double baseSalary;
    private CommissionEmployee commissionEmployee;
    // six-argument constructor
    public BasePlusCommissionEmployeeComposition( String first, String last, 
            String ssn, double sales, double rate, double salary) {
        // TODO: implement this constructor
        commissionEmployee = new CommissionEmployee( first, last, ssn, sales, rate );
        setBaseSalary( salary );

We're not mind readers.
What exactly is the complete error message?

"Dont need you to be a mind reader."
No error messages.Just want to know if i can introduce this keyword in the above code.

Change the code as you want to do and try to compile it. If there are errors, copy and paste them here.

why doesnt this work.(Using Composition)
An example of the error when my cursor is over , say ,first-
"first cannot be resolved or is not a field"

public class BasePlusCommissionEmployee1 {

    private double baseSalary; // base salary per week
    // Demonstrate composition of (as opposed to inheritance from) a CommissionEmployee object
    private CommissionEmployee commissionEmployee;

    // six-argument constructor
    public BasePlusCommissionEmployee1( String first, String last, 
            String ssn, double sales, double rate, double salary) {
        // TODO: implement this constructor
        this.first = first;//complier find an error in .first
        this.last = last;//complier find an error in .last
        this.ssn = ssn;//complier find an error in .ssn
        this.sales = sales;//complier find an error in .sales
        this.rate = rate;//complier find an error in .rate
        this.salary = salary;//complier find an error in .salary

The mind reading was also needed to understand what you were trying to achieve.
Anyway. "first ... is not a field" You do know that you must declare variables in Java before you use them? Or are you confused about the use of "this"? Or is it that you don't understand composition (as implied by your latest code)?
You must explain your problem properly if you want a relevant answer.

do you havd those variables declared as instance variables? we can see you didn't declare them at the beginning of the class, but, then again, java does allow declaring them anywhere.
if you haven't:
this. refers to the current instance. this.first refers to the instance variable first of the current instance, if that variable isn't there, an error message is to be expected.

The truth is i have read composition and "this" a hundred times and still dont get a clear picture.:)

Composition is where you use an existing class to hold some of the data and methods for your class - in this case you use an instance of ComissionEmployee to hold all the basic info for your BasePlusCE. It's an alternative to extending ComissionEmployee which takes more work, but gets round the "can only extend one class" limitation.
In your constructor you have all the info parameters - the ones that BasePlusCE needs to pass on to your BasePlusCE's constructor, the rest you deal with yourself.

"this" is used to refer to the current instance where a method is running. It's used in various ways, but the relevant ones for constructors are
1. To refer to another constructor of the same class, eg you have 2 constructors

MyClass (int a, int b) { ... }  // explicit values for a and b
MyClass() { ...}  // default values

in that case you could write the second constructor like this

MyClass() { 
    this(0, 0); // use the other constructor to save the default values
}
  1. The other use is to refer to instance variables when you have "hidden" theor names by using the same name for a parameter, eg

    int x;                      // instance variable
    MyClass() {int x) {         // parameter "hides" the instance variable of the same name
        System.out.println(x);  // refers to the parameter 
        System.out.println(this.x); // refers to the instance variable
        this.x = x;             // copies the value of the parameter to the instance variable
    }
    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.