Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The following is a toString( ) method for a GymMembership class.
public String toString( )
{
return “Member Number: “ + memberNumber
+ “Member Name: “ + memberName
+ “ Membership Type: “ + memberShipType;
}
Write two constructors for the GymMembership class as follows:
 A constructor to accept input parameters for the
memberNumber and memberName instance fields
 A constructor to accept input parameters for all three instance
fields

What I have tried:

This is what I've done so far; I think its wrong

public class GymMembership
{

//instance variables
private int memberNumber=0;
private String memberName=null;
private String memberShipType=null;

//constructor

public GymMembership(int memberNumber, String memberName, String memberShipType )
{
memberNumber=memberNumberIn;

memberName=memberNameIn;

memberShipType=memberShipTypeIn;

}
}
Posted
Updated 21-Jun-17 7:18am

1 solution

there are basically two ways:
public class SomeClass
{
    private int someMember;
    public SomeClass(int someMember)
    {
        this.someMember = someMember; // note the 'this' keyword denoting the constructed class
    }
}

or
public class SomeClass
{
    private int _someMember;
    public SomeClass(int someMember)
    {
        _someMember = someMember; // note the '_' prefix on the private member
    }
}

Essentially you need to indicate to the compiler what is what. When the parameter name is the same name as the member name, then the compiler will assume one or the other but not both. So you need to differentiate in order for the compiler to know what is wanted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900